Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/ctr_decode_msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/ctr_decode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions src/ctr_encode_msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
22 changes: 2 additions & 20 deletions src/ctr_encode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 50 additions & 0 deletions tests/decoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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},
Expand Down
12 changes: 8 additions & 4 deletions tests/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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 }
};
Loading