Skip to content

Commit c25926a

Browse files
johnoliverCopilotCopilotCopilot
authored
Apply sample rate to all profiler subsystem telemetry to bypass ingestion sampling (#4786)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 666bcea commit c25926a

4 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/AlertingSubsystemInit.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.microsoft.applicationinsights.agent.internal.profiler.Profiler;
1717
import com.microsoft.applicationinsights.agent.internal.profiler.ProfilerControl;
1818
import com.microsoft.applicationinsights.agent.internal.profiler.upload.ServiceProfilerIndex;
19+
import com.microsoft.applicationinsights.agent.internal.sampling.SamplerUtil;
1920
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
2021
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryObservers;
2122
import com.microsoft.applicationinsights.alerting.AlertingSubsystem;
@@ -149,6 +150,7 @@ private static void sendServiceProfilerIndex(
149150
EventTelemetryBuilder telemetryBuilder = telemetryClient.newEventTelemetryBuilder();
150151

151152
telemetryBuilder.setName("ServiceProfilerIndex");
153+
telemetryBuilder.setSampleRate((float) SamplerUtil.SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING);
152154

153155
for (Map.Entry<String, String> entry : serviceProfilerIndex.getProperties().entrySet()) {
154156
telemetryBuilder.addProperty(entry.getKey(), entry.getValue());
@@ -170,6 +172,7 @@ private static void sendMessageTelemetry(TelemetryClient telemetryClient, String
170172
MessageTelemetryBuilder telemetryBuilder = telemetryClient.newMessageTelemetryBuilder();
171173

172174
telemetryBuilder.setMessage(message);
175+
telemetryBuilder.setSampleRate((float) SamplerUtil.SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING);
173176
telemetryBuilder.setTime(FormattedTime.offSetDateTimeFromNow());
174177

175178
telemetryClient.trackAsync(telemetryBuilder.build());

‎agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/GcEventInit.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.FormattedTime;
88
import com.microsoft.applicationinsights.agent.internal.configuration.GcReportingLevel;
99
import com.microsoft.applicationinsights.agent.internal.profiler.util.ServiceLoaderUtil;
10+
import com.microsoft.applicationinsights.agent.internal.sampling.SamplerUtil;
1011
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
1112
import com.microsoft.applicationinsights.alerting.AlertingSubsystem;
1213
import com.microsoft.applicationinsights.alerting.config.AlertMetricType;
@@ -125,6 +126,7 @@ private static void emitGcEvent(
125126
EventTelemetryBuilder telemetryBuilder = telemetryClient.newEventTelemetryBuilder();
126127

127128
telemetryBuilder.setName("GcEvent");
129+
telemetryBuilder.setSampleRate((float) SamplerUtil.SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING);
128130

129131
telemetryBuilder.addProperty("collector", event.getCollector().getName());
130132
telemetryBuilder.addProperty("type", event.getGcCause());

‎agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/sampling/SamplerUtil.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
public class SamplerUtil {
1414

15-
private static final double SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING = 99.99;
15+
public static final double SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING = 99.99;
1616

1717
private static final Cache<Double, SamplingResult> recordAndSampleWithSampleRateMap =
1818
Cache.bounded(100);

‎agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/triggers/GcEventInitTest.java‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55

66
import static org.assertj.core.api.Assertions.assertThat;
77

8+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.builders.EventTelemetryBuilder;
9+
import com.azure.monitor.opentelemetry.autoconfigure.implementation.models.TelemetryItem;
810
import com.microsoft.applicationinsights.agent.internal.configuration.GcReportingLevel;
911
import com.microsoft.applicationinsights.agent.internal.profiler.testutil.TestTimeSource;
12+
import com.microsoft.applicationinsights.agent.internal.sampling.SamplerUtil;
1013
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
1114
import com.microsoft.applicationinsights.alerting.AlertingSubsystem;
1215
import com.microsoft.applicationinsights.alerting.alert.AlertBreach;
@@ -29,10 +32,59 @@
2932
import java.util.concurrent.TimeoutException;
3033
import javax.management.MBeanServerConnection;
3134
import org.junit.jupiter.api.Test;
35+
import org.mockito.ArgumentCaptor;
3236
import org.mockito.Mockito;
3337

3438
class GcEventInitTest {
3539

40+
@Test
41+
void gcEventIsEmittedWithSampleRateThatBypassesIngestionSampling() {
42+
43+
TelemetryClient telemetryClient = Mockito.mock(TelemetryClient.class);
44+
Mockito.when(telemetryClient.newEventTelemetryBuilder())
45+
.thenReturn(EventTelemetryBuilder.create());
46+
47+
CompletableFuture<AlertBreach> alertFuture = new CompletableFuture<>();
48+
TestTimeSource timeSource = new TestTimeSource();
49+
AlertingSubsystem alertingSubsystem = getAlertingSubsystem(alertFuture, timeSource);
50+
51+
GcMonitorFactory factory =
52+
new GcMonitorFactory() {
53+
@Override
54+
public MemoryManagement monitorSelf(
55+
ExecutorService executorService, GcEventConsumer consumer) {
56+
consumer.accept(fullyMockedGcEvent());
57+
return null;
58+
}
59+
60+
@Override
61+
public MemoryManagement monitor(
62+
MBeanServerConnection connection,
63+
ExecutorService executorService,
64+
GcEventConsumer consumer) {
65+
return null;
66+
}
67+
};
68+
69+
ExecutorService executorService = Executors.newSingleThreadExecutor();
70+
try {
71+
GcEventInit.init(
72+
alertingSubsystem,
73+
telemetryClient,
74+
executorService,
75+
new GcEventInit.GcEventMonitorConfiguration(GcReportingLevel.ALL),
76+
factory);
77+
78+
ArgumentCaptor<TelemetryItem> captor = ArgumentCaptor.forClass(TelemetryItem.class);
79+
Mockito.verify(telemetryClient).trackAsync(captor.capture());
80+
81+
assertThat(captor.getValue().getSampleRate())
82+
.isEqualTo((float) SamplerUtil.SAMPLE_RATE_TO_DISABLE_INGESTION_SAMPLING);
83+
} finally {
84+
executorService.shutdownNow();
85+
}
86+
}
87+
3688
@Test
3789
void endToEndAlertIsTriggered()
3890
throws ExecutionException, InterruptedException, TimeoutException {
@@ -102,4 +154,25 @@ private static GcCollectionEvent mockGcEvent() {
102154
.thenReturn(new MemoryUsage(1, 9, 10, 10));
103155
return event;
104156
}
157+
158+
private static GcCollectionEvent fullyMockedGcEvent() {
159+
GcCollectionEvent event = Mockito.mock(GcCollectionEvent.class);
160+
GarbageCollector collector = Mockito.mock(GarbageCollector.class);
161+
MemoryPool tenuredPool = Mockito.mock(MemoryPool.class);
162+
MemoryUsage memoryUsage = new MemoryUsage(1, 9, 10, 10);
163+
164+
Mockito.when(collector.isTenuredCollector()).thenReturn(true);
165+
Mockito.when(collector.getName()).thenReturn("test-collector");
166+
Mockito.when(event.getCollector()).thenReturn(collector);
167+
Mockito.when(event.getGcCause()).thenReturn("test-cause");
168+
Mockito.when(event.getGcAction()).thenReturn("test-action");
169+
Mockito.when(event.getTenuredPool()).thenReturn(Optional.of(tenuredPool));
170+
Mockito.when(event.getMemoryUsageBeforeGc(Mockito.any(MemoryPool.class)))
171+
.thenReturn(memoryUsage);
172+
Mockito.when(event.getMemoryUsageAfterGc(Mockito.any(MemoryPool.class)))
173+
.thenReturn(memoryUsage);
174+
Mockito.when(event.getMemoryUsageBeforeGc(Mockito.anyList())).thenReturn(memoryUsage);
175+
Mockito.when(event.getMemoryUsageAfterGc(Mockito.anyList())).thenReturn(memoryUsage);
176+
return event;
177+
}
105178
}

0 commit comments

Comments
 (0)