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 void setInstrumentationKey(String key) { if (this.telemetryClient != null && key != null) { this.telemetryClient.getContext().setInstrumentationKey(key); } }
private static TelemetryClient buildTelemetryClient(String instrumentationKey) { TelemetryClient client = new TelemetryClient(); if (!isValid(instrumentationKey)) { log.warn("Telemetry instrumentationKey {} is invalid", instrumentationKey); throw new IllegalArgumentException("Telemetry instrumentationKey is invalid"); } client.getContext().setInstrumentationKey(instrumentationKey); return client; }
private static TelemetryClient buildTelemetryClient(String instrumentationKey) { TelemetryClient client = new TelemetryClient(); if (!isValid(instrumentationKey)) { log.warn("Telemetry instrumentationKey {} is invalid", instrumentationKey); throw new IllegalArgumentException("Telemetry instrumentationKey is invalid"); } client.getContext().setInstrumentationKey(instrumentationKey); return client; }
/** * Constructs new telemetry client proxy instance with the given client. * @param telemetryClient The telemetry client. * @param instrumentationKey The instrumentation key. */ public LogTelemetryClientProxy(TelemetryClient telemetryClient, String instrumentationKey) { try { this.telemetryClient = telemetryClient; if (!LocalStringsUtils.isNullOrEmpty(instrumentationKey)) { this.telemetryClient.getContext().setInstrumentationKey(instrumentationKey); } this.isInitialized = true; } catch (Exception e) { // Catching all exceptions so in case of a failure the calling appender won't throw exception. // TODO: Assert.Debug/warning on exception? } }
/** * Constructs new telemetry client proxy instance with the given client. * @param telemetryClient The telemetry client. * @param instrumentationKey The instrumentation key. */ public LogTelemetryClientProxy(TelemetryClient telemetryClient, String instrumentationKey) { try { this.telemetryClient = telemetryClient; if (!LocalStringsUtils.isNullOrEmpty(instrumentationKey)) { this.telemetryClient.getContext().setInstrumentationKey(instrumentationKey); } this.isInitialized = true; } catch (Exception e) { // Catching all exceptions so in case of a failure the calling appender won't throw exception. // TODO: Assert.Debug/warning on exception? } }
/** * Constructs new telemetry client proxy instance with the given client. * @param telemetryClient The telemetry client. * @param instrumentationKey The instrumentation key. */ public LogTelemetryClientProxy(TelemetryClient telemetryClient, String instrumentationKey) { try { this.telemetryClient = telemetryClient; if (!LocalStringsUtils.isNullOrEmpty(instrumentationKey)) { this.telemetryClient.getContext().setInstrumentationKey(instrumentationKey); } this.isInitialized = true; } catch (Exception e) { // Catching all exceptions so in case of a failure the calling appender won't throw exception. // TODO: Assert.Debug/warning on exception? } }
/** * Constructs new telemetry client proxy instance with the given client. * @param telemetryClient The telemetry client. * @param instrumentationKey The instrumentation key. */ public LogTelemetryClientProxy(TelemetryClient telemetryClient, String instrumentationKey) { try { this.telemetryClient = telemetryClient; if (!LocalStringsUtils.isNullOrEmpty(instrumentationKey)) { this.telemetryClient.getContext().setInstrumentationKey(instrumentationKey); } this.isInitialized = true; } catch (Exception e) { // Catching all exceptions so in case of a failure the calling appender won't throw exception. // TODO: Assert.Debug/warning on exception? } }
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 Collection<Telemetry> generateTelemetryCollection() { Collection<Telemetry> rval = new ArrayList<>(); RequestTelemetry rt = new RequestTelemetry("test request", new Date(), 1234, "211", true); rt.getContext().setInstrumentationKey("fake-ikey"); rval.add(LocalForwarderModelTransformer.transform(rt)); return rval; }
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); }
@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()); } }
@Test public void sendAddsItemToBuffer() { final MetricTelemetry telemetry = new MetricTelemetry(); telemetry.getContext().setInstrumentationKey("fake-ikey"); channel.send(telemetry); verify(mockBuffer, times(1)).add(any(Telemetry.class)); }
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 testTelemetryNameWithIkey() throws IOException{ StubTelemetry telemetry = new StubTelemetry("Test Base Telemetry"); telemetry.getContext().setInstrumentationKey("AIF-00000000-1111-2222-3333-000000000000"); telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); int index = asJson.indexOf("\"name\":\"Microsoft.ApplicationInsights.aif00000000111122223333000000000000.Stub\""); assertTrue(index != -1); }
@Test public void testTelemetryNameWithIkey_SpecialChar() throws IOException{ StubTelemetry telemetry = new StubTelemetry("Test Base Telemetry"); telemetry.getContext().setInstrumentationKey("--. .--"); telemetry.setTimestamp(new Date()); StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); int index = asJson.indexOf("\"name\":\"Microsoft.ApplicationInsights.Stub\""); assertTrue(index != -1); }
@Test public void testSetInstrumentationKey() { TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey("key"); assertEquals("key", context.getInstrumentationKey()); }
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 testEmptyInstrumentationKeyOverridenWhenContextInitialized() { TelemetryContext contextToInitialize = new TelemetryContext(); TelemetryContext context = new TelemetryContext(); context.setInstrumentationKey(TEST_IKEY); contextToInitialize.initialize(context); Assert.assertEquals(TEST_IKEY, contextToInitialize.getInstrumentationKey()); }
@Test public void telemetryBuilderWithStandardFieldsMinimalFields() { String ikey = "123"; for (BaseTelemetry<?> bt : new BaseTelemetry[] { new MetricTelemetry(), new TraceTelemetry(), new ExceptionTelemetry(new RuntimeException("fake")), new RemoteDependencyTelemetry(), new EventTelemetry(), new PageViewTelemetry(), new RequestTelemetry(), new PerformanceCounterTelemetry() }) { String envelopeName = bt instanceof PerformanceCounterTelemetry ? MetricTelemetry.ENVELOPE_NAME : bt.getEnvelopName(); bt.getContext().setInstrumentationKey(ikey); final Date date = new Date(); bt.setTimestamp(date); assertEquals(Telemetry.newBuilder() .setDateTime(LocalStringsUtils.getDateFormatter().format(date)) .setVer(LocalForwarderModelTransformer.CURRENT_ENVELOPE_VERSION) .setInstrumentationKey(ikey) .setDataTypeName(String.format("Microsoft.ApplicationInsights.%s.%s", ikey, envelopeName)).buildPartial(), LocalForwarderModelTransformer.telemetryBuilderWithStandardFields(bt).buildPartial()); } }