use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData 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.exporter.models.TelemetryEventData in project ApplicationInsights-Java by microsoft.
the class BytecodeUtilImpl method trackEvent.
@Override
public void trackEvent(Date timestamp, String name, Map<String, String> properties, Map<String, String> tags, Map<String, Double> metrics, String instrumentationKey) {
if (Strings.isNullOrEmpty(name)) {
return;
}
TelemetryItem telemetry = new TelemetryItem();
TelemetryEventData data = new TelemetryEventData();
TelemetryClient.getActive().initEventTelemetry(telemetry, data);
data.setName(name);
data.setMeasurements(metrics);
// shared
if (!properties.isEmpty()) {
Map<String, String> existingProperties = data.getProperties();
if (existingProperties == null) {
data.setProperties(properties);
} else {
existingProperties.putAll(properties);
}
}
if (timestamp != null) {
telemetry.setTime(FormattedTime.offSetDateTimeFromDate(timestamp));
} else {
telemetry.setTime(FormattedTime.offSetDateTimeFromNow());
}
selectivelySetTags(telemetry, tags);
if (instrumentationKey != null) {
telemetry.setInstrumentationKey(instrumentationKey);
}
track(telemetry, true);
}
use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData in project ApplicationInsights-Java by microsoft.
the class TelemetryUtil method getProperties.
// TODO (trask) Azure SDK: can we move getProperties up to MonitorDomain, or if not, a common
// interface?
public static Map<String, String> getProperties(MonitorDomain data) {
if (data instanceof AvailabilityData) {
AvailabilityData availabilityData = (AvailabilityData) data;
Map<String, String> properties = availabilityData.getProperties();
if (properties == null) {
properties = new HashMap<>();
availabilityData.setProperties(properties);
}
return properties;
} else if (data instanceof MessageData) {
MessageData messageData = (MessageData) data;
Map<String, String> properties = messageData.getProperties();
if (properties == null) {
properties = new HashMap<>();
messageData.setProperties(properties);
}
return properties;
} else if (data instanceof MetricsData) {
MetricsData metricsData = (MetricsData) data;
Map<String, String> properties = metricsData.getProperties();
if (properties == null) {
properties = new HashMap<>();
metricsData.setProperties(properties);
}
return properties;
} else if (data instanceof PageViewData) {
PageViewData pageViewData = (PageViewData) data;
Map<String, String> properties = pageViewData.getProperties();
if (properties == null) {
properties = new HashMap<>();
pageViewData.setProperties(properties);
}
return properties;
} else if (data instanceof PageViewPerfData) {
PageViewPerfData pageViewPerfData = (PageViewPerfData) data;
Map<String, String> properties = pageViewPerfData.getProperties();
if (properties == null) {
properties = new HashMap<>();
pageViewPerfData.setProperties(properties);
}
return properties;
} else if (data instanceof RemoteDependencyData) {
RemoteDependencyData remoteDependencyData = (RemoteDependencyData) data;
Map<String, String> properties = remoteDependencyData.getProperties();
if (properties == null) {
properties = new HashMap<>();
remoteDependencyData.setProperties(properties);
}
return properties;
} else if (data instanceof RequestData) {
RequestData requestData = (RequestData) data;
Map<String, String> properties = requestData.getProperties();
if (properties == null) {
properties = new HashMap<>();
requestData.setProperties(properties);
}
return properties;
} else if (data instanceof TelemetryEventData) {
TelemetryEventData eventData = (TelemetryEventData) data;
Map<String, String> properties = eventData.getProperties();
if (properties == null) {
properties = new HashMap<>();
eventData.setProperties(properties);
}
return properties;
} else if (data instanceof TelemetryExceptionData) {
TelemetryExceptionData exceptionData = (TelemetryExceptionData) data;
Map<String, String> properties = exceptionData.getProperties();
if (properties == null) {
properties = new HashMap<>();
exceptionData.setProperties(properties);
}
return properties;
} else {
throw new IllegalArgumentException("Unexpected type: " + data.getClass().getName());
}
}
use of com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryEventData in project ApplicationInsights-Java by microsoft.
the class GcEventMonitor method emitGcEvent.
/**
* If gc reporting is enabled, send gc event to Application Insights.
*/
private static void emitGcEvent(TelemetryClient telemetryClient, GcEventMonitorConfiguration gcEventMonitorConfiguration, GcCollectionEvent event) {
boolean reportEvent = gcEventMonitorConfiguration.reportingLevel == GcReportingLevel.ALL;
reportEvent |= gcEventMonitorConfiguration.reportingLevel == GcReportingLevel.TENURED_ONLY && event.getCollector().isTenuredCollector();
if (!reportEvent) {
return;
}
TelemetryItem telemetry = new TelemetryItem();
TelemetryEventData data = new TelemetryEventData();
TelemetryClient.getActive().initEventTelemetry(telemetry, data);
data.setName("GcEvent");
Map<String, String> properties = new HashMap<>();
properties.put("collector", event.getCollector().getName());
properties.put("type", event.getGcCause());
properties.put("action", event.getGcAction());
properties.put("jvm_instance_id", JVM_INSTANCE_UID);
data.setProperties(properties);
Map<String, Double> measurements = new HashMap<>();
measurements.put("id", (double) event.getId());
measurements.put("duration_ms", (double) event.getDuration());
measurements.put("end_time_ms", (double) event.getEndTime());
measurements.put("thread_count", (double) event.getGcThreadCount());
measurements.put("collection_count", (double) event.getCollector().getCollectionCount());
measurements.put("cumulative_collector_time_sec", (double) event.getCollector().getCollectionTime());
addMemoryUsage("young", "before", measurements, event.getMemoryUsageBeforeGc(event.getYoungPools()));
addMemoryUsage("young", "after", measurements, event.getMemoryUsageAfterGc(event.getYoungPools()));
Optional<MemoryPool> tenuredPool = event.getTenuredPool();
if (tenuredPool.isPresent()) {
MemoryUsage beforeOg = event.getMemoryUsageBeforeGc(tenuredPool.get());
addMemoryUsage("tenured", "before", measurements, beforeOg);
MemoryUsage afterOg = event.getMemoryUsageAfterGc(tenuredPool.get());
addMemoryUsage("tenured", "after", measurements, afterOg);
}
data.setMeasurements(measurements);
telemetry.setTime(FormattedTime.offSetDateTimeFromNow());
telemetryClient.trackAsync(telemetry);
}
Aggregations