Skip to content

Commit 22f72d6

Browse files
committed
fix(exporters): always pad Zipkin trace_id to 8 bytes
Previously, when the input trace_id was empty or non-hex, hex_to_bytes returned an empty vector and the encoder emitted no trace_id field. Decoded spans then had a zero-length trace_id, which Zipkin rejects. Mirror the Jaeger behavior: if the decoded hex is not exactly 16 bytes we left-pad to 8 bytes, producing a valid on-wire ID in all cases.
1 parent 51171a7 commit 22f72d6

1 file changed

Lines changed: 8 additions & 7 deletions

File tree

include/kcenon/monitoring/exporters/trace_exporters.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,12 @@ struct zipkin_span_data {
327327
zipkin_proto::span out;
328328
auto trace_bytes = protobuf_wire::hex_to_bytes(trace_id);
329329
// Zipkin accepts 8 or 16 byte trace IDs; normalize the common
330-
// shorter cases by padding to 8 bytes.
331-
if (!trace_bytes.empty() && trace_bytes.size() != 16) {
332-
out.trace_id = protobuf_wire::left_pad(trace_bytes, 8);
330+
// shorter/empty/non-hex inputs to 8 bytes so the wire always carries
331+
// a valid ID (Zipkin rejects spans without a trace ID).
332+
if (trace_bytes.size() == 16) {
333+
out.trace_id = std::move(trace_bytes);
333334
} else {
334-
out.trace_id = trace_bytes;
335+
out.trace_id = protobuf_wire::left_pad(trace_bytes, 8);
335336
}
336337
out.id = protobuf_wire::left_pad(
337338
protobuf_wire::hex_to_bytes(span_id), 8);
@@ -365,10 +366,10 @@ inline std::vector<uint8_t> encode_zipkin_list_of_spans(
365366
for (const auto& s : spans) {
366367
zipkin_proto::span ps;
367368
auto trace_bytes = protobuf_wire::hex_to_bytes(s.trace_id);
368-
if (!trace_bytes.empty() && trace_bytes.size() != 16) {
369-
ps.trace_id = protobuf_wire::left_pad(trace_bytes, 8);
369+
if (trace_bytes.size() == 16) {
370+
ps.trace_id = std::move(trace_bytes);
370371
} else {
371-
ps.trace_id = trace_bytes;
372+
ps.trace_id = protobuf_wire::left_pad(trace_bytes, 8);
372373
}
373374
ps.id = protobuf_wire::left_pad(
374375
protobuf_wire::hex_to_bytes(s.span_id), 8);

0 commit comments

Comments
 (0)