Search in sources :

Example 1 with AlertingSubsystem

use of com.microsoft.applicationinsights.alerting.AlertingSubsystem 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 AlertingSubsystem

use of com.microsoft.applicationinsights.alerting.AlertingSubsystem in project ApplicationInsights-Java by microsoft.

the class AlertingServiceFactory method create.

public static AlertingSubsystem create(Consumer<AlertBreach> alertAction, TelemetryObservers telemetryObservers, TelemetryClient telemetryClient, ExecutorService executorService, GcEventMonitor.GcEventMonitorConfiguration gcEventMonitorConfiguration) {
    AlertingSubsystem alertingSubsystem = AlertingSubsystem.create(alertAction, executorService);
    addObserver(alertingSubsystem, telemetryObservers);
    monitorGcActivity(alertingSubsystem, telemetryClient, executorService, gcEventMonitorConfiguration);
    return alertingSubsystem;
}
Also used : AlertingSubsystem(com.microsoft.applicationinsights.alerting.AlertingSubsystem)

Example 3 with AlertingSubsystem

use of com.microsoft.applicationinsights.alerting.AlertingSubsystem in project ApplicationInsights-Java by microsoft.

the class ProfilerServiceInitializer method initialize.

public static synchronized void initialize(Supplier<String> appIdSupplier, String processId, ServiceProfilerServiceConfig config, String machineName, String roleName, TelemetryClient telemetryClient, String userAgent, GcEventMonitor.GcEventMonitorConfiguration gcEventMonitorConfiguration, HttpPipeline httpPipeline) {
    if (!initialized) {
        initialized = true;
        ProfilerServiceFactory factory = null;
        try {
            factory = loadProfilerServiceFactory();
        } catch (RuntimeException e) {
            LOGGER.error("Failed to load profiler factory", e);
        }
        if (factory == null) {
            LOGGER.error("Profiling has been enabled however no profiler implementation was provided. Please install an ApplicationInsights agent which provides a profiler.");
            return;
        }
        ScheduledExecutorService serviceProfilerExecutorService = Executors.newScheduledThreadPool(2, ThreadPoolUtils.createDaemonThreadFactory(ProfilerServiceFactory.class, "ServiceProfilerService"));
        ScheduledExecutorService alertServiceExecutorService = Executors.newScheduledThreadPool(2, ThreadPoolUtils.createDaemonThreadFactory(ProfilerServiceFactory.class, "ServiceProfilerAlertingService"));
        AlertingSubsystem alerting = createAlertMonitor(alertServiceExecutorService, telemetryClient, gcEventMonitorConfiguration);
        Future<ProfilerService> future = factory.initialize(appIdSupplier, sendServiceProfilerIndex(telemetryClient), updateAlertingConfig(alerting), processId, config, machineName, telemetryClient.getInstrumentationKey(), httpPipeline, serviceProfilerExecutorService, userAgent, roleName);
        serviceProfilerExecutorService.submit(() -> {
            try {
                profilerService = future.get();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                LOGGER.error("Unable to obtain JFR connection, this may indicate that your JVM does not have JFR enabled. JFR profiling system will shutdown", e);
                alertServiceExecutorService.shutdown();
                serviceProfilerExecutorService.shutdown();
            }
        });
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ProfilerServiceFactory(com.microsoft.applicationinsights.profiler.ProfilerServiceFactory) ProfilerService(com.microsoft.applicationinsights.profiler.ProfilerService) AlertingSubsystem(com.microsoft.applicationinsights.alerting.AlertingSubsystem)

Example 4 with AlertingSubsystem

use of com.microsoft.applicationinsights.alerting.AlertingSubsystem in project ApplicationInsights-Java by microsoft.

the class GcEventMonitorTest method getAlertingSubsystem.

private static AlertingSubsystem getAlertingSubsystem(CompletableFuture<AlertBreach> alertFuture) {
    AlertingSubsystem alertingSubsystem = AlertingSubsystem.create(alertFuture::complete, Executors.newSingleThreadExecutor());
    AlertingConfiguration config = AlertConfigParser.parse("--cpu-trigger-enabled true --cpu-threshold 80 --cpu-trigger-profilingDuration 30 --cpu-trigger-cooldown 14400", "--memory-trigger-enabled true --memory-threshold 20 --memory-trigger-profilingDuration 120 --memory-trigger-cooldown 14400", "--sampling-enabled true --sampling-rate 5 --sampling-profiling-duration 120", "--single --mode immediate --immediate-profiling-duration 120  --expiration 5249157885138288517 --settings-moniker a-settings-moniker");
    alertingSubsystem.initialize(config);
    return alertingSubsystem;
}
Also used : AlertingSubsystem(com.microsoft.applicationinsights.alerting.AlertingSubsystem) AlertingConfiguration(com.microsoft.applicationinsights.alerting.config.AlertingConfiguration)

Example 5 with AlertingSubsystem

use of com.microsoft.applicationinsights.alerting.AlertingSubsystem in project ApplicationInsights-Java by microsoft.

the class GcEventMonitorTest method endToEndAlertIsTriggered.

@Test
void endToEndAlertIsTriggered() throws ExecutionException, InterruptedException, TimeoutException {
    CompletableFuture<AlertBreach> alertFuture = new CompletableFuture<>();
    AlertingSubsystem alertingSubsystem = getAlertingSubsystem(alertFuture);
    GcMonitorFactory factory = new GcMonitorFactory() {

        @Override
        public MemoryManagement monitorSelf(ExecutorService executorService, GcEventConsumer consumer) {
            consumer.accept(mockGcEvent());
            return null;
        }

        @Override
        public MemoryManagement monitor(MBeanServerConnection connection, ExecutorService executorService, GcEventConsumer consumer) {
            return null;
        }
    };
    GcEventMonitor.init(alertingSubsystem, TelemetryClient.createForTest(), Executors.newSingleThreadExecutor(), new GcEventMonitor.GcEventMonitorConfiguration(GcReportingLevel.NONE), factory);
    AlertBreach alert = alertFuture.get(10, TimeUnit.SECONDS);
    assertThat(alert.getAlertValue()).isEqualTo(90.0);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) GcEventConsumer(com.microsoft.gcmonitor.GcEventConsumer) AlertBreach(com.microsoft.applicationinsights.alerting.alert.AlertBreach) ExecutorService(java.util.concurrent.ExecutorService) AlertingSubsystem(com.microsoft.applicationinsights.alerting.AlertingSubsystem) MBeanServerConnection(javax.management.MBeanServerConnection) GcMonitorFactory(com.microsoft.gcmonitor.GcMonitorFactory) Test(org.junit.jupiter.api.Test)

Aggregations

AlertingSubsystem (com.microsoft.applicationinsights.alerting.AlertingSubsystem)5 ProfilerService (com.microsoft.applicationinsights.profiler.ProfilerService)2 ProfilerServiceFactory (com.microsoft.applicationinsights.profiler.ProfilerServiceFactory)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 MonitorDomain (com.microsoft.applicationinsights.agent.internal.exporter.models.MonitorDomain)1 TelemetryEventData (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData)1 TelemetryItem (com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem)1 TelemetryClient (com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient)1 AlertBreach (com.microsoft.applicationinsights.alerting.alert.AlertBreach)1 AlertingConfiguration (com.microsoft.applicationinsights.alerting.config.AlertingConfiguration)1 ServiceProfilerServiceConfig (com.microsoft.applicationinsights.profiler.config.ServiceProfilerServiceConfig)1 JfrProfilerService (com.microsoft.applicationinsights.serviceprofilerapi.JfrProfilerService)1 ServiceProfilerClientV2 (com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2)1 JfrProfiler (com.microsoft.applicationinsights.serviceprofilerapi.profiler.JfrProfiler)1 ServiceProfilerUploader (com.microsoft.applicationinsights.serviceprofilerapi.upload.ServiceProfilerUploader)1 GcEventConsumer (com.microsoft.gcmonitor.GcEventConsumer)1 GcMonitorFactory (com.microsoft.gcmonitor.GcMonitorFactory)1 URL (java.net.URL)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ExecutorService (java.util.concurrent.ExecutorService)1