/** * Gets a dictionary of application-defined property names and values providing additional information about this event. * * @return The properties */ @Override public Map<String, String> getProperties() { return this.context.getProperties(); }
@Override public void initialize(TelemetryContext context) { if (!hasBuildData) { return; } context.getProperties().put(GIT_BRANCH_KEY, gitBranch); context.getProperties().put(GIT_COMMIT_KEY, gitCommit); context.getProperties().put(GIT_REPO_KEY, gitRepo); context.getProperties().put(GIT_COMMIT_URL_KEY, gitCommitUrl); } }
private void putInProperties(Telemetry telemetry, HttpServletRequest request, String... headers) { ConcurrentMap<String, String> properties = telemetry.getContext().getProperties(); for (String header : headers) { String headerValue = request.getHeader(header); if (headerValue != null) { properties.put(header, headerValue); } } } }
private void putInProperties(Telemetry telemetry, HttpServletRequest request, String... headers) { ConcurrentMap<String, String> properties = telemetry.getContext().getProperties(); for (String header : headers) { String headerValue = request.getHeader(header); if (headerValue != null) { properties.put(header, headerValue); } } } }
private static void verify(TelemetryContext context, String key, String expectedValue) { String value = context.getProperties().get(key); assertEquals(expectedValue, value); } }
public Tester(int numberOfContextProperties) { TelemetryConfiguration configuration = new TelemetryConfiguration(); configuration.setInstrumentationKey(TEST_IKEY); configuration.setChannel(new InProcessTelemetryChannel()); fakeTransmissionOutput = TestThreadLocalData.getTransmissionOutput(); testResultsVerifier = fakeTransmissionOutput.getTestResultsVerifier(); telemetryClient = new TelemetryClient(configuration); TelemetryContext context = telemetryClient.getContext(); for (int i = 0; i < numberOfContextProperties; ++i) { String asStr = String.valueOf(i); context.getProperties().put(MOCK_CONTEXT_PROPERTY_BASE_NAME + asStr, MOCK_CONTEXT_PROPERTY_BASE_VALUE + asStr); } }
/** * This method processes the legacy Request-ID header for backward compatibility. * @param request * @return */ private static Traceparent processLegacyCorrelation(HttpServletRequest request) { String requestId = request.getHeader(TelemetryCorrelationUtils.CORRELATION_HEADER_NAME); try { if (requestId != null && !requestId.isEmpty()) { String legacyOperationId = TelemetryCorrelationUtils.extractRootId(requestId); RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); requestTelemetry.getContext().getProperties().putIfAbsent("ai_legacyRootID", legacyOperationId); requestTelemetry.getContext().getOperation().setParentId(requestId); return new Traceparent(0, legacyOperationId, null, 0); } } catch (Exception e) { InternalLogger.INSTANCE.error(String.format("unable to create traceparent from legacy request-id header" + " %s", ExceptionUtils.getStackTrace(e))); } return null; }
@Test public void testCustomParametersAddedByTelemetryClientProxy() { Telemetry telemetry = sendAIEventAndGetOutputTelemetry(false); // TODO: should custom parameters validated one-by-one for values? Map<String, String> customParameters = telemetry.getContext().getProperties(); Assert.assertTrue("Custom parameters list shouldn't be empty.", customParameters.size() > 0); }
/** * Sends a TraceTelemetry record to Application Insights. Appears in "traces" in Analytics and Search. * @param message A log message. Max length 10000. * @param severityLevel The severity level. * @param properties Named string values you can use to search and classify trace messages. */ public void trackTrace(String message, SeverityLevel severityLevel, Map<String, String> properties) { if (isDisabled()) { return; } if (Strings.isNullOrEmpty(message)) { message = ""; } TraceTelemetry et = new TraceTelemetry(message, severityLevel); MapUtil.copy(properties, et.getContext().getProperties()); this.track(et); }
@Override public void methodFinished(String classAndMethodNames, long deltaInNS, Object[] args, Throwable throwable) { long durationInMS = nanoToMilliseconds(deltaInNS); Duration duration = new Duration(durationInMS); Date dependencyStartTime = new Date(System.currentTimeMillis() - durationInMS); RemoteDependencyTelemetry telemetry = new RemoteDependencyTelemetry(classAndMethodNames, null, duration, throwable == null); telemetry.setTimestamp(dependencyStartTime); telemetry.setDependencyKind(DependencyKind.Other); if (args != null) { String argsAsString = new ArgsFormatter().format(args); telemetry.getContext().getProperties().put("Args", argsAsString); } InternalLogger.INSTANCE.trace("Sending RDD event for '%s', duration=%s ms", classAndMethodNames, durationInMS); telemetryClient.track(telemetry); if (throwable != null) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(throwable); telemetryClient.track(exceptionTelemetry); } }
@Test public void testCustomParametersAddedByTelemetryClientProxy() { Telemetry telemetry = sendAIEventAndGetOutputTelemetry(false); // TODO: should custom parameters validated one-by-one for values? Map<String, String> customParameters = telemetry.getContext().getProperties(); Assert.assertTrue("Custom parameters list shouldn't be empty.", customParameters.size() > 0); }
@Test public void testCustomParametersAddedByTelemetryClientProxy() { Telemetry telemetry = sendAIEventAndGetOutputTelemetry(false); // TODO: should custom parameters validated one-by-one for values? Map<String, String> customParameters = telemetry.getContext().getProperties(); Assert.assertTrue("Custom parameters list shouldn't be empty.", customParameters.size() > 0); }
@Override public void initialize(final TelemetryContext context) { if (!isInitialized) { logger.info("Starting TelemetryContext initialization"); initializeInstrumentationKey(context, isDeveloperMode); initializeProperties(context.getProperties()); initializeUser(context.getUser()); initializeComponent(context.getComponent()); initializeDevice(context.getDevice()); initializeTags(context.getTags()); initializeSession(context.getSession()); isInitialized = true; logger.info("Ending TelemetryContext initialization"); } }
/** * Sends an exception record to Application Insights. Appears in "exceptions" in Analytics and Search. * @param exception The exception to log information about. * @param properties Named string values you can use to search and classify trace messages. * @param metrics Measurements associated with this exception event. Appear in "custom metrics" in Metrics Explorer. */ public void trackException(Exception exception, Map<String, String> properties, Map<String, Double> metrics) { if (isDisabled()) { return; } ExceptionTelemetry et = new ExceptionTelemetry(exception); MapUtil.copy(properties, et.getContext().getProperties()); MapUtil.copy(metrics, et.getMetrics()); this.track(et); }
@Test public void testCtor() { TelemetryContext context = new TelemetryContext(); assertTrue(context.getProperties().isEmpty()); assertTrue(context.getTags().isEmpty()); assertNull(context.getInstrumentationKey()); }
/** * Sends a custom event record to Application Insights. Appears in custom events in Analytics, Search and Metrics Explorer. * @param name A name for the event. Max length 150. * @param properties Named string values you can use to search and filter events. * @param metrics Numeric measurements associated with this event. Appear under Custom Metrics in Metrics Explorer. */ public void trackEvent(String name, Map<String, String> properties, Map<String, Double> metrics) { if (isDisabled()) { return; } if (Strings.isNullOrEmpty(name)) { name = ""; } EventTelemetry et = new EventTelemetry(name); MapUtil.copy(properties, et.getContext().getProperties()); MapUtil.copy(metrics, et.getMetrics()); this.track(et); }
/** * Sends the given event to AI. * * @param event The event to send. */ public void sendEvent(ApplicationInsightsEvent event) { String formattedMessage = event.getMessage(); Map<String, String> customParameters = event.getCustomParameters(); Telemetry telemetry; if (event.isException()) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException()); exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = exceptionTelemetry; } else { TraceTelemetry traceTelemetry = new TraceTelemetry(formattedMessage); traceTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = traceTelemetry; } telemetry.getContext().getProperties().putAll(customParameters); telemetryClient.track(telemetry); }
/** * Sends the given event to AI. * * @param event The event to send. */ public void sendEvent(ApplicationInsightsEvent event) { String formattedMessage = event.getMessage(); Map<String, String> customParameters = event.getCustomParameters(); Telemetry telemetry; if (event.isException()) { ExceptionTelemetry exceptionTelemetry = new ExceptionTelemetry(event.getException()); exceptionTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = exceptionTelemetry; } else { TraceTelemetry traceTelemetry = new TraceTelemetry(formattedMessage); traceTelemetry.setSeverityLevel(event.getNormalizedSeverityLevel()); telemetry = traceTelemetry; } telemetry.getContext().getProperties().putAll(customParameters); telemetryClient.track(telemetry); }
private static void verifyTraceTelemetry(Telemetry telemetry, SeverityLevel expectedSeverityLevel, Map<String, String> expectedProperties) { assertNotNull(telemetry); assertTrue(telemetry instanceof TraceTelemetry); TraceTelemetry traceTelemetry = (TraceTelemetry)telemetry; assertEquals(traceTelemetry.getSeverityLevel(), expectedSeverityLevel); if (expectedProperties != null) { assertEquals(traceTelemetry.getContext().getProperties(), expectedProperties); } }
@Test public void testCtorWithInitialize() { StubTelemetry telemetry = new StubTelemetry("1"); assertNotNull(telemetry.getContext()); assertTrue(telemetry.getContext().getProperties().isEmpty()); assertTrue(telemetry.getContext().getTags().isEmpty()); assertTrue(telemetry.getProperties().isEmpty()); assertNull(telemetry.getTimestamp()); assertNull(telemetry.getSequence()); }