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