@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; }
/** * 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); }