Search in sources :

Example 1 with ServiceProfilerClientV2

use of com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2 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 ServiceProfilerClientV2

use of com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2 in project ApplicationInsights-Java by microsoft.

the class ServiceProfilerConfigMonitorServiceTest method pullsConfig.

@Test
void pullsConfig() throws IOException, URISyntaxException {
    AtomicReference<Runnable> job = new AtomicReference<>();
    ScheduledExecutorService executorService = mockScheduledExecutorService(job::set);
    ServiceProfilerClientV2 serviceProfilerClient = mockServiceProfilerClient();
    ServiceProfilerConfigMonitorService serviceMonitor = new ServiceProfilerConfigMonitorService(executorService, 100, serviceProfilerClient);
    AtomicReference<ProfilerConfiguration> config = new AtomicReference<>();
    serviceMonitor.initialize(Collections.singletonList(config::set));
    assertThat(job.get()).isNotNull();
    job.get().run();
    Mockito.verify(serviceProfilerClient, times(1)).getSettings(any(Date.class));
    assertThat(config.get()).isNotNull();
    assertThat(config.get().getCollectionPlan()).isEqualTo("--single --mode immediate --immediate-profiling-duration 120  --expiration 5249157885138288517 --settings-moniker a-settings-moniker");
    assertThat(config.get().getCpuTriggerConfiguration()).isEqualTo("--cpu-trigger-enabled true --cpu-threshold 80 --cpu-trigger-profilingDuration 30 --cpu-trigger-cooldown 14400");
    assertThat(config.get().getDefaultConfiguration()).isEqualTo("--sampling-enabled true --sampling-rate 5 --sampling-profiling-duration 120");
    assertThat(config.get().getMemoryTriggerConfiguration()).isEqualTo("--memory-trigger-enabled true --memory-threshold 20 --memory-trigger-profilingDuration 120 --memory-trigger-cooldown 14400");
}
Also used : ServiceProfilerConfigMonitorService(com.microsoft.applicationinsights.serviceprofilerapi.config.ServiceProfilerConfigMonitorService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ServiceProfilerClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2) ProfilerConfiguration(com.microsoft.applicationinsights.profiler.ProfilerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with ServiceProfilerClientV2

use of com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2 in project ApplicationInsights-Java by microsoft.

the class ServiceProfilerConfigMonitorServiceTest method mockServiceProfilerClient.

static ServiceProfilerClientV2 mockServiceProfilerClient() throws IOException, URISyntaxException {
    ServiceProfilerClientV2 serviceProfilerClient = Mockito.mock(ServiceProfilerClientV2.class);
    when(serviceProfilerClient.getSettings(any(Date.class))).thenReturn(Mono.just("{\"id\":\"8929ed2e-24da-4ad4-8a8b-5a5ebc03abb4\",\"lastModified\":\"2021-01-25T15:46:11" + ".0900613+00:00\",\"enabledLastModified\":\"0001-01-01T00:00:00+00:00\",\"enabled\":true,\"collectionPlan\":\"--single --mode immediate --immediate-profiling-duration 120  " + "--expiration 5249157885138288517 --settings-moniker a-settings-moniker\",\"cpuTriggerConfiguration\":\"--cpu-trigger-enabled true --cpu-threshold 80 " + "--cpu-trigger-profilingDuration 30 --cpu-trigger-cooldown 14400\",\"memoryTriggerConfiguration\":\"--memory-trigger-enabled true --memory-threshold 20 " + "--memory-trigger-profilingDuration 120 --memory-trigger-cooldown 14400\",\"defaultConfiguration\":\"--sampling-enabled true --sampling-rate 5 --sampling-profiling-duration 120\"," + "\"geoOverride\":null}"));
    return serviceProfilerClient;
}
Also used : ServiceProfilerClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2) Date(java.util.Date)

Example 4 with ServiceProfilerClientV2

use of com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2 in project ApplicationInsights-Java by microsoft.

the class ServiceProfilerSettingsClientTest method badServiceResponseDoesNotProvideReturn.

@Test
void badServiceResponseDoesNotProvideReturn() throws IOException, URISyntaxException {
    ServiceProfilerClientV2 serviceProfilerClient = Mockito.mock(ServiceProfilerClientV2.class);
    Mockito.when(serviceProfilerClient.getSettings(Mockito.any())).thenReturn(Mono.just(""));
    ServiceProfilerSettingsClient settingsClient = new ServiceProfilerSettingsClient(serviceProfilerClient);
    Mono<ProfilerConfiguration> result = settingsClient.pullSettings();
    assertThatThrownBy(result::block).isInstanceOf(Exception.class);
}
Also used : ServiceProfilerClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2) ProfilerConfiguration(com.microsoft.applicationinsights.profiler.ProfilerConfiguration) ServiceProfilerSettingsClient(com.microsoft.applicationinsights.serviceprofilerapi.config.ServiceProfilerSettingsClient) Test(org.junit.jupiter.api.Test)

Example 5 with ServiceProfilerClientV2

use of com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2 in project ApplicationInsights-Java by microsoft.

the class ServiceProfilerUploaderTest method uploadFileGoodPathReturnsExpectedResponse.

@Test
void uploadFileGoodPathReturnsExpectedResponse() throws IOException {
    ServiceProfilerClientV2 serviceProfilerClient = stubServiceProfilerClient();
    File tmpFile = createFakeJfrFile();
    UUID appId = UUID.randomUUID();
    ServiceProfilerUploader serviceProfilerUploader = new ServiceProfilerUploader(serviceProfilerClient, "a-machine-name", "a-process-id", appId::toString, "a-role-name") {

        @Override
        protected Mono<UploadFinishArgs> performUpload(UploadContext uploadContext, BlobAccessPass uploadPass, File file) {
            return Mono.just(new UploadFinishArgs("a-stamp-id", "a-timestamp"));
        }
    };
    serviceProfilerUploader.uploadJfrFile("a-trigger", 321, tmpFile, 0.0, 0.0).subscribe(result -> {
        assertThat(result.getServiceProfilerIndex().getProperties().get(ServiceProfilerIndex.SERVICE_PROFILER_STAMPID_PROPERTY_NAME)).isEqualTo("a-stamp-id");
        assertThat(result.getServiceProfilerIndex().getProperties().get(ServiceProfilerIndex.SERVICE_PROFILER_MACHINENAME_PROPERTY_NAME)).isEqualTo("a-machine-name");
        assertThat(result.getServiceProfilerIndex().getProperties().get(ServiceProfilerIndex.SERVICE_PROFILER_ETLFILESESSIONID_PROPERTY_NAME)).isEqualTo("a-timestamp");
        assertThat(result.getServiceProfilerIndex().getProperties().get(ServiceProfilerIndex.SERVICE_PROFILER_DATACUBE_PROPERTY_NAME)).isEqualTo(appId.toString());
    });
}
Also used : ServiceProfilerClientV2(com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2) BlobAccessPass(com.microsoft.applicationinsights.serviceprofilerapi.client.contract.BlobAccessPass) UUID(java.util.UUID) File(java.io.File) UploadContext(com.microsoft.applicationinsights.serviceprofilerapi.client.uploader.UploadContext) UploadFinishArgs(com.microsoft.applicationinsights.serviceprofilerapi.client.uploader.UploadFinishArgs) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceProfilerClientV2 (com.microsoft.applicationinsights.serviceprofilerapi.client.ServiceProfilerClientV2)8 Test (org.junit.jupiter.api.Test)5 File (java.io.File)3 UUID (java.util.UUID)3 ProfilerConfiguration (com.microsoft.applicationinsights.profiler.ProfilerConfiguration)2 UploadContext (com.microsoft.applicationinsights.serviceprofilerapi.client.uploader.UploadContext)2 JfrProfiler (com.microsoft.applicationinsights.serviceprofilerapi.profiler.JfrProfiler)2 ServiceProfilerUploader (com.microsoft.applicationinsights.serviceprofilerapi.upload.ServiceProfilerUploader)2 Date (java.util.Date)2 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 BlobUploadFromFileOptions (com.azure.storage.blob.options.BlobUploadFromFileOptions)1 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 AlertingSubsystem (com.microsoft.applicationinsights.alerting.AlertingSubsystem)1 ProfilerService (com.microsoft.applicationinsights.profiler.ProfilerService)1 ProfilerServiceFactory (com.microsoft.applicationinsights.profiler.ProfilerServiceFactory)1