Search in sources :

Example 16 with TelemetryClient

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

the class LazyConfigurationAccessorTest method disableLazySetWithLazySetOptInOffConnectionStringNotNullInstrumentationKeyNull.

@Test
// is TRUE"
void disableLazySetWithLazySetOptInOffConnectionStringNotNullInstrumentationKeyNull() {
    assertThat(LazyConfigurationAccessor.shouldSetConnectionString(false, "true")).isTrue();
    // given
    TelemetryClient telemetryClient = mock(TelemetryClient.class);
    AppIdSupplier appIdSupplier = mock(AppIdSupplier.class);
    LazyConfigurationAccessor lazyConfigurationAccessor = new LazyConfigurationAccessor(telemetryClient, appIdSupplier);
    // when
    lazyConfigurationAccessor.setConnectionString(CONNECTION_STRING, null);
    // then
    verify(telemetryClient).setConnectionString(CONNECTION_STRING);
    // when
    lazyConfigurationAccessor.setWebsiteSiteName(WEBSITE_SITE_NAME);
    // then
    verify(telemetryClient).setRoleName(WEBSITE_SITE_NAME);
}
Also used : TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Test(org.junit.jupiter.api.Test)

Example 17 with TelemetryClient

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

the class LazyConfigurationAccessorTest method enableLazySetWithLazySetOptInOnConnectionStringNullInstrumentationKeyNotNull.

@Test
// is TRUE"
void enableLazySetWithLazySetOptInOnConnectionStringNullInstrumentationKeyNotNull() {
    assertThat(LazyConfigurationAccessor.shouldSetConnectionString(false, "true")).isTrue();
    // given
    TelemetryClient telemetryClient = mock(TelemetryClient.class);
    AppIdSupplier appIdSupplier = mock(AppIdSupplier.class);
    LazyConfigurationAccessor lazyConfigurationAccessor = new LazyConfigurationAccessor(telemetryClient, appIdSupplier);
    // when
    lazyConfigurationAccessor.setConnectionString(null, INSTRUMENTATION_KEY);
    // then
    verify(telemetryClient).setConnectionString("InstrumentationKey=" + INSTRUMENTATION_KEY);
}
Also used : TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Test(org.junit.jupiter.api.Test)

Example 18 with TelemetryClient

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

the class OpenTelemetryConfigurer method configure.

@Override
@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", justification = "this method is only called once during initialization")
public void configure(SdkTracerProviderBuilder tracerProvider, ConfigProperties config) {
    TelemetryClient telemetryClient = TelemetryClient.getActive();
    if (telemetryClient == null) {
        // agent failed during startup
        return;
    }
    Configuration configuration = MainEntryPoint.getConfiguration();
    tracerProvider.setSampler(DelegatingSampler.getInstance());
    if (configuration.connectionString != null) {
        if (!configuration.preview.disablePropagation) {
            DelegatingPropagator.getInstance().setUpStandardDelegate(configuration.preview.legacyRequestIdPropagation.enabled);
        }
        DelegatingSampler.getInstance().setDelegate(Samplers.getSampler(configuration.sampling.percentage, configuration));
    } else {
    // in Azure Functions, we configure later on, once we know user has opted in to tracing
    // (note: the default for DelegatingPropagator is to not propagate anything
    // and the default for DelegatingSampler is to not sample anything)
    }
    // operation name span processor is only applied on span start, so doesn't need to be chained
    // with the batch span processor
    tracerProvider.addSpanProcessor(new AiOperationNameSpanProcessor());
    // chained with the batch span processor
    if (!configuration.preview.inheritedAttributes.isEmpty()) {
        tracerProvider.addSpanProcessor(new InheritedAttributesSpanProcessor(configuration.preview.inheritedAttributes));
    }
    // propagator)
    if (configuration.preview.legacyRequestIdPropagation.enabled) {
        tracerProvider.addSpanProcessor(new AiLegacyHeaderSpanProcessor());
    }
    // to be chained with the batch span processor
    if (!configuration.preview.instrumentationKeyOverrides.isEmpty()) {
        tracerProvider.addSpanProcessor(new InheritedInstrumentationKeySpanProcessor(configuration.preview.instrumentationKeyOverrides));
    }
    String tracesExporter = config.getString("otel.traces.exporter");
    if ("none".equals(tracesExporter)) {
        batchSpanProcessor = createExporter(configuration);
        tracerProvider.addSpanProcessor(batchSpanProcessor);
    }
}
Also used : Configuration(com.microsoft.applicationinsights.agent.internal.configuration.Configuration) AiLegacyHeaderSpanProcessor(com.microsoft.applicationinsights.agent.internal.legacyheaders.AiLegacyHeaderSpanProcessor) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 19 with TelemetryClient

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

the class QuickPulseIntegrationTests method getQuickPulsePingSenderWithAuthentication.

private QuickPulsePingSender getQuickPulsePingSenderWithAuthentication() {
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setConnectionString(connectionString);
    return new QuickPulsePingSender(getHttpPipelineWithAuthentication(), telemetryClient, "machine1", "instance1", "qpid123");
}
Also used : TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)

Example 20 with TelemetryClient

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

the class QuickPulseIntegrationTests method testPostRequest.

@Test
public void testPostRequest() throws InterruptedException {
    ArrayBlockingQueue<HttpRequest> sendQueue = new ArrayBlockingQueue<>(256, true);
    CountDownLatch pingCountDown = new CountDownLatch(1);
    CountDownLatch postCountDown = new CountDownLatch(1);
    Date currDate = new Date();
    String expectedPingRequestBody = "\\{\"Documents\":null,\"InstrumentationKey\":null,\"Metrics\":null,\"InvariantVersion\":1,\"Timestamp\":\"\\\\/Date\\(\\d+\\)\\\\/\",\"Version\":\"\\(unknown\\)\",\"StreamId\":\"qpid123\",\"MachineName\":\"machine1\",\"Instance\":\"instance1\",\"RoleName\":null\\}";
    String expectedPostRequestBody = "\\[\\{\"Documents\":\\[\\{\"__type\":\"RequestTelemetryDocument\",\"DocumentType\":\"Request\",\"Version\":\"1.0\",\"OperationId\":null,\"Properties\":\\{\"customProperty\":\"customValue\"\\},\"Name\":\"request-test\",\"Success\":true,\"Duration\":\"PT.*S\",\"ResponseCode\":\"200\",\"OperationName\":null,\"Url\":\"foo\"\\},\\{\"__type\":\"DependencyTelemetryDocument\",\"DocumentType\":\"RemoteDependency\",\"Version\":\"1.0\",\"OperationId\":null,\"Properties\":\\{\"customProperty\":\"customValue\"\\},\"Name\":\"dep-test\",\"Target\":null,\"Success\":true,\"Duration\":\"PT.*S\",\"ResultCode\":null,\"CommandName\":\"dep-test-cmd\",\"DependencyTypeName\":null,\"OperationName\":null\\},\\{\"__type\":\"ExceptionTelemetryDocument\",\"DocumentType\":\"Exception\",\"Version\":\"1.0\",\"OperationId\":null,\"Properties\":null,\"Exception\":\"\",\"ExceptionMessage\":\"test\",\"ExceptionType\":\"java.lang.Exception\"\\}\\],\"InstrumentationKey\":\"" + instrumentationKey + "\",\"Metrics\":\\[\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Requests\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Request Duration\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Requests Failed\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Requests Succeeded\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Dependency Calls\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Dependency Call Duration\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Dependency Calls Failed\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Dependency Calls Succeeded\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\ApplicationInsights\\\\\\\\Exceptions\\\\\\/Sec\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\Memory\\\\\\\\Committed Bytes\",\"Value\":\\d+,\"Weight\":\\d+\\},\\{\"Name\":\"\\\\\\\\Processor\\(_Total\\)\\\\\\\\% Processor Time\",\"Value\":-?\\d+,\"Weight\":\\d+\\}\\],\"InvariantVersion\":1,\"Timestamp\":\"\\\\\\/Date\\(\\d+\\)\\\\\\/\",\"Version\":\"[^\"]*\",\"StreamId\":null,\"MachineName\":\"machine1\",\"Instance\":\"instance1\",\"RoleName\":null\\}\\]";
    QuickPulsePingSender pingSender = getQuickPulsePingSenderWithValidator(new ValidationPolicy(pingCountDown, expectedPingRequestBody));
    QuickPulseHeaderInfo quickPulseHeaderInfo = pingSender.ping(null);
    QuickPulseDataSender dataSender = new QuickPulseDataSender(getHttpPipeline(new ValidationPolicy(postCountDown, expectedPostRequestBody)), sendQueue);
    TelemetryClient telemetryClient = TelemetryClient.createForTest();
    telemetryClient.setConnectionString(connectionString);
    QuickPulseDataFetcher dataFetcher = new QuickPulseDataFetcher(sendQueue, telemetryClient, "machine1", "instance1", null);
    QuickPulseDataCollector.INSTANCE.setQuickPulseStatus(QuickPulseStatus.QP_IS_ON);
    QuickPulseDataCollector.INSTANCE.enable(telemetryClient);
    final long duration = 112233L;
    // Request Telemetry
    TelemetryItem requestTelemetry = createRequestTelemetry("request-test", currDate, duration, "200", true);
    requestTelemetry.setInstrumentationKey(instrumentationKey);
    QuickPulseDataCollector.INSTANCE.add(requestTelemetry);
    // Dependency Telemetry
    TelemetryItem dependencyTelemetry = createRemoteDependencyTelemetry("dep-test", "dep-test-cmd", duration, true);
    dependencyTelemetry.setInstrumentationKey(instrumentationKey);
    QuickPulseDataCollector.INSTANCE.add(dependencyTelemetry);
    // Exception Telemetry
    TelemetryItem exceptionTelemetry = createExceptionTelemetry(new Exception("test"));
    exceptionTelemetry.setInstrumentationKey(instrumentationKey);
    QuickPulseDataCollector.INSTANCE.add(exceptionTelemetry);
    QuickPulseCoordinatorInitData initData = new QuickPulseCoordinatorInitDataBuilder().withDataFetcher(dataFetcher).withDataSender(dataSender).withPingSender(pingSender).withWaitBetweenPingsInMillis(10L).withWaitBetweenPostsInMillis(10L).withWaitOnErrorInMillis(10L).build();
    QuickPulseCoordinator coordinator = new QuickPulseCoordinator(initData);
    Thread coordinatorThread = new Thread(coordinator, QuickPulseCoordinator.class.getSimpleName());
    coordinatorThread.setDaemon(true);
    coordinatorThread.start();
    Thread senderThread = new Thread(dataSender, QuickPulseDataSender.class.getSimpleName());
    senderThread.setDaemon(true);
    senderThread.start();
    Thread.sleep(50);
    assertTrue(pingCountDown.await(1, TimeUnit.SECONDS));
    assertThat(quickPulseHeaderInfo.getQuickPulseStatus()).isEqualTo(QuickPulseStatus.QP_IS_ON);
    assertThat(QuickPulseDataCollector.INSTANCE.getQuickPulseStatus()).isEqualTo(QuickPulseStatus.QP_IS_ON);
    assertTrue(postCountDown.await(1, TimeUnit.SECONDS));
    senderThread.interrupt();
    coordinatorThread.interrupt();
}
Also used : HttpRequest(com.azure.core.http.HttpRequest) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) CountDownLatch(java.util.concurrent.CountDownLatch) TelemetryClient(com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient) Date(java.util.Date) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) 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