Skip to content

Commit

Permalink
Include trace flags in otlp marshaller (#6167)
Browse files Browse the repository at this point in the history
  • Loading branch information
breedx-splk authored Jan 31, 2024
1 parent f123d78 commit 1969059
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 29 deletions.
2 changes: 1 addition & 1 deletion dependencyManagement/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ val DEPENDENCIES = listOf(
"eu.rekawek.toxiproxy:toxiproxy-java:2.1.7",
"io.github.netmikey.logunit:logunit-jul:2.0.0",
"io.jaegertracing:jaeger-client:1.8.1",
"io.opentelemetry.proto:opentelemetry-proto:1.0.0-alpha",
"io.opentelemetry.proto:opentelemetry-proto:1.1.0-alpha",
"io.opentelemetry.contrib:opentelemetry-aws-xray-propagator:1.29.0-alpha",
"io.opentracing:opentracing-api:0.33.0",
"io.opentracing:opentracing-noop:0.33.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,11 @@ public static int sizeFixed64Optional(ProtoFieldInfo field, long value) {
return field.getTagSize() + CodedOutputStream.computeFixed64SizeNoTag(value);
}

/** Returns the size of a byte field when propagated to a fixed32. */
public static int sizeByteAsFixed32(ProtoFieldInfo field, byte message) {
return sizeFixed32(field, ((int) message) & 0xff);
}

/** Returns the size of a fixed32 field. */
public static int sizeFixed32(ProtoFieldInfo field, int message) {
if (message == 0L) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ public void serializeFixed64Optional(ProtoFieldInfo field, long value) throws IO

protected abstract void writeUInt64Value(long value) throws IOException;

/**
* Serializes a byte as a protobuf {@code fixed32} field. Ensures that there is no sign
* propagation if the high bit in the byte is set.
*/
public void serializeByteAsFixed32(ProtoFieldInfo field, byte value) throws IOException {
serializeFixed32(field, ((int) value) & 0xff);
}

/** Serializes a protobuf {@code fixed32} field. */
public void serializeFixed32(ProtoFieldInfo field, int value) throws IOException {
if (value == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected void writeTo(Serializer output) throws IOException {
output.serializeRepeatedMessage(LogRecord.ATTRIBUTES, attributeMarshalers);
output.serializeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);

output.serializeFixed32(LogRecord.FLAGS, toUnsignedInt(traceFlags.asByte()));
output.serializeByteAsFixed32(LogRecord.FLAGS, traceFlags.asByte());
output.serializeTraceId(LogRecord.TRACE_ID, traceId);
output.serializeSpanId(LogRecord.SPAN_ID, spanId);
}
Expand Down Expand Up @@ -155,7 +155,7 @@ private static int calculateSize(
size += MarshalerUtil.sizeRepeatedMessage(LogRecord.ATTRIBUTES, attributeMarshalers);
size += MarshalerUtil.sizeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);

size += MarshalerUtil.sizeFixed32(LogRecord.FLAGS, toUnsignedInt(traceFlags.asByte()));
size += MarshalerUtil.sizeByteAsFixed32(LogRecord.FLAGS, traceFlags.asByte());
size += MarshalerUtil.sizeTraceId(LogRecord.TRACE_ID, traceId);
size += MarshalerUtil.sizeSpanId(LogRecord.SPAN_ID, spanId);
return size;
Expand Down Expand Up @@ -218,9 +218,4 @@ static ProtoEnumInfo toProtoSeverityNumber(Severity severity) {
// NB: Should not be possible with aligned versions.
return SeverityNumber.SEVERITY_NUMBER_UNSPECIFIED;
}

/** Vendored {@link Byte#toUnsignedInt(byte)} to support Android. */
private static int toUnsignedInt(byte x) {
return ((int) x) & 0xff;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState;

import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
Expand All @@ -26,6 +27,7 @@ final class SpanLinkMarshaler extends MarshalerWithSize {
private final byte[] traceStateUtf8;
private final KeyValueMarshaler[] attributeMarshalers;
private final int droppedAttributesCount;
private final TraceFlags traceFlags;

static SpanLinkMarshaler[] createRepeated(List<LinkData> links) {
if (links.isEmpty()) {
Expand All @@ -51,6 +53,7 @@ static SpanLinkMarshaler create(LinkData link) {
return new SpanLinkMarshaler(
link.getSpanContext().getTraceId(),
link.getSpanContext().getSpanId(),
link.getSpanContext().getTraceFlags(),
traceStateUtf8,
KeyValueMarshaler.createForAttributes(link.getAttributes()),
link.getTotalAttributeCount() - link.getAttributes().size());
Expand All @@ -59,14 +62,21 @@ static SpanLinkMarshaler create(LinkData link) {
private SpanLinkMarshaler(
String traceId,
String spanId,
TraceFlags traceFlags,
byte[] traceStateUtf8,
KeyValueMarshaler[] attributeMarshalers,
int droppedAttributesCount) {
super(
calculateSize(
traceId, spanId, traceStateUtf8, attributeMarshalers, droppedAttributesCount));
traceId,
spanId,
traceFlags,
traceStateUtf8,
attributeMarshalers,
droppedAttributesCount));
this.traceId = traceId;
this.spanId = spanId;
this.traceFlags = traceFlags;
this.traceStateUtf8 = traceStateUtf8;
this.attributeMarshalers = attributeMarshalers;
this.droppedAttributesCount = droppedAttributesCount;
Expand All @@ -79,11 +89,13 @@ public void writeTo(Serializer output) throws IOException {
output.serializeString(Span.Link.TRACE_STATE, traceStateUtf8);
output.serializeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
output.serializeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
output.serializeByteAsFixed32(Span.Link.FLAGS, traceFlags.asByte());
}

private static int calculateSize(
String traceId,
String spanId,
TraceFlags flags,
byte[] traceStateUtf8,
KeyValueMarshaler[] attributeMarshalers,
int droppedAttributesCount) {
Expand All @@ -93,6 +105,7 @@ private static int calculateSize(
size += MarshalerUtil.sizeBytes(Span.Link.TRACE_STATE, traceStateUtf8);
size += MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
size += MarshalerUtil.sizeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
size += MarshalerUtil.sizeByteAsFixed32(Span.Link.FLAGS, flags.asByte());
return size;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
Expand Down Expand Up @@ -37,6 +38,7 @@ final class SpanMarshaler extends MarshalerWithSize {
private final SpanLinkMarshaler[] spanLinkMarshalers;
private final int droppedLinksCount;
private final SpanStatusMarshaler spanStatusMarshaler;
private final TraceFlags flags;

// Because SpanMarshaler is always part of a repeated field, it cannot return "null".
static SpanMarshaler create(SpanData spanData) {
Expand Down Expand Up @@ -72,7 +74,8 @@ static SpanMarshaler create(SpanData spanData) {
spanData.getTotalRecordedEvents() - spanData.getEvents().size(),
spanLinkMarshalers,
spanData.getTotalRecordedLinks() - spanData.getLinks().size(),
SpanStatusMarshaler.create(spanData.getStatus()));
SpanStatusMarshaler.create(spanData.getStatus()),
spanData.getSpanContext().getTraceFlags());
}

private SpanMarshaler(
Expand All @@ -90,7 +93,8 @@ private SpanMarshaler(
int droppedEventsCount,
SpanLinkMarshaler[] spanLinkMarshalers,
int droppedLinksCount,
SpanStatusMarshaler spanStatusMarshaler) {
SpanStatusMarshaler spanStatusMarshaler,
TraceFlags flags) {
super(
calculateSize(
traceId,
Expand All @@ -107,7 +111,8 @@ private SpanMarshaler(
droppedEventsCount,
spanLinkMarshalers,
droppedLinksCount,
spanStatusMarshaler));
spanStatusMarshaler,
flags));
this.traceId = traceId;
this.spanId = spanId;
this.traceStateUtf8 = traceStateUtf8;
Expand All @@ -123,6 +128,7 @@ private SpanMarshaler(
this.spanLinkMarshalers = spanLinkMarshalers;
this.droppedLinksCount = droppedLinksCount;
this.spanStatusMarshaler = spanStatusMarshaler;
this.flags = flags;
}

@Override
Expand All @@ -148,6 +154,7 @@ public void writeTo(Serializer output) throws IOException {
output.serializeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);

output.serializeMessage(Span.STATUS, spanStatusMarshaler);
output.serializeByteAsFixed32(Span.FLAGS, flags.asByte());
}

private static int calculateSize(
Expand All @@ -165,7 +172,8 @@ private static int calculateSize(
int droppedEventsCount,
SpanLinkMarshaler[] spanLinkMarshalers,
int droppedLinksCount,
SpanStatusMarshaler spanStatusMarshaler) {
SpanStatusMarshaler spanStatusMarshaler,
TraceFlags flags) {
int size = 0;
size += MarshalerUtil.sizeTraceId(Span.TRACE_ID, traceId);
size += MarshalerUtil.sizeSpanId(Span.SPAN_ID, spanId);
Expand All @@ -188,6 +196,7 @@ private static int calculateSize(
size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);

size += MarshalerUtil.sizeMessage(Span.STATUS, spanStatusMarshaler);
size += MarshalerUtil.sizeByteAsFixed32(Span.FLAGS, flags.asByte());
return size;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void toProtoResourceSpans() {

@Test
void toProtoSpan() {
Span span =
Span protoSpan =
parse(
Span.getDefaultInstance(),
SpanMarshaler.create(
Expand Down Expand Up @@ -145,15 +145,17 @@ void toProtoSpan() {
.setStatus(StatusData.ok())
.build()));

assertThat(span.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
assertThat(span.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
assertThat(span.getTraceState()).isEqualTo(TRACE_STATE_VALUE);
assertThat(span.getParentSpanId().toByteArray()).isEqualTo(new byte[] {});
assertThat(span.getName()).isEqualTo("GET /api/endpoint");
assertThat(span.getKind()).isEqualTo(SPAN_KIND_SERVER);
assertThat(span.getStartTimeUnixNano()).isEqualTo(12345);
assertThat(span.getEndTimeUnixNano()).isEqualTo(12349);
assertThat(span.getAttributesList())
assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
assertThat(protoSpan.getFlags())
.isEqualTo(((int) SPAN_CONTEXT.getTraceFlags().asByte()) & 0x00ff);
assertThat(protoSpan.getTraceState()).isEqualTo(TRACE_STATE_VALUE);
assertThat(protoSpan.getParentSpanId().toByteArray()).isEqualTo(new byte[] {});
assertThat(protoSpan.getName()).isEqualTo("GET /api/endpoint");
assertThat(protoSpan.getKind()).isEqualTo(SPAN_KIND_SERVER);
assertThat(protoSpan.getStartTimeUnixNano()).isEqualTo(12345);
assertThat(protoSpan.getEndTimeUnixNano()).isEqualTo(12349);
assertThat(protoSpan.getAttributesList())
.containsOnly(
KeyValue.newBuilder()
.setKey("key")
Expand Down Expand Up @@ -215,20 +217,22 @@ void toProtoSpan() {
.build())
.build())
.build());
assertThat(span.getDroppedAttributesCount()).isEqualTo(1);
assertThat(span.getEventsList())
assertThat(protoSpan.getDroppedAttributesCount()).isEqualTo(1);
assertThat(protoSpan.getEventsList())
.containsExactly(
Span.Event.newBuilder().setTimeUnixNano(12347).setName("my_event").build());
assertThat(span.getDroppedEventsCount()).isEqualTo(2); // 3 - 1
assertThat(span.getLinksList())
assertThat(protoSpan.getDroppedEventsCount()).isEqualTo(2); // 3 - 1
assertThat(protoSpan.getLinksList())
.containsExactly(
Span.Link.newBuilder()
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
.setTraceState(encodeTraceState(SPAN_CONTEXT.getTraceState()))
.build());
assertThat(span.getDroppedLinksCount()).isEqualTo(1); // 2 - 1
assertThat(span.getStatus()).isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).build());
assertThat(protoSpan.getDroppedLinksCount()).isEqualTo(1); // 2 - 1
assertThat(protoSpan.getStatus())
.isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).build());
}

@Test
Expand Down Expand Up @@ -314,6 +318,7 @@ void toProtoSpanLink_WithoutAttributes() {
Span.Link.newBuilder()
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
.setTraceState(TRACE_STATE_VALUE)
.build());
}
Expand All @@ -330,6 +335,7 @@ void toProtoSpanLink_WithAttributes() {
Span.Link.newBuilder()
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
.setTraceState(TRACE_STATE_VALUE)
.addAttributes(
KeyValue.newBuilder()
Expand Down

0 comments on commit 1969059

Please sign in to comment.