Skip to content

Commit 1969059

Browse files
authored
Include trace flags in otlp marshaller (#6167)
1 parent f123d78 commit 1969059

File tree

7 files changed

+65
-29
lines changed

7 files changed

+65
-29
lines changed

dependencyManagement/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ val DEPENDENCIES = listOf(
6565
"eu.rekawek.toxiproxy:toxiproxy-java:2.1.7",
6666
"io.github.netmikey.logunit:logunit-jul:2.0.0",
6767
"io.jaegertracing:jaeger-client:1.8.1",
68-
"io.opentelemetry.proto:opentelemetry-proto:1.0.0-alpha",
68+
"io.opentelemetry.proto:opentelemetry-proto:1.1.0-alpha",
6969
"io.opentelemetry.contrib:opentelemetry-aws-xray-propagator:1.29.0-alpha",
7070
"io.opentracing:opentracing-api:0.33.0",
7171
"io.opentracing:opentracing-noop:0.33.0",

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/MarshalerUtil.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,11 @@ public static int sizeFixed64Optional(ProtoFieldInfo field, long value) {
257257
return field.getTagSize() + CodedOutputStream.computeFixed64SizeNoTag(value);
258258
}
259259

260+
/** Returns the size of a byte field when propagated to a fixed32. */
261+
public static int sizeByteAsFixed32(ProtoFieldInfo field, byte message) {
262+
return sizeFixed32(field, ((int) message) & 0xff);
263+
}
264+
260265
/** Returns the size of a fixed32 field. */
261266
public static int sizeFixed32(ProtoFieldInfo field, int message) {
262267
if (message == 0L) {

exporters/common/src/main/java/io/opentelemetry/exporter/internal/marshal/Serializer.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ public void serializeFixed64Optional(ProtoFieldInfo field, long value) throws IO
128128

129129
protected abstract void writeUInt64Value(long value) throws IOException;
130130

131+
/**
132+
* Serializes a byte as a protobuf {@code fixed32} field. Ensures that there is no sign
133+
* propagation if the high bit in the byte is set.
134+
*/
135+
public void serializeByteAsFixed32(ProtoFieldInfo field, byte value) throws IOException {
136+
serializeFixed32(field, ((int) value) & 0xff);
137+
}
138+
131139
/** Serializes a protobuf {@code fixed32} field. */
132140
public void serializeFixed32(ProtoFieldInfo field, int value) throws IOException {
133141
if (value == 0) {

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected void writeTo(Serializer output) throws IOException {
125125
output.serializeRepeatedMessage(LogRecord.ATTRIBUTES, attributeMarshalers);
126126
output.serializeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
127127

128-
output.serializeFixed32(LogRecord.FLAGS, toUnsignedInt(traceFlags.asByte()));
128+
output.serializeByteAsFixed32(LogRecord.FLAGS, traceFlags.asByte());
129129
output.serializeTraceId(LogRecord.TRACE_ID, traceId);
130130
output.serializeSpanId(LogRecord.SPAN_ID, spanId);
131131
}
@@ -155,7 +155,7 @@ private static int calculateSize(
155155
size += MarshalerUtil.sizeRepeatedMessage(LogRecord.ATTRIBUTES, attributeMarshalers);
156156
size += MarshalerUtil.sizeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
157157

158-
size += MarshalerUtil.sizeFixed32(LogRecord.FLAGS, toUnsignedInt(traceFlags.asByte()));
158+
size += MarshalerUtil.sizeByteAsFixed32(LogRecord.FLAGS, traceFlags.asByte());
159159
size += MarshalerUtil.sizeTraceId(LogRecord.TRACE_ID, traceId);
160160
size += MarshalerUtil.sizeSpanId(LogRecord.SPAN_ID, spanId);
161161
return size;
@@ -218,9 +218,4 @@ static ProtoEnumInfo toProtoSeverityNumber(Severity severity) {
218218
// NB: Should not be possible with aligned versions.
219219
return SeverityNumber.SEVERITY_NUMBER_UNSPECIFIED;
220220
}
221-
222-
/** Vendored {@link Byte#toUnsignedInt(byte)} to support Android. */
223-
private static int toUnsignedInt(byte x) {
224-
return ((int) x) & 0xff;
225-
}
226221
}

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/traces/SpanLinkMarshaler.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

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

10+
import io.opentelemetry.api.trace.TraceFlags;
1011
import io.opentelemetry.api.trace.TraceState;
1112
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
1213
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
@@ -26,6 +27,7 @@ final class SpanLinkMarshaler extends MarshalerWithSize {
2627
private final byte[] traceStateUtf8;
2728
private final KeyValueMarshaler[] attributeMarshalers;
2829
private final int droppedAttributesCount;
30+
private final TraceFlags traceFlags;
2931

3032
static SpanLinkMarshaler[] createRepeated(List<LinkData> links) {
3133
if (links.isEmpty()) {
@@ -51,6 +53,7 @@ static SpanLinkMarshaler create(LinkData link) {
5153
return new SpanLinkMarshaler(
5254
link.getSpanContext().getTraceId(),
5355
link.getSpanContext().getSpanId(),
56+
link.getSpanContext().getTraceFlags(),
5457
traceStateUtf8,
5558
KeyValueMarshaler.createForAttributes(link.getAttributes()),
5659
link.getTotalAttributeCount() - link.getAttributes().size());
@@ -59,14 +62,21 @@ static SpanLinkMarshaler create(LinkData link) {
5962
private SpanLinkMarshaler(
6063
String traceId,
6164
String spanId,
65+
TraceFlags traceFlags,
6266
byte[] traceStateUtf8,
6367
KeyValueMarshaler[] attributeMarshalers,
6468
int droppedAttributesCount) {
6569
super(
6670
calculateSize(
67-
traceId, spanId, traceStateUtf8, attributeMarshalers, droppedAttributesCount));
71+
traceId,
72+
spanId,
73+
traceFlags,
74+
traceStateUtf8,
75+
attributeMarshalers,
76+
droppedAttributesCount));
6877
this.traceId = traceId;
6978
this.spanId = spanId;
79+
this.traceFlags = traceFlags;
7080
this.traceStateUtf8 = traceStateUtf8;
7181
this.attributeMarshalers = attributeMarshalers;
7282
this.droppedAttributesCount = droppedAttributesCount;
@@ -79,11 +89,13 @@ public void writeTo(Serializer output) throws IOException {
7989
output.serializeString(Span.Link.TRACE_STATE, traceStateUtf8);
8090
output.serializeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
8191
output.serializeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
92+
output.serializeByteAsFixed32(Span.Link.FLAGS, traceFlags.asByte());
8293
}
8394

8495
private static int calculateSize(
8596
String traceId,
8697
String spanId,
98+
TraceFlags flags,
8799
byte[] traceStateUtf8,
88100
KeyValueMarshaler[] attributeMarshalers,
89101
int droppedAttributesCount) {
@@ -93,6 +105,7 @@ private static int calculateSize(
93105
size += MarshalerUtil.sizeBytes(Span.Link.TRACE_STATE, traceStateUtf8);
94106
size += MarshalerUtil.sizeRepeatedMessage(Span.Link.ATTRIBUTES, attributeMarshalers);
95107
size += MarshalerUtil.sizeUInt32(Span.Link.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount);
108+
size += MarshalerUtil.sizeByteAsFixed32(Span.Link.FLAGS, flags.asByte());
96109
return size;
97110
}
98111
}

exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/traces/SpanMarshaler.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import static io.opentelemetry.api.trace.propagation.internal.W3CTraceContextEncoding.encodeTraceState;
99

1010
import io.opentelemetry.api.trace.SpanKind;
11+
import io.opentelemetry.api.trace.TraceFlags;
1112
import io.opentelemetry.api.trace.TraceState;
1213
import io.opentelemetry.exporter.internal.marshal.MarshalerUtil;
1314
import io.opentelemetry.exporter.internal.marshal.MarshalerWithSize;
@@ -37,6 +38,7 @@ final class SpanMarshaler extends MarshalerWithSize {
3738
private final SpanLinkMarshaler[] spanLinkMarshalers;
3839
private final int droppedLinksCount;
3940
private final SpanStatusMarshaler spanStatusMarshaler;
41+
private final TraceFlags flags;
4042

4143
// Because SpanMarshaler is always part of a repeated field, it cannot return "null".
4244
static SpanMarshaler create(SpanData spanData) {
@@ -72,7 +74,8 @@ static SpanMarshaler create(SpanData spanData) {
7274
spanData.getTotalRecordedEvents() - spanData.getEvents().size(),
7375
spanLinkMarshalers,
7476
spanData.getTotalRecordedLinks() - spanData.getLinks().size(),
75-
SpanStatusMarshaler.create(spanData.getStatus()));
77+
SpanStatusMarshaler.create(spanData.getStatus()),
78+
spanData.getSpanContext().getTraceFlags());
7679
}
7780

7881
private SpanMarshaler(
@@ -90,7 +93,8 @@ private SpanMarshaler(
9093
int droppedEventsCount,
9194
SpanLinkMarshaler[] spanLinkMarshalers,
9295
int droppedLinksCount,
93-
SpanStatusMarshaler spanStatusMarshaler) {
96+
SpanStatusMarshaler spanStatusMarshaler,
97+
TraceFlags flags) {
9498
super(
9599
calculateSize(
96100
traceId,
@@ -107,7 +111,8 @@ private SpanMarshaler(
107111
droppedEventsCount,
108112
spanLinkMarshalers,
109113
droppedLinksCount,
110-
spanStatusMarshaler));
114+
spanStatusMarshaler,
115+
flags));
111116
this.traceId = traceId;
112117
this.spanId = spanId;
113118
this.traceStateUtf8 = traceStateUtf8;
@@ -123,6 +128,7 @@ private SpanMarshaler(
123128
this.spanLinkMarshalers = spanLinkMarshalers;
124129
this.droppedLinksCount = droppedLinksCount;
125130
this.spanStatusMarshaler = spanStatusMarshaler;
131+
this.flags = flags;
126132
}
127133

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

150156
output.serializeMessage(Span.STATUS, spanStatusMarshaler);
157+
output.serializeByteAsFixed32(Span.FLAGS, flags.asByte());
151158
}
152159

153160
private static int calculateSize(
@@ -165,7 +172,8 @@ private static int calculateSize(
165172
int droppedEventsCount,
166173
SpanLinkMarshaler[] spanLinkMarshalers,
167174
int droppedLinksCount,
168-
SpanStatusMarshaler spanStatusMarshaler) {
175+
SpanStatusMarshaler spanStatusMarshaler,
176+
TraceFlags flags) {
169177
int size = 0;
170178
size += MarshalerUtil.sizeTraceId(Span.TRACE_ID, traceId);
171179
size += MarshalerUtil.sizeSpanId(Span.SPAN_ID, spanId);
@@ -188,6 +196,7 @@ private static int calculateSize(
188196
size += MarshalerUtil.sizeUInt32(Span.DROPPED_LINKS_COUNT, droppedLinksCount);
189197

190198
size += MarshalerUtil.sizeMessage(Span.STATUS, spanStatusMarshaler);
199+
size += MarshalerUtil.sizeByteAsFixed32(Span.FLAGS, flags.asByte());
191200
return size;
192201
}
193202

exporters/otlp/common/src/test/java/io/opentelemetry/exporter/internal/otlp/traces/TraceRequestMarshalerTest.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void toProtoResourceSpans() {
112112

113113
@Test
114114
void toProtoSpan() {
115-
Span span =
115+
Span protoSpan =
116116
parse(
117117
Span.getDefaultInstance(),
118118
SpanMarshaler.create(
@@ -145,15 +145,17 @@ void toProtoSpan() {
145145
.setStatus(StatusData.ok())
146146
.build()));
147147

148-
assertThat(span.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
149-
assertThat(span.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
150-
assertThat(span.getTraceState()).isEqualTo(TRACE_STATE_VALUE);
151-
assertThat(span.getParentSpanId().toByteArray()).isEqualTo(new byte[] {});
152-
assertThat(span.getName()).isEqualTo("GET /api/endpoint");
153-
assertThat(span.getKind()).isEqualTo(SPAN_KIND_SERVER);
154-
assertThat(span.getStartTimeUnixNano()).isEqualTo(12345);
155-
assertThat(span.getEndTimeUnixNano()).isEqualTo(12349);
156-
assertThat(span.getAttributesList())
148+
assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(TRACE_ID_BYTES);
149+
assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(SPAN_ID_BYTES);
150+
assertThat(protoSpan.getFlags())
151+
.isEqualTo(((int) SPAN_CONTEXT.getTraceFlags().asByte()) & 0x00ff);
152+
assertThat(protoSpan.getTraceState()).isEqualTo(TRACE_STATE_VALUE);
153+
assertThat(protoSpan.getParentSpanId().toByteArray()).isEqualTo(new byte[] {});
154+
assertThat(protoSpan.getName()).isEqualTo("GET /api/endpoint");
155+
assertThat(protoSpan.getKind()).isEqualTo(SPAN_KIND_SERVER);
156+
assertThat(protoSpan.getStartTimeUnixNano()).isEqualTo(12345);
157+
assertThat(protoSpan.getEndTimeUnixNano()).isEqualTo(12349);
158+
assertThat(protoSpan.getAttributesList())
157159
.containsOnly(
158160
KeyValue.newBuilder()
159161
.setKey("key")
@@ -215,20 +217,22 @@ void toProtoSpan() {
215217
.build())
216218
.build())
217219
.build());
218-
assertThat(span.getDroppedAttributesCount()).isEqualTo(1);
219-
assertThat(span.getEventsList())
220+
assertThat(protoSpan.getDroppedAttributesCount()).isEqualTo(1);
221+
assertThat(protoSpan.getEventsList())
220222
.containsExactly(
221223
Span.Event.newBuilder().setTimeUnixNano(12347).setName("my_event").build());
222-
assertThat(span.getDroppedEventsCount()).isEqualTo(2); // 3 - 1
223-
assertThat(span.getLinksList())
224+
assertThat(protoSpan.getDroppedEventsCount()).isEqualTo(2); // 3 - 1
225+
assertThat(protoSpan.getLinksList())
224226
.containsExactly(
225227
Span.Link.newBuilder()
226228
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
227229
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
230+
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
228231
.setTraceState(encodeTraceState(SPAN_CONTEXT.getTraceState()))
229232
.build());
230-
assertThat(span.getDroppedLinksCount()).isEqualTo(1); // 2 - 1
231-
assertThat(span.getStatus()).isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).build());
233+
assertThat(protoSpan.getDroppedLinksCount()).isEqualTo(1); // 2 - 1
234+
assertThat(protoSpan.getStatus())
235+
.isEqualTo(Status.newBuilder().setCode(STATUS_CODE_OK).build());
232236
}
233237

234238
@Test
@@ -314,6 +318,7 @@ void toProtoSpanLink_WithoutAttributes() {
314318
Span.Link.newBuilder()
315319
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
316320
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
321+
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
317322
.setTraceState(TRACE_STATE_VALUE)
318323
.build());
319324
}
@@ -330,6 +335,7 @@ void toProtoSpanLink_WithAttributes() {
330335
Span.Link.newBuilder()
331336
.setTraceId(ByteString.copyFrom(TRACE_ID_BYTES))
332337
.setSpanId(ByteString.copyFrom(SPAN_ID_BYTES))
338+
.setFlags(SPAN_CONTEXT.getTraceFlags().asByte())
333339
.setTraceState(TRACE_STATE_VALUE)
334340
.addAttributes(
335341
KeyValue.newBuilder()

0 commit comments

Comments
 (0)