From 1b3a9c25245d16be013ca3a16cefafefffaf1f69 Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Thu, 22 Oct 2026 07:47:25 -0600 Subject: [PATCH] span: preserve unknown kinds with compatible MessagePack encoding Signed-off-by: Eduardo Silva --- src/ctr_decode_msgpack.c | 5 ++-- src/ctr_decode_opentelemetry.c | 6 ++-- src/ctr_encode_msgpack.c | 13 +++++++-- src/ctr_encode_opentelemetry.c | 22 ++------------- tests/decoding.c | 50 ++++++++++++++++++++++++++++++++++ tests/opentelemetry.c | 12 +++++--- 6 files changed, 76 insertions(+), 32 deletions(-) diff --git a/src/ctr_decode_msgpack.c b/src/ctr_decode_msgpack.c index a77b932..fc3de4a 100644 --- a/src/ctr_decode_msgpack.c +++ b/src/ctr_decode_msgpack.c @@ -512,8 +512,9 @@ static int unpack_span_kind(mpack_reader_t *reader, size_t index, void *ctx) int result; result = ctr_mpack_consume_int32_tag(reader, &kind); - if (result == CTR_MPACK_SUCCESS && ctr_span_kind_set(context->span, kind) != 0) { - return CTR_MPACK_CORRUPT_INPUT_DATA_ERROR; + if (result == CTR_MPACK_SUCCESS) { + /* Match proto3 decoding: preserve unknown received enum values. */ + context->span->kind = kind; } return result; diff --git a/src/ctr_decode_opentelemetry.c b/src/ctr_decode_opentelemetry.c index 60ae7b0..8bb878e 100644 --- a/src/ctr_decode_opentelemetry.c +++ b/src/ctr_decode_opentelemetry.c @@ -774,10 +774,8 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr, ctr_span_set_trace_state(span, otel_span->trace_state, strlen(otel_span->trace_state)); } - if (ctr_span_kind_set(span, otel_span->kind) != 0) { - return decode_opentelemetry_error( - service_request, ctr, CTR_DECODE_OPENTELEMETRY_INVALID_PAYLOAD); - } + /* Preserve unknown proto3 enum values received on the wire. */ + span->kind = otel_span->kind; ctr_span_set_flags(span, otel_span->flags); ctr_span_start_ts(ctr, span, otel_span->start_time_unix_nano); ctr_span_end_ts(ctr, span, otel_span->end_time_unix_nano); diff --git a/src/ctr_encode_msgpack.c b/src/ctr_encode_msgpack.c index b20b3b0..b2bf014 100644 --- a/src/ctr_encode_msgpack.c +++ b/src/ctr_encode_msgpack.c @@ -361,9 +361,18 @@ static void pack_span(mpack_writer_t *writer, struct ctrace_span *span) mpack_write_nil(writer); } - /* kind */ + /* + * kind: decoders before unknown kinds were preserved reject a whole + * context when the value is outside the known range, so unknown kinds + * are stored as unspecified. + */ mpack_write_cstr(writer, "kind"); - mpack_write_u32(writer, span->kind); + if (span->kind >= CTRACE_SPAN_UNSPECIFIED && span->kind <= CTRACE_SPAN_CONSUMER) { + mpack_write_u32(writer, span->kind); + } + else { + mpack_write_u32(writer, CTRACE_SPAN_UNSPECIFIED); + } /* start_time_unix_nano */ mpack_write_cstr(writer, "start_time_unix_nano"); diff --git a/src/ctr_encode_opentelemetry.c b/src/ctr_encode_opentelemetry.c index e56770f..5c6a114 100644 --- a/src/ctr_encode_opentelemetry.c +++ b/src/ctr_encode_opentelemetry.c @@ -658,26 +658,8 @@ static void otel_span_set_parent_span_id(Opentelemetry__Proto__Trace__V1__Span * static void otel_span_set_kind(Opentelemetry__Proto__Trace__V1__Span *otel_span, int kind) { - switch (kind) { - case CTRACE_SPAN_INTERNAL: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_INTERNAL; - break; - case CTRACE_SPAN_SERVER: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_SERVER; - break; - case CTRACE_SPAN_CLIENT: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_CLIENT; - break; - case CTRACE_SPAN_PRODUCER: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_PRODUCER; - break; - case CTRACE_SPAN_CONSUMER: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_CONSUMER; - break; - default: - otel_span->kind = OPENTELEMETRY__PROTO__TRACE__V1__SPAN__SPAN_KIND__SPAN_KIND_UNSPECIFIED; - break; - } + /* CTrace kind values match the OTLP enum. Preserve unknown proto3 values. */ + otel_span->kind = kind; } static void otel_span_set_start_time(Opentelemetry__Proto__Trace__V1__Span *span, diff --git a/tests/decoding.c b/tests/decoding.c index 04239b9..5e7cf9c 100644 --- a/tests/decoding.c +++ b/tests/decoding.c @@ -625,6 +625,55 @@ void test_msgpack_preserves_flags() ctr_destroy(ctx); } +void test_msgpack_stores_unknown_kind_as_unspecified() +{ + char *buffer; + size_t size; + size_t offset; + struct ctrace *ctx; + struct ctrace *decoded; + struct ctrace_resource_span *rs; + struct ctrace_scope_span *ss; + struct ctrace_span *span; + int kinds[] = {-1, INT32_MIN, 0, 5, INT32_MAX}; + size_t index; + + for (index = 0; index < sizeof(kinds) / sizeof(kinds[0]); index++) { + ctx = ctr_create(NULL); + TEST_ASSERT(ctx != NULL); + rs = ctr_resource_span_create(ctx); + TEST_ASSERT(rs != NULL); + ss = ctr_scope_span_create(rs); + TEST_ASSERT(ss != NULL); + span = ctr_span_create(ctx, ss, "unknown-kind", NULL); + TEST_ASSERT(span != NULL); + /* OTLP decoders keep unknown enum values, msgpack stores them as unspecified */ + span->kind = kinds[index]; + TEST_ASSERT(ctr_span_create(ctx, ss, "healthy-sibling", NULL) != NULL); + TEST_ASSERT(ctr_encode_msgpack_create(ctx, &buffer, &size) == 0); + offset = 0; + decoded = NULL; + TEST_CHECK(ctr_decode_msgpack_create(&decoded, buffer, size, &offset) == 0); + if (decoded != NULL) { + rs = cfl_list_entry(decoded->resource_spans.next, + struct ctrace_resource_span, _head); + ss = cfl_list_entry(rs->scope_spans.next, struct ctrace_scope_span, _head); + span = cfl_list_entry(ss->spans.next, struct ctrace_span, _head); + if (kinds[index] >= CTRACE_SPAN_UNSPECIFIED && kinds[index] <= CTRACE_SPAN_CONSUMER) { + TEST_CHECK(span->kind == kinds[index]); + } + else { + TEST_CHECK(span->kind == CTRACE_SPAN_UNSPECIFIED); + } + TEST_CHECK(cfl_list_size(&ss->spans) == 2); + TEST_CHECK(offset == size); + ctr_destroy(decoded); + } + ctr_encode_msgpack_destroy(buffer); + ctr_destroy(ctx); + } +} + void test_msgpack_invalid_offset() { char data[] = {0x80}; @@ -1013,6 +1062,7 @@ void test_simple_to_msgpack_and_back() TEST_LIST = { + {"msgpack_unknown_kind_unspecified", test_msgpack_stores_unknown_kind_as_unspecified}, {"cmt_simple_to_msgpack_and_back", test_simple_to_msgpack_and_back}, {"cmt_msgpack", test_msgpack_to_cmt}, {"empty_spans", test_msgpack_to_ctr_with_empty_spans}, diff --git a/tests/opentelemetry.c b/tests/opentelemetry.c index fee972b..1d8c249 100644 --- a/tests/opentelemetry.c +++ b/tests/opentelemetry.c @@ -670,7 +670,7 @@ void test_otlp_decode_span_empty_name() free(wire); } -void test_otlp_decode_invalid_kind() +void test_otlp_preserves_unknown_kind() { Opentelemetry__Proto__Trace__V1__Span span; uint8_t *wire; @@ -688,8 +688,12 @@ void test_otlp_decode_invalid_kind() offset = 0; decoded = NULL; ret = ctr_decode_opentelemetry_create(&decoded, (char *) wire, wire_len, &offset); - TEST_CHECK(ret != CTR_DECODE_OPENTELEMETRY_SUCCESS); - TEST_CHECK(decoded == NULL); + TEST_ASSERT(ret == CTR_DECODE_OPENTELEMETRY_SUCCESS); + TEST_ASSERT(decoded != NULL); + TEST_CHECK(cfl_list_entry_first(&decoded->span_list, struct ctrace_span, _head_global)->kind == 99); + TEST_CHECK(ctr_span_kind_set(cfl_list_entry_first(&decoded->span_list, + struct ctrace_span, _head_global), 99) == -1); + ctr_destroy(decoded); free(wire); } @@ -774,6 +778,6 @@ TEST_LIST = { {"otlp_decode_attribute_null_value", test_otlp_decode_attribute_null_value}, {"otlp_decode_nested_malformed_kv", test_otlp_decode_nested_malformed_kv}, {"otlp_decode_span_empty_name", test_otlp_decode_span_empty_name}, - {"otlp_decode_invalid_kind", test_otlp_decode_invalid_kind}, + {"otlp_preserves_unknown_kind", test_otlp_preserves_unknown_kind}, { 0 } };