Search in sources :

Example 1 with TelemetryItem

use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem 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 TelemetryItem

use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem 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 TelemetryItem

use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem 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 TelemetryItem

use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem 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 TelemetryItem

use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem in project ApplicationInsights-Java by microsoft.

the class QuickPulseTestBase method createRemoteDependencyTelemetry.

public static TelemetryItem createRemoteDependencyTelemetry(String name, String command, long durationMillis, boolean success) {
    TelemetryItem telemetry = new TelemetryItem();
    RemoteDependencyData data = new RemoteDependencyData();
    TelemetryClient.createForTest().initRemoteDependencyTelemetry(telemetry, data);
    Map<String, String> properties = new HashMap<>();
    properties.put("customProperty", "customValue");
    data.setProperties(properties);
    data.setName(name);
    data.setData(command);
    data.setDuration(FormattedDuration.fromMillis(durationMillis));
    data.setSuccess(success);
    return telemetry;
}
Also used : HashMap(java.util.HashMap) TelemetryItem(com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem) RemoteDependencyData(com.microsoft.applicationinsights.agent.internal.exporter.models.RemoteDependencyData)

Aggregations

TelemetryItem (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem)51 Test (org.junit.jupiter.api.Test)13 ArrayList (java.util.ArrayList)9 CompletableResultCode (io.opentelemetry.sdk.common.CompletableResultCode)8 MetricsData (com.microsoft.applicationinsights.agent.internal.exporter.models.MetricsData)7 TelemetryClient (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)7 HashMap (java.util.HashMap)7 MetricDataPoint (com.microsoft.applicationinsights.agent.internal.exporter.models.MetricDataPoint)6 MessageData (com.microsoft.applicationinsights.agent.internal.exporter.models.MessageData)5 TelemetryExceptionData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionData)4 RemoteDependencyData (com.microsoft.applicationinsights.agent.internal.exporter.models.RemoteDependencyData)3 RequestData (com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData)3 TelemetryEventData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)3 FinalCounters (com.microsoft.applicationinsights.agent.internal.quickpulse.QuickPulseDataCollector.FinalCounters)3 Attributes (io.opentelemetry.api.common.Attributes)3 SemanticAttributes (io.opentelemetry.semconv.trace.attributes.SemanticAttributes)3 MemoryUsage (java.lang.management.MemoryUsage)3 Date (java.util.Date)3 HttpRequest (com.azure.core.http.HttpRequest)1 SerializedString (com.fasterxml.jackson.core.io.SerializedString)1