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);
}
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);
}
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);
}
}
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");
}
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();
}
Aggregations