Skip to content

Commit

Permalink
Serialize observed timestamp (#5382)
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored Apr 16, 2023
1 parent 8136639 commit da38225
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class OtlpJsonLoggingLogRecordExporterTest {
.setBody("body1")
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTimestamp(1631533710L, TimeUnit.MILLISECONDS)
.setTimestamp(100L, TimeUnit.NANOSECONDS)
.setObservedTimestamp(200L, TimeUnit.NANOSECONDS)
.setAttributes(Attributes.of(stringKey("animal"), "cat", longKey("lives"), 9L))
.setSpanContext(
SpanContext.create(
Expand All @@ -66,7 +67,8 @@ class OtlpJsonLoggingLogRecordExporterTest {
.setBody("body2")
.setSeverity(Severity.INFO)
.setSeverityText("INFO")
.setTimestamp(1631533710L, TimeUnit.MILLISECONDS)
.setTimestamp(100L, TimeUnit.NANOSECONDS)
.setObservedTimestamp(200L, TimeUnit.NANOSECONDS)
.setAttributes(Attributes.of(booleanKey("important"), true))
.setSpanContext(
SpanContext.create(
Expand Down Expand Up @@ -110,7 +112,8 @@ void log() throws Exception {
+ " \"version\":\"2\""
+ " },"
+ " \"logRecords\": [{"
+ " \"timeUnixNano\":\"1631533710000000\","
+ " \"timeUnixNano\":\"100\","
+ " \"observedTimeUnixNano\":\"200\","
+ " \"severityNumber\":9,"
+ " \"severityText\":\"INFO\","
+ " \"body\": {"
Expand All @@ -137,7 +140,8 @@ void log() throws Exception {
+ " }]"
+ " },"
+ " \"logRecords\": [{"
+ " \"timeUnixNano\":\"1631533710000000\","
+ " \"timeUnixNano\":\"100\","
+ " \"observedTimeUnixNano\":\"200\","
+ " \"severityNumber\":9,"
+ " \"severityText\":\"INFO\","
+ " \"body\": {"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class LogMarshaler extends MarshalerWithSize {
private static final String INVALID_SPAN_ID = SpanId.getInvalid();

private final long timeUnixNano;
private final long observedTimeUnixNano;
private final ProtoEnumInfo severityNumber;
private final byte[] severityText;
private final MarshalerWithSize anyValueMarshaler;
Expand All @@ -47,6 +48,7 @@ static LogMarshaler create(LogRecordData logRecordData) {
SpanContext spanContext = logRecordData.getSpanContext();
return new LogMarshaler(
logRecordData.getTimestampEpochNanos(),
logRecordData.getObservedTimestampEpochNanos(),
toProtoSeverityNumber(logRecordData.getSeverity()),
MarshalerUtil.toBytes(logRecordData.getSeverityText()),
anyValueMarshaler,
Expand All @@ -59,6 +61,7 @@ static LogMarshaler create(LogRecordData logRecordData) {

private LogMarshaler(
long timeUnixNano,
long observedTimeUnixNano,
ProtoEnumInfo severityNumber,
byte[] severityText,
MarshalerWithSize anyValueMarshaler,
Expand All @@ -70,6 +73,7 @@ private LogMarshaler(
super(
calculateSize(
timeUnixNano,
observedTimeUnixNano,
severityNumber,
severityText,
anyValueMarshaler,
Expand All @@ -79,6 +83,7 @@ private LogMarshaler(
traceId,
spanId));
this.timeUnixNano = timeUnixNano;
this.observedTimeUnixNano = observedTimeUnixNano;
this.traceId = traceId;
this.spanId = spanId;
this.traceFlags = traceFlags;
Expand All @@ -93,6 +98,8 @@ private LogMarshaler(
protected void writeTo(Serializer output) throws IOException {
output.serializeFixed64(LogRecord.TIME_UNIX_NANO, timeUnixNano);

output.serializeFixed64(LogRecord.OBSERVED_TIME_UNIX_NANO, observedTimeUnixNano);

output.serializeEnum(LogRecord.SEVERITY_NUMBER, severityNumber);

output.serializeString(LogRecord.SEVERITY_TEXT, severityText);
Expand All @@ -109,6 +116,7 @@ protected void writeTo(Serializer output) throws IOException {

private static int calculateSize(
long timeUnixNano,
long observedTimeUnixNano,
ProtoEnumInfo severityNumber,
byte[] severityText,
MarshalerWithSize anyValueMarshaler,
Expand All @@ -120,6 +128,8 @@ private static int calculateSize(
int size = 0;
size += MarshalerUtil.sizeFixed64(LogRecord.TIME_UNIX_NANO, timeUnixNano);

size += MarshalerUtil.sizeFixed64(LogRecord.OBSERVED_TIME_UNIX_NANO, observedTimeUnixNano);

size += MarshalerUtil.sizeEnum(LogRecord.SEVERITY_NUMBER, severityNumber);

size += MarshalerUtil.sizeBytes(LogRecord.SEVERITY_TEXT, severityText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ void toProtoResourceLogs() {
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setTotalAttributeCount(2)
.setTimestamp(12345, TimeUnit.NANOSECONDS)
.setObservedTimestamp(6789, TimeUnit.NANOSECONDS)
.build()));

assertThat(resourceLogsMarshalers).hasSize(1);
Expand Down Expand Up @@ -114,6 +115,7 @@ void toProtoLogRecord() {
.setAttributes(Attributes.of(AttributeKey.booleanKey("key"), true))
.setTotalAttributeCount(2)
.setTimestamp(12345, TimeUnit.NANOSECONDS)
.setObservedTimestamp(6789, TimeUnit.NANOSECONDS)
.build()));

assertThat(logRecord.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
Expand All @@ -128,6 +130,7 @@ void toProtoLogRecord() {
.build());
assertThat(logRecord.getDroppedAttributesCount()).isEqualTo(1);
assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345);
assertThat(logRecord.getObservedTimeUnixNano()).isEqualTo(6789);
}

@Test
Expand All @@ -142,6 +145,7 @@ void toProtoLogRecord_MinimalFields() {
.setInstrumentationScopeInfo(
InstrumentationScopeInfo.builder("instrumentation").setVersion("1").build())
.setTimestamp(12345, TimeUnit.NANOSECONDS)
.setObservedTimestamp(6789, TimeUnit.NANOSECONDS)
.build()));

assertThat(logRecord.getTraceId()).isEmpty();
Expand All @@ -153,6 +157,7 @@ void toProtoLogRecord_MinimalFields() {
assertThat(logRecord.getAttributesList()).isEmpty();
assertThat(logRecord.getDroppedAttributesCount()).isZero();
assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345);
assertThat(logRecord.getObservedTimeUnixNano()).isEqualTo(6789);
}

@SuppressWarnings("unchecked")
Expand Down

0 comments on commit da38225

Please sign in to comment.