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); } } } }
@Test public void testWhenTelemetryPropertiesAlreadyPopulatedDeviceIdStillSet() { final String host = "predefined_host"; telemetry.getProperties().put(Constants.DOCKER_HOST_PROPERTY_KEY, host); initializerUnderTest.initialize(telemetry); Assert.assertEquals(host, telemetry.getProperties().get(Constants.DOCKER_HOST_PROPERTY_KEY)); Assert.assertEquals(DEFAULT_CONTAINER_NAME, telemetry.getContext().getDevice().getId()); }
/** * Sets the timestamp to 'now' unless the timestamp is already set. * @param telemetry The {@link com.microsoft.applicationinsights.telemetry.Telemetry} to initialize. */ @Override public void initialize(Telemetry telemetry) { if (telemetry.getTimestamp() == null) { telemetry.setTimestamp(new Date()); } } }
@Override protected boolean doSend(Telemetry telemetry) { StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = null; try { jsonWriter = new JsonTelemetryDataSerializer(writer); telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); telemetryBuffer.add(asJson); telemetry.reset(); } catch (IOException e) { InternalLogger.INSTANCE.error("Failed to serialize Telemetry"); InternalLogger.INSTANCE.trace("Stack trace is %s", ExceptionUtils.getStackTrace(e)); return false; } return true; }
@Test public void testMethodsOnTelemetryAreCalledWhenTracking() { TelemetryChannel mockChannel = Mockito.mock(TelemetryChannel.class); configuration.setChannel(mockChannel); TelemetryContext mockContext = new TelemetryContext(); 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); Mockito.verify(mockTelemetry, Mockito.times(1)).setTimestamp(any(Date.class)); }
if (telemetry.getTimestamp() == null) { telemetry.setTimestamp(new Date()); telemetry.getContext().initialize(ctx); } catch (ThreadDeath td) { throw td; if (Strings.isNullOrEmpty(telemetry.getContext().getInstrumentationKey())) { throw new IllegalArgumentException("Instrumentation key cannot be undefined.");
@Test public void testTelemetryPropertiesNotInitializedWhenAlreadyPopulated() { final String host = "predefined_host"; telemetry.getProperties().put(Constants.DOCKER_HOST_PROPERTY_KEY, host); initializerUnderTest.initialize(telemetry); Assert.assertEquals(host, telemetry.getProperties().get(Constants.DOCKER_HOST_PROPERTY_KEY)); }
@Override public void send(Telemetry item) { try { StringWriter writer = new StringWriter(); item.serialize(new JsonTelemetryDataSerializer(writer)); InternalLogger.INSTANCE.trace("StdOutChannel, TELEMETRY: %s", writer.toString()); } catch (IOException ioe) { } }
private static Telemetry createMockTelemetryAndActivateInitializer(Date mockValue) { Telemetry telemetry = Mockito.mock(Telemetry.class); Mockito.doReturn(mockValue).when(telemetry).getTimestamp(); new TimestampPropertyInitializer().initialize(telemetry); return telemetry; } }
@Test public void testSetTimestampWithNonNull() { Telemetry telemetry = createMockTelemetryAndActivateInitializer(new Date()); Mockito.verify(telemetry, Mockito.never()).setTimestamp((Date)anyObject()); }
@Test public void testMDCPropertiesAreBeingSetAsCustomDimensions() throws Exception { Logger logger = (Logger) LoggerFactory.getLogger("root"); MDC.put("key", "value"); logger.error("This is an exception", new Exception("Fake Exception")); TimeUnit.SECONDS.sleep(1); Assert.assertTrue(LogChannelMockVerifier.INSTANCE.getTelemetryCollection().get(0).getProperties().get("key").equals("value")); }
private static ArrayList<String> toJson(List<Telemetry> telemetries) throws IOException { StringWriter writer = new StringWriter(); JsonTelemetryDataSerializer jsonWriter = new JsonTelemetryDataSerializer(writer); ArrayList<String> asJsons = new ArrayList<String>(); for (Telemetry telemetry : telemetries) { telemetry.serialize(jsonWriter); jsonWriter.close(); String asJson = writer.toString(); asJsons.add(asJson); writer.getBuffer().setLength(0); jsonWriter.reset(writer); } return asJsons; } }
@Test public void testTrackWithCustomTelemetryTimestamp() { Date timestamp = new Date(10000); client.track(new RequestTelemetry("Name", timestamp, 1, "200", true)); Telemetry telemetry = verifyAndGetLastEventSent(); assertEquals(telemetry.getTimestamp(), timestamp); }
@Test public void testSetTimestampWithNull() { Telemetry telemetry = createMockTelemetryAndActivateInitializer(null); Mockito.verify(telemetry, Mockito.times(1)).setTimestamp((Date)anyObject()); }
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); } } } }
telemetry.getContext().getOperation().setId(requestTelemetry.getId()); return; if (CommonUtils.isNullOrEmpty(telemetry.getContext().getOperation().getId())) { telemetry.getContext().getOperation().setId(currentOperationId); if (CommonUtils.isNullOrEmpty(telemetry.getContext().getOperation().getParentId())) { telemetry.getContext().getOperation().setParentId(requestTelemetry.getId()); if (telemetry.getProperties().get(key) == null) { telemetry.getProperties().put(key, correlationContextMap.get(key));
@Test public void testLoggerMessageIsRetainedWhenReportingException() throws Exception { Logger logger = LogManager.getRootLogger(); logger.error("This is an exception", new Exception("Fake Exception")); TimeUnit.SECONDS.sleep(1); Assert.assertEquals(1, LogChannelMockVerifier.INSTANCE.getTelemetryCollection().size()); Assert.assertTrue(LogChannelMockVerifier.INSTANCE.getTelemetryCollection().get(0).getProperties().containsKey("Logger Message")); Assert.assertTrue(LogChannelMockVerifier.INSTANCE.getTelemetryCollection().get(0).getProperties().get("Logger Message").equals("This is an exception")); }
item.serialize(new JsonTelemetryDataSerializer(writer));
@Override public void initialize(Telemetry telemetry) { CloudContext cloud = telemetry.getContext().getCloud(); cloud.setRole(appName); }
telemetry.getContext().getOperation().setId(requestTelemetry.getId()); return; if (CommonUtils.isNullOrEmpty(telemetry.getContext().getOperation().getId())) { telemetry.getContext().getOperation().setId(currentOperationId); if (CommonUtils.isNullOrEmpty(telemetry.getContext().getOperation().getParentId())) { telemetry.getContext().getOperation().setParentId(requestTelemetry.getId()); if (telemetry.getProperties().get(key) == null) { telemetry.getProperties().put(key, correlationContextMap.get(key));