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