@Override public String getInstrumentationKey() { return this.telemetryClient.getContext().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); }
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(); }
private void testIkeySystemProperty(String propertyName) { System.setProperty(propertyName, "00000000-0000-0000-0000-000000000001"); context.register(PropertyPlaceholderAutoConfiguration.class, ApplicationInsightsTelemetryAutoConfiguration.class); context.refresh(); TelemetryClient telemetryClient = context.getBean(TelemetryClient.class); TelemetryConfiguration telemetryConfiguration = context.getBean(TelemetryConfiguration.class); assertThat(telemetryConfiguration).isSameAs(TelemetryConfiguration.getActive()); assertThat(telemetryConfiguration.getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000001"); assertThat(telemetryClient.getContext().getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000001"); System.clearProperty(propertyName); }
/** * Checks whether tracking is enabled. * @return 'true' if tracking is disabled, 'false' otherwise. */ public boolean isDisabled() { return (Strings.isNullOrEmpty(configuration.getInstrumentationKey()) && Strings.isNullOrEmpty(getContext().getInstrumentationKey())) || configuration.isTrackingDisabled(); }
@Test public void shouldSetInstrumentationKeyWhenContextLoads() { EnvironmentTestUtils.addEnvironment(context, "azure.application-insights.instrumentation-key:00000000-0000-0000-0000-000000000000"); context.register(PropertyPlaceholderAutoConfiguration.class, ApplicationInsightsTelemetryAutoConfiguration.class); context.refresh(); TelemetryClient telemetryClient = context.getBean(TelemetryClient.class); TelemetryConfiguration telemetryConfiguration = context.getBean(TelemetryConfiguration.class); assertThat(telemetryConfiguration).isSameAs(TelemetryConfiguration.getActive()); assertThat(telemetryConfiguration.getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000000"); assertThat(telemetryClient.getContext().getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000000"); }
@Test @Ignore("Boot 2 causes backport issues if depending on RelaxedPropertyBinder") public void shouldSetInstrumentationKeyFromRelaxedCase() { EnvironmentTestUtils.addEnvironment(context, "AZURE.APPLICATION_INSIGHTS.INSTRUMENTATION_KEY: 00000000-0000-0000-0000-000000000000"); context.register(PropertyPlaceholderAutoConfiguration.class, ApplicationInsightsTelemetryAutoConfiguration.class); context.refresh(); TelemetryClient telemetryClient = context.getBean(TelemetryClient.class); TelemetryConfiguration telemetryConfiguration = context.getBean(TelemetryConfiguration.class); assertThat(telemetryConfiguration).isSameAs(TelemetryConfiguration.getActive()); assertThat(telemetryConfiguration.getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000000"); assertThat(telemetryClient.getContext().getInstrumentationKey()).isEqualTo("00000000-0000-0000-0000-000000000000"); }
String instrumentationKey = this.telemetryClient.getContext().getInstrumentationKey(); if (isW3CEnabled) { TraceContextCorrelation.resolveRequestSource((HttpServletRequest) req, telemetry, instrumentationKey);
String instrumentationKey = this.telemetryClient.getContext().getInstrumentationKey(); if (isW3CEnabled) { TraceContextCorrelation.resolveRequestSource((HttpServletRequest) req, telemetry, instrumentationKey);
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()); }
@Test public void testCtor() { TelemetryContext context = new TelemetryContext(); assertTrue(context.getProperties().isEmpty()); assertTrue(context.getTags().isEmpty()); assertNull(context.getInstrumentationKey()); }
@Test public void testSetInstrumentationKey() { TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey("key"); assertEquals("key", 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()); } }
/** * 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); }
@Test public void testInstrumentationKeyIsLoadedFromConfiguration() { ApplicationInsightsAppender appender = getApplicationInsightsAppender(); String configurationKey = appender.getTelemetryClientProxy().getTelemetryClient().getContext().getInstrumentationKey(); Assert.assertEquals(TestInstrumentationKey, configurationKey); }
@Test public void testInstrumentationKeyIsLoadedFromConfiguration() { ApplicationInsightsAppender appender = getApplicationInsightsAppender(); String configurationKey = appender.getTelemetryClientProxy().getTelemetryClient().getContext().getInstrumentationKey(); Assert.assertEquals(TestInstrumentationKey, configurationKey); }
@Test public void testInstrumentationKeyIsLoadedFromConfiguration() { ApplicationInsightsAppender appender = getApplicationInsightsAppender(); String configurationKey = appender.getTelemetryClientProxy().getTelemetryClient().getContext().getInstrumentationKey(); Assert.assertEquals(TestInstrumentationKey, configurationKey); }
@Test public void testEmptyInstrumentationKeyOverridenWhenContextInitialized() { TelemetryContext contextToInitialize = new TelemetryContext(); TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey(TEST_IKEY); contextToInitialize.initialize(context); Assert.assertEquals(TEST_IKEY, contextToInitialize.getInstrumentationKey()); }
@VisibleForTesting static <T extends BaseTelemetry> Telemetry.Builder telemetryBuilderWithStandardFields(T telemetry) { Preconditions.checkArgument(telemetry.getContext() != null, "TelemetryContext is null for telemetry with "+telemetry.getBaseTypeName()); TelemetryContext context = telemetry.getContext(); final String iKey = context.getInstrumentationKey(); Preconditions.checkArgument(iKey != null, "The TelemetryContext.InstrumentationKey is null inside "+telemetry.getBaseTypeName()); final Builder tb = Telemetry.newBuilder(); if (telemetry.getTimestamp() != null) tb.setDateTime(LocalStringsUtils.getDateFormatter().format(telemetry.getTimestamp())); if (telemetry.getSequence() != null) tb.setSequenceNumber(telemetry.getSequence()); if (telemetry instanceof BaseSampleSourceTelemetry) { final BaseSampleSourceTelemetry bsst = (BaseSampleSourceTelemetry) telemetry; if (bsst.getSamplingPercentage() != null) tb.setSamplingRate(DoubleValue.of(bsst.getSamplingPercentage())); } tb.setInstrumentationKey(iKey); if (context.getTags() != null) tb.putAllTags(context.getTags()); tb.setVer(CURRENT_ENVELOPE_VERSION); final String envelopName; if (telemetry instanceof PerformanceCounterTelemetry) { envelopName = MetricTelemetry.ENVELOPE_NAME; } else { envelopName = telemetry.getEnvelopName(); } tb.setDataTypeName(generateDataTypeName(iKey, envelopName)); return tb; }