/** * 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(); }
@Test public void testCtor() { TelemetryContext context = new TelemetryContext(); assertTrue(context.getProperties().isEmpty()); assertTrue(context.getTags().isEmpty()); assertNull(context.getInstrumentationKey()); }
@Test public void testInstrumentationKeyNotOverridenWhenContextInitialized() { TelemetryContext contextToInitialize = new TelemetryContext(); contextToInitialize.setInstrumentationKey(TEST_IKEY); TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey(TEST_IKEY.replaceFirst("0", "1")); contextToInitialize.initialize(context); Assert.assertEquals(TEST_IKEY, contextToInitialize.getInstrumentationKey()); } }
public void initialize(TelemetryContext source) { if (Strings.isNullOrEmpty(this.instrumentationKey) && !Strings.isNullOrEmpty(source.getInstrumentationKey())) setInstrumentationKey(source.getInstrumentationKey()); MapUtil.copy(source.tags, this.tags); MapUtil.copy(source.properties, this.properties); }
@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"); } }
@Test public void testSetInstrumentationKey() { TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey("key"); assertEquals("key", context.getInstrumentationKey()); }
private void initializeInstrumentationKey(final TelemetryContext context, final boolean isDeveloperMode) { if (isDeveloperMode) { context.setInstrumentationKey("149da81b-a0ab-4bdf-a7e9-11e5af9e39bd"); } else { context.setInstrumentationKey("0f243a28-b3c3-41f2-b7cc-d10feec45a81"); } }
@Override public String getInstrumentationKey() { return this.telemetryClient.getContext().getInstrumentationKey(); }
/** * Initializes the properties of the given telemetry. * * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override protected void onInitializeTelemetry(Telemetry telemetry) { RequestTelemetry requestTelemetry = ThreadContext.getRequestTelemetryContext().getHttpRequestTelemetry(); String userAgent = requestTelemetry.getContext().getUser().getUserAgent(); telemetry.getContext().getUser().setUserAgent(userAgent); } }
private TelemetryContext createInitializedContext() { TelemetryContext ctx = new TelemetryContext(); ctx.setInstrumentationKey(configuration.getInstrumentationKey()); for (ContextInitializer init : configuration.getContextInitializers()) { try { init.initialize(ctx); } catch (ThreadDeath td) { throw td; } catch (Throwable t) { try { InternalLogger.INSTANCE.error("Exception in context initializer: '%s'", t.toString()); } catch (ThreadDeath td) { throw td; } catch (Throwable t2) { // chomp } } } // Set the nodeName for billing purpose if it does not already exist InternalContext internal = ctx.getInternal(); if (CommonUtils.isNullOrEmpty(internal.getNodeName())) { String host = CommonUtils.getHostName(); if (!CommonUtils.isNullOrEmpty(host)) { internal.setNodeName(host); } } return ctx; } }
@Test public void gsmSyntheticHeadersTest() { WebSyntheticRequestTelemetryInitializer tested = new WebSyntheticRequestTelemetryInitializer(); HttpServletRequest mockRequest = Mockito.mock(HttpServletRequest.class); Mockito.doReturn("").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_SOURCE); Mockito.doReturn("A1").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_RUN_ID); Mockito.doReturn("A2").when(mockRequest).getHeader(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_TEST_LOCATION); RequestTelemetryContext context = new RequestTelemetryContext(DateTimeUtils.getDateTimeNow().getTime(), mockRequest); ThreadContext.setRequestTelemetryContext(context); TraceTelemetry telemetry = new TraceTelemetry(); tested.initialize(telemetry); assertEquals(WebSyntheticRequestTelemetryInitializer.SYNTHETIC_SOURCE_NAME, telemetry.getContext().getOperation().getSyntheticSource()); assertEquals("A1", telemetry.getContext().getSession().getId()); assertEquals("A2", telemetry.getContext().getUser().getId()); }
@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()); }
@Override public boolean process(Telemetry telemetry) { if (telemetry == null) { return true; } if (notNeededSources.isEmpty()) { return LocalStringsUtils.isNullOrEmpty(telemetry.getContext().getOperation().getSyntheticSource()); } if (notNeededSources.contains(telemetry.getContext().getOperation().getSyntheticSource())) { return false; } return true; }
/** * Initializes the instance with the context properties * * @param properties The context properties */ protected void initialize(ConcurrentMap<String, String> properties) { this.context = new TelemetryContext(properties, new ConcurrentHashMap<String, String>()); }
/** * Serializes this object in JSON format. * * @param writer The writer that helps with serializing into Json format * @throws IOException The exception that might be thrown during the serialization */ @Override public void serialize(JsonTelemetryDataSerializer writer) throws IOException { String telemetryName = this.getTelemetryName( this.normalizeInstrumentationKey(context.getInstrumentationKey()), this.getEnvelopName()); Envelope envelope = new Envelope(); envelope.setName(telemetryName); setSampleRate(envelope); envelope.setIKey(context.getInstrumentationKey()); envelope.setSeq(sequence); Data<T> tmp = new Data<T>(); tmp.setBaseData(getData()); tmp.setBaseType(this.getBaseTypeName()); envelope.setData(tmp); envelope.setTime(LocalStringsUtils.getDateFormatter().format(getTimestamp())); envelope.setTags(context.getTags()); envelope.serialize(writer); }
@Override public void initialize(TelemetryContext context) { if (!CommonUtils.isNullOrEmpty(appName)) { CloudContext cloud = context.getCloud(); cloud.setRole(appName); } } }
private void testUseConfigurationInstrumentatonKey(String contextInstrumentationKey) { TelemetryConfiguration configuration = new TelemetryConfiguration(); TelemetryChannel mockChannel = Mockito.mock(TelemetryChannel.class); configuration.setChannel(mockChannel); configuration.setInstrumentationKey("00000000-0000-0000-0000-000000000000"); TelemetryContext mockContext = new TelemetryContext(); mockContext.setInstrumentationKey(contextInstrumentationKey); Telemetry mockTelemetry = Mockito.mock(Telemetry.class); Mockito.doReturn(mockContext).when(mockTelemetry).getContext(); TelemetryClient telemetryClient = new TelemetryClient(configuration); telemetryClient.track(mockTelemetry); Mockito.verify(mockChannel, Mockito.times(1)).send(mockTelemetry); assertEquals("00000000-0000-0000-0000-000000000000", mockContext.getInstrumentationKey()); }
@Override public void setInstrumentationKey(String key) { if (this.telemetryClient != null && key != null) { this.telemetryClient.getContext().setInstrumentationKey(key); } }
public void add(Telemetry telemetry) { if (!telemetry.getContext().getInstrumentationKey().equals(ikey)) { return; } if (telemetry instanceof RequestTelemetry) { RequestTelemetry requestTelemetry = (RequestTelemetry)telemetry; addRequest(requestTelemetry); } else if (telemetry instanceof RemoteDependencyTelemetry) { addDependency((RemoteDependencyTelemetry) telemetry); } else if (telemetry instanceof ExceptionTelemetry) { addException(); } }
public static void main(String[] args) throws IOException { System.out.println("This program sends application insights telemetry event using custom context initializer."); TelemetryClient appInsights = new TelemetryClient(); if (args.length > 0) { appInsights.getContext().setInstrumentationKey(args[0]); } appInsights.getContext().setInstrumentationKey("your-ikey"); String iKey = appInsights.getContext().getInstrumentationKey(); if (iKey == null) { System.out.println("Error: no iKey set in ApplicationInsights.xml or as a parameter for this program."); return; } GitBuildInfoContextInitializer customContextInitializer = new GitBuildInfoContextInitializer(); TelemetryConfiguration.getActive().getContextInitializers().add(customContextInitializer); System.out.println("Custom context initializer added to configuration"); // Trace telemetry appInsights.trackTrace("Things seem to be going well"); System.out.println("[3] Trace -- text=\"Things seem to be going well\""); System.out.println(); System.out.println("Press any key to exit"); System.in.read(); }