Skip to content

Commit 96954d7

Browse files
committed
Fix NPE on null span type tag, and add regression test
1 parent 2456c8b commit 96954d7

2 files changed

Lines changed: 16 additions & 9 deletions

File tree

dd-trace-core/src/main/java/datadog/trace/core/otlp/trace/OtlpTraceProto.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ public static byte[] recordSpanMessage(
138138
}
139139
writeSpanTag(buf, RESOURCE_NAME, span.getResourceName());
140140
writeSpanTag(buf, OPERATION_NAME, span.getOperationName());
141-
writeSpanTag(buf, SPAN_TYPE, span.getSpanType());
141+
if (span.getSpanType() != null) {
142+
writeSpanTag(buf, SPAN_TYPE, span.getSpanType());
143+
}
142144

143145
span.processTagsAndBaggage(metaWriter);
144146

@@ -222,17 +224,11 @@ private static void writeSpanTag(StreamingBuffer buf, TagMap.EntryReader tagEntr
222224

223225
private static void writeSpanTag(
224226
StreamingBuffer buf, UTF8BytesString key, UTF8BytesString value) {
225-
if (value == null) {
226-
return;
227-
}
228227
writeTag(buf, 9, LEN_WIRE_TYPE);
229228
writeAttribute(buf, key, value);
230229
}
231230

232231
private static void writeSpanTag(StreamingBuffer buf, UTF8BytesString key, CharSequence value) {
233-
if (value == null) {
234-
return;
235-
}
236232
writeTag(buf, 9, LEN_WIRE_TYPE);
237233
if (value instanceof UTF8BytesString) {
238234
writeAttribute(buf, key, (UTF8BytesString) value);

dd-trace-core/src/test/java/datadog/trace/core/otlp/trace/OtlpTraceProtoTest.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,11 @@ static Stream<Arguments> cases() {
443443
Arguments.of(
444444
"minimal span — default UNSPECIFIED kind",
445445
asList(span("GET /api/users", "servlet.request", "web"))),
446+
447+
// ── null span type — regression: must not NPE, span.type attribute omitted ─
448+
Arguments.of(
449+
"null span type — span.type attribute omitted, no NPE",
450+
asList(span("GET /api/users", "servlet.request", null))),
446451
Arguments.of("internal span kind", asList(kindSpan("GET /api/users", SPAN_KIND_INTERNAL))),
447452
Arguments.of("server span kind", asList(kindSpan("GET /api/users", SPAN_KIND_SERVER))),
448453
Arguments.of("client span kind", asList(kindSpan("redis.get", SPAN_KIND_CLIENT))),
@@ -1029,8 +1034,14 @@ private static void verifySpan(CodedInputStream sp, DDSpan span, SpanSpec spec,
10291034
assertTrue(
10301035
attrKeys.contains("operation.name"),
10311036
"attributes must include 'operation.name' [" + caseName + "]");
1032-
assertTrue(
1033-
attrKeys.contains("span.type"), "attributes must include 'span.type' [" + caseName + "]");
1037+
if (spec.spanType != null) {
1038+
assertTrue(
1039+
attrKeys.contains("span.type"), "attributes must include 'span.type' [" + caseName + "]");
1040+
} else {
1041+
assertFalse(
1042+
attrKeys.contains("span.type"),
1043+
"attributes must omit 'span.type' when null [" + caseName + "]");
1044+
}
10341045

10351046
// service.name attribute is written only when the span's service differs from the default
10361047
if (spec.serviceName != null) {

0 commit comments

Comments
 (0)