@Override public void setInstrumentationKey(String key) { if (this.telemetryClient != null && key != null) { this.telemetryClient.getContext().setInstrumentationKey(key); } }
@Override public String getInstrumentationKey() { return this.telemetryClient.getContext().getInstrumentationKey(); }
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; }
private void setupTelemetryClientMock(final List<Telemetry> telemetries) { Mockito.when(this.telemetryClientMock.getContext()).thenReturn(new TelemetryContext()); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Telemetry telemetry = ((Telemetry) invocation.getArguments()[0]); telemetries.add(telemetry); return null; } }).when(this.telemetryClientMock).track(Matchers.any(Telemetry.class)); }
private void setupTelemetryClientMock(final List<Telemetry> telemetries) { Mockito.when(this.telemetryClientMock.getContext()).thenReturn(new TelemetryContext()); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Telemetry telemetry = ((Telemetry) invocation.getArguments()[0]); telemetries.add(telemetry); return null; } }).when(this.telemetryClientMock).track(Matchers.any(Telemetry.class)); }
private void setupTelemetryClientMock(final List<Telemetry> telemetries) { Mockito.when(this.telemetryClientMock.getContext()).thenReturn(new TelemetryContext()); Mockito.doAnswer(new Answer() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { Telemetry telemetry = ((Telemetry) invocation.getArguments()[0]); telemetries.add(telemetry); return null; } }).when(this.telemetryClientMock).track(Matchers.any(Telemetry.class)); }
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); } }
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 testWhenTelemetryClientProxyFailedToInitializeNoExceptionIsThrown() { // Setting telemetry client mock to fail proxy initialization when getting client context. Mockito.when(this.telemetryClientMock.getContext()).thenThrow(new NullPointerException("Exception!")); this.telemetryClientProxy = new LogTelemetryClientProxy(this.telemetryClientMock, INSTRUMENTATION_KEY); Assert.assertFalse("The proxy shouldn't have been initialized.", this.telemetryClientProxy.isInitialized()); }
@Test public void testWhenTelemetryClientProxyFailedToInitializeNoExceptionIsThrown() { // Setting telemetry client mock to fail proxy initialization when getting client context. Mockito.when(this.telemetryClientMock.getContext()).thenThrow(new NullPointerException("Exception!")); this.telemetryClientProxy = new LogTelemetryClientProxy(this.telemetryClientMock, INSTRUMENTATION_KEY); Assert.assertFalse("The proxy shouldn't have been initialized.", this.telemetryClientProxy.isInitialized()); }
@Test public void testWhenTelemetryClientProxyFailedToInitializeNoExceptionIsThrown() { // Setting telemetry client mock to fail proxy initialization when getting client context. Mockito.when(this.telemetryClientMock.getContext()).thenThrow(new NullPointerException("Exception!")); this.telemetryClientProxy = new LogTelemetryClientProxy(this.telemetryClientMock, INSTRUMENTATION_KEY); Assert.assertFalse("The proxy shouldn't have been initialized.", this.telemetryClientProxy.isInitialized()); }
@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 public void testNodeNameExists() { String nodeName = client.getContext().getInternal().getNodeName(); Assert.assertFalse(nodeName == null || nodeName.length()==0); }
@Test public void testWebAppNameInitializerAppNameHandoff() throws Exception { FilterAndTelemetryClientMock mocks = createInitializedFilterWithExposedTelemetryClient(); FilterChain chain = mock(FilterChain.class); mocks.filter.doFilter(ServletUtils.generateDummyServletRequest(), ServletUtils.generateDummyServletResponse(), chain); String role = mocks.mockTelemetryClient.getContext().getCloud().getRole(); assertNotNull(role); assertEquals(this.mockContextPath.replace("/", ""), role); }
@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 testOverideNodeName(){ String overrideNode = "NewNodeName"; client.getContext().getInternal().setNodeName(overrideNode); client.trackEvent("Event"); EventTelemetry telemetry = (EventTelemetry) verifyAndGetLastEventSent(); String nodeName = telemetry.getContext().getInternal().getNodeName(); Assert.assertTrue("NodeName was not overriden", nodeName.equals(overrideNode)); }