Skip to content

Commit edd20e9

Browse files
committed
fix: guard setTraceId against malformed trace IDs
1 parent b2327dc commit edd20e9

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

lib/src/main/java/io/otel/pyroscope/PyroscopeOtelSpanProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ public void onStart(Context parentContext, ReadWriteSpan span) {
6666
// W3C trace ID is 32 hex chars (128 bits). Parse directly into two longs
6767
// to avoid the String#substring allocations on this hot path.
6868
String traceId = span.getSpanContext().getTraceId();
69-
asprof.setTraceId(parseHex64(traceId, 0), parseHex64(traceId, 16));
69+
try {
70+
asprof.setTraceId(parseHex64(traceId, 0), parseHex64(traceId, 16));
71+
} catch (NumberFormatException | IndexOutOfBoundsException e) {
72+
asprof.setTraceId(0, 0);
73+
}
7074
}
7175

7276
@Override

otel-extension/src/main/java/io/otel/pyroscope/PyroscopeOtelSpanProcessor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public void onStart(Context parentContext, ReadWriteSpan span) {
5252

5353
span.setAttribute(ATTRIBUTE_KEY_PROFILE_ID, strProfileId);
5454
api.setTracingContext(spanId, spanName);
55-
api.setTraceId(span.getSpanContext().getTraceId());
55+
try {
56+
api.setTraceId(span.getSpanContext().getTraceId());
57+
} catch (NumberFormatException | IndexOutOfBoundsException e) {
58+
api.clearTraceId();
59+
}
5660
}
5761

5862
@Override

0 commit comments

Comments
 (0)