@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 testCtor() { TelemetryContext context = new TelemetryContext(); assertTrue(context.getProperties().isEmpty()); assertTrue(context.getTags().isEmpty()); assertNull(context.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; }
@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()); }
/** * 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); }
put("tag2", "something else"); }}; bt.getContext().getTags().putAll(tags);