Search in sources :

Example 1 with TelemetryClient

use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient in project ApplicationInsights-Java by microsoft.

the class ProfilerServiceTest method endToEndAlertTriggerCycle.

void endToEndAlertTriggerCycle(boolean triggerNow, TelemetryItem metricTelemetry, Consumer<TelemetryEventData> assertTelemetry) throws Exception {
    AtomicBoolean profileInvoked = new AtomicBoolean(false);
    AtomicReference<TelemetryEventData> serviceProfilerIndex = new AtomicReference<>();
    String appId = UUID.randomUUID().toString();
    ServiceProfilerClientV2 clientV2 = stubClient(triggerNow);
    Supplier<String> appIdSupplier = () -> appId;
    ServiceProfilerUploader serviceProfilerUploader = getServiceProfilerJfrUpload(clientV2, appIdSupplier);
    JfrProfiler jfrProfiler = getJfrDaemon(profileInvoked);
    Object monitor = new Object();
    TelemetryClient client = spy(TelemetryClient.builder().setCustomDimensions(new HashMap<>()).build());
    doAnswer(invocation -> {
        TelemetryItem telemetry = invocation.getArgument(0);
        MonitorDomain data = telemetry.getData().getBaseData();
        if (data instanceof TelemetryEventData) {
            if ("ServiceProfilerIndex".equals(((TelemetryEventData) data).getName())) {
                serviceProfilerIndex.set((TelemetryEventData) data);
            }
            synchronized (monitor) {
                monitor.notifyAll();
            }
        }
        return null;
    }).when(client).trackAsync(any(TelemetryItem.class));
    ScheduledExecutorService serviceProfilerExecutorService = Executors.newScheduledThreadPool(2, ThreadPoolUtils.createDaemonThreadFactory(ProfilerServiceFactory.class, "ServiceProfilerService"));
    ScheduledExecutorService alertServiceExecutorService = Executors.newSingleThreadScheduledExecutor(ThreadPoolUtils.createDaemonThreadFactory(ProfilerServiceFactory.class, "ServiceProfilerAlertingService"));
    AtomicReference<ProfilerService> service = new AtomicReference<>();
    AlertingSubsystem alertService = AlertingServiceFactory.create(alert -> awaitReferenceSet(service).getProfiler().accept(alert), TelemetryObservers.INSTANCE, client, alertServiceExecutorService, new GcEventMonitor.GcEventMonitorConfiguration(GcReportingLevel.ALL));
    service.set(new JfrProfilerService(() -> appId, new ServiceProfilerServiceConfig(1, 2, 3, new URL("http://localhost"), null, null, LocalFileSystemUtils.getTempDir()), jfrProfiler, ProfilerServiceInitializer.updateAlertingConfig(alertService), ProfilerServiceInitializer.sendServiceProfilerIndex(client), clientV2, serviceProfilerUploader, serviceProfilerExecutorService).initialize().get());
    // Wait up to 10 seconds
    for (int i = 0; i < 100; i++) {
        TelemetryObservers.INSTANCE.getObservers().forEach(telemetryObserver -> telemetryObserver.accept(metricTelemetry));
        synchronized (monitor) {
            if (serviceProfilerIndex.get() != null) {
                break;
            }
            monitor.wait(100);
        }
    }
    assertThat(profileInvoked.get()).isTrue();
    assertThat(serviceProfilerIndex.get()).isNotNull();
    assertThat(serviceProfilerIndex.get().getProperties().get("ArtifactKind")).isEqualTo("Profile");
    assertThat(serviceProfilerIndex.get().getProperties().get("EtlFileSessionId")).isEqualTo(timeStamp);
    assertThat(serviceProfilerIndex.get().getProperties().get("DataCube")).isEqualTo(appId);
    assertThat(serviceProfilerIndex.get().getProperties().get("Extension")).isEqualTo(jfrExtension);
    assertThat(serviceProfilerIndex.get().getProperties().get("MachineName")).isEqualTo(machineName);
    assertThat(serviceProfilerIndex.get().getProperties().get("ProcessId")).isEqualTo(processId);
    assertThat(serviceProfilerIndex.get().getProperties().get("StampId")).isEqualTo(stampId);
    assertTelemetry.accept(serviceProfilerIndex.get());
}
Also used : MonitorDomain(com.microsoft.applicationinsights.agent.internal.exporter.models.MonitorDomain) ServiceProfilerUploader(com.microsoft.applicationinsights.serviceprofilerapi.upload.ServiceProfilerUploader) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) AlertingSubsystem(com.microsoft.applicationinsights.alerting.AlertingSubsystem) URL(java.net.URL) JfrProfilerService(com.microsoft.applicationinsights.serviceprofilerapi.JfrProfilerService) ProfilerServiceFactory(com.microsoft.applicationinsights.profiler.ProfilerServiceFactory) ProfilerService(com.microsoft.applicationinsights.profiler.ProfilerService) JfrProfilerService(com.microsoft.applicationinsights.serviceprofilerapi.JfrProfilerService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) AtomicReference(java.util.concurrent.atomic.AtomicReference) JfrProfiler(com.microsoft.applicationinsights.serviceprofilerapi.profiler.JfrProfiler) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ServiceProfilerServiceConfig(com.microsoft.applicationinsights.profiler.config.ServiceProfilerServiceConfig) ServiceProfilerClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2) TelemetryEventData(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)

Example 2 with TelemetryClient

use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient in project ApplicationInsights-Java by microsoft.

the class QuickPulseDataCollectorTests method dependencyTelemetryIsCounted_DurationIsSum.

@Test
void dependencyTelemetryIsCounted_DurationIsSum() {
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.setQuickPulseStatus(QuickPulseStatus.QP_IS_ON);
    QuickPulseDataCollector.INSTANCE.enable(telemetryClient);
    // add a success and peek.
    final long duration = 112233L;
    TelemetryItem telemetry = createRemoteDependencyTelemetry("dep-test", "dep-test-cmd", duration, true);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek();
    assertThat(counters.rdds).isEqualTo(1);
    assertThat(counters.unsuccessfulRdds).isEqualTo(0);
    assertThat(counters.rddsDuration).isEqualTo(duration);
    // add another success and peek.
    final long duration2 = 334455L;
    telemetry = createRemoteDependencyTelemetry("dep-test-2", "dep-test-cmd-2", duration2, true);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    counters = QuickPulseDataCollector.INSTANCE.peek();
    assertThat(counters.rdds).isEqualTo(2);
    assertThat(counters.unsuccessfulRdds).isEqualTo(0);
    double total = duration + duration2;
    assertThat(counters.rddsDuration).isEqualTo(total);
    // add a failure and get/reset.
    final long duration3 = 123456L;
    telemetry = createRemoteDependencyTelemetry("dep-test-3", "dep-test-cmd-3", duration3, false);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    counters = QuickPulseDataCollector.INSTANCE.getAndRestart();
    assertThat(counters.rdds).isEqualTo(3);
    assertThat(counters.unsuccessfulRdds).isEqualTo(1);
    total += duration3;
    assertThat(counters.rddsDuration).isEqualTo(total);
    assertCountersReset(QuickPulseDataCollector.INSTANCE.peek());
}
Also used : TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) FinalCounters(com.microsoft.applicationinsights.agent.internal.quickpulse.QuickPulseDataCollector.FinalCounters) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Test(org.junit.jupiter.api.Test)

Example 3 with TelemetryClient

use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient in project ApplicationInsights-Java by microsoft.

the class QuickPulseDataCollectorTests method checkDocumentsListSize.

@Test
void checkDocumentsListSize() {
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.setQuickPulseStatus(QuickPulseStatus.QP_IS_ON);
    QuickPulseDataCollector.INSTANCE.enable(telemetryClient);
    final long duration = 112233L;
    TelemetryItem telemetry = createRequestTelemetry("request-test", new Date(), duration, "200", true);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    for (int i = 0; i < 1005; i++) {
        QuickPulseDataCollector.INSTANCE.add(telemetry);
    }
    // check max documentList size
    assertThat(QuickPulseDataCollector.INSTANCE.getAndRestart().documentList.size()).isEqualTo(1000);
    QuickPulseDataCollector.INSTANCE.setQuickPulseStatus(QuickPulseStatus.QP_IS_OFF);
    for (int i = 0; i < 5; i++) {
        QuickPulseDataCollector.INSTANCE.add(telemetry);
    }
    // no telemetry items are added when QP_IS_OFF
    assertThat(QuickPulseDataCollector.INSTANCE.getAndRestart().documentList.size()).isEqualTo(0);
}
Also used : TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 4 with TelemetryClient

use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient in project ApplicationInsights-Java by microsoft.

the class QuickPulseDataCollectorTests method requestTelemetryIsCounted_DurationIsSum.

@Test
void requestTelemetryIsCounted_DurationIsSum() {
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.setQuickPulseStatus(QuickPulseStatus.QP_IS_ON);
    QuickPulseDataCollector.INSTANCE.enable(telemetryClient);
    // add a success and peek
    final long duration = 112233L;
    TelemetryItem telemetry = createRequestTelemetry("request-test", new Date(), duration, "200", true);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    FinalCounters counters = QuickPulseDataCollector.INSTANCE.peek();
    assertThat(counters.requests).isEqualTo(1);
    assertThat(counters.unsuccessfulRequests).isEqualTo(0);
    assertThat(counters.requestsDuration).isEqualTo(duration);
    // add another success and peek
    final long duration2 = 65421L;
    telemetry = createRequestTelemetry("request-test-2", new Date(), duration2, "200", true);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    counters = QuickPulseDataCollector.INSTANCE.peek();
    double total = duration + duration2;
    assertThat(counters.requests).isEqualTo(2);
    assertThat(counters.unsuccessfulRequests).isEqualTo(0);
    assertThat(counters.requestsDuration).isEqualTo(total);
    // add a failure and get/reset
    final long duration3 = 9988L;
    telemetry = createRequestTelemetry("request-test-3", new Date(), duration3, "400", false);
    telemetry.setInstrumentationKey(FAKE_INSTRUMENTATION_KEY);
    QuickPulseDataCollector.INSTANCE.add(telemetry);
    counters = QuickPulseDataCollector.INSTANCE.getAndRestart();
    total += duration3;
    assertThat(counters.requests).isEqualTo(3);
    assertThat(counters.unsuccessfulRequests).isEqualTo(1);
    assertThat(counters.requestsDuration).isEqualTo(total);
    assertCountersReset(QuickPulseDataCollector.INSTANCE.peek());
}
Also used : TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) FinalCounters(com.microsoft.applicationinsights.agent.internal.quickpulse.QuickPulseDataCollector.FinalCounters) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with TelemetryClient

use of com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient in project ApplicationInsights-Java by microsoft.

the class QuickPulseDataFetcherTests method endpointChangesWithRedirectHeaderAndGetNewPingInterval.

@Test
void endpointChangesWithRedirectHeaderAndGetNewPingInterval() {
    Map<String, String> headers = new HashMap<>();
    headers.put("x-ms-qps-service-polling-interval-hint", "1000");
    headers.put("x-ms-qps-service-endpoint-redirect", "https://new.endpoint.com");
    headers.put("x-ms-qps-subscribed", "true");
    HttpHeaders httpHeaders = new HttpHeaders(headers);
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setInstrumentationKey("fake-key");
    HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(request -> Mono.just(new MockHttpResponse(request, 200, httpHeaders))).build();
    QuickPulsePingSender quickPulsePingSender = new QuickPulsePingSender(httpPipeline, telemetryClient, "machine1", "instance1", "qpid123");
    QuickPulseHeaderInfo quickPulseHeaderInfo = quickPulsePingSender.ping(null);
    assertThat(QuickPulseStatus.QP_IS_ON).isEqualTo(quickPulseHeaderInfo.getQuickPulseStatus());
    assertThat(1000).isEqualTo(quickPulseHeaderInfo.getQpsServicePollingInterval());
    assertThat("https://new.endpoint.com").isEqualTo(quickPulseHeaderInfo.getQpsServiceEndpointRedirect());
}
Also used : Test(org.junit.jupiter.api.Test) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HttpPipeline(com.azure.core.http.HttpPipeline) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) MockHttpResponse(com.microsoft.applicationinsights.agent.internal.MockHttpResponse) HashMap(java.util.HashMap) Mono(reactor.core.publisher.Mono) HttpHeaders(com.azure.core.http.HttpHeaders) URI(java.net.URI) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) HttpHeaders(com.azure.core.http.HttpHeaders) HashMap(java.util.HashMap) HttpPipeline(com.azure.core.http.HttpPipeline) HttpPipelineBuilder(com.azure.core.http.HttpPipelineBuilder) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) MockHttpResponse(com.microsoft.applicationinsights.agent.internal.MockHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

TelemetryClient (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)28 Test (org.junit.jupiter.api.Test)21 TelemetryItem (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem)7 Configuration (com.microsoft.applicationinsights.agent.internal.configuration.Configuration)6 URI (java.net.URI)5 FinalCounters (com.microsoft.applicationinsights.agent.internal.quickpulse.QuickPulseDataCollector.FinalCounters)4 RpConfiguration (com.microsoft.applicationinsights.agent.internal.configuration.RpConfiguration)3 Date (java.util.Date)3 HttpHeaders (com.azure.core.http.HttpHeaders)2 HttpPipeline (com.azure.core.http.HttpPipeline)2 HttpPipelineBuilder (com.azure.core.http.HttpPipelineBuilder)2 MockHttpResponse (com.microsoft.applicationinsights.agent.internal.MockHttpResponse)2 ProcessorConfig (com.microsoft.applicationinsights.agent.internal.configuration.Configuration.ProcessorConfig)2 AiLegacyHeaderSpanProcessor (com.microsoft.applicationinsights.agent.internal.legacyheaders.AiLegacyHeaderSpanProcessor)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Mono (reactor.core.publisher.Mono)2 HttpRequest (com.azure.core.http.HttpRequest)1