|
5 | 5 |
|
6 | 6 | import static org.assertj.core.api.Assertions.assertThat; |
7 | 7 |
|
| 8 | +import com.azure.monitor.opentelemetry.autoconfigure.implementation.builders.EventTelemetryBuilder; |
| 9 | +import com.azure.monitor.opentelemetry.autoconfigure.implementation.models.TelemetryItem; |
8 | 10 | import com.microsoft.applicationinsights.agent.internal.configuration.GcReportingLevel; |
9 | 11 | import com.microsoft.applicationinsights.agent.internal.profiler.testutil.TestTimeSource; |
| 12 | +import com.microsoft.applicationinsights.agent.internal.sampling.SamplerUtil; |
10 | 13 | import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient; |
11 | 14 | import com.microsoft.applicationinsights.alerting.AlertingSubsystem; |
12 | 15 | import com.microsoft.applicationinsights.alerting.alert.AlertBreach; |
|
29 | 32 | import java.util.concurrent.TimeoutException; |
30 | 33 | import javax.management.MBeanServerConnection; |
31 | 34 | import org.junit.jupiter.api.Test; |
| 35 | +import org.mockito.ArgumentCaptor; |
32 | 36 | import org.mockito.Mockito; |
33 | 37 |
|
34 | 38 | class GcEventInitTest { |
35 | 39 |
|
| 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 | + |
36 | 88 | @Test |
37 | 89 | void endToEndAlertIsTriggered() |
38 | 90 | throws ExecutionException, InterruptedException, TimeoutException { |
@@ -102,4 +154,25 @@ private static GcCollectionEvent mockGcEvent() { |
102 | 154 | .thenReturn(new MemoryUsage(1, 9, 10, 10)); |
103 | 155 | return event; |
104 | 156 | } |
| 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 | + } |
105 | 178 | } |
0 commit comments