Skip to content

Commit

Permalink
encode_opentelemetry: Handle minimal otel payloads
Browse files Browse the repository at this point in the history
Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
  • Loading branch information
cosmo0920 committed Sep 24, 2024
1 parent cae1e69 commit d69ad8b
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/ctr_encode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -917,11 +917,21 @@ static Opentelemetry__Proto__Common__V1__InstrumentationScope *set_instrumentati
return NULL;
}

otel_scope->name = instrumentation_scope->name;
otel_scope->version = instrumentation_scope->version;
if (instrumentation_scope->name) {
otel_scope->name = instrumentation_scope->name;
}
else {
otel_scope->name = "";
}
if (instrumentation_scope->version) {
otel_scope->version = instrumentation_scope->version;
}
else {
otel_scope->version = "";
}
otel_scope->n_attributes = get_attributes_count(instrumentation_scope->attr);
otel_scope->dropped_attributes_count = instrumentation_scope->dropped_attr_count;
otel_scope->attributes = set_attributes_from_ctr(instrumentation_scope->attr);;
otel_scope->attributes = set_attributes_from_ctr(instrumentation_scope->attr);

return otel_scope;
}
Expand Down Expand Up @@ -989,7 +999,9 @@ static Opentelemetry__Proto__Trace__V1__ScopeSpans **set_scope_spans(struct ctra
}

otel_scope_span->schema_url = scope_span->schema_url;
otel_scope_span->scope = set_instrumentation_scope(scope_span->instrumentation_scope);
if (scope_span->instrumentation_scope != NULL) {
otel_scope_span->scope = set_instrumentation_scope(scope_span->instrumentation_scope);
}

span_count = cfl_list_size(&scope_span->spans);
otel_scope_span->n_spans = span_count;
Expand Down
93 changes: 93 additions & 0 deletions tests/decoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,81 @@ static struct ctrace *generate_encoder_test_data()
return context;
}

static int generate_sample_resource_minimal_attributes(struct ctrace_resource *resource)
{
struct ctrace_attributes *attributes;
int result;

attributes = ctr_attributes_create();

if (attributes == NULL) {
return -1;
}

result = ctr_attributes_set_string(attributes, "receiver.tool", "ctraces");

if (result != 0) {
ctr_attributes_destroy(attributes);

return -2;
}

result = ctr_resource_set_attributes(resource, attributes);

if (result != 0) {
ctr_attributes_destroy(attributes);

return -3;
}

return 0;
}

static struct ctrace *generate_encoder_test_data_with_empty_spans()
{
struct ctrace_resource_span *resource_span;
struct ctrace_scope_span *scope_span;
struct ctrace *context;
int result;

context = ctr_create(NULL);

if (context == NULL) {
return NULL;
}

resource_span = ctr_resource_span_create(context);

if (resource_span == NULL) {
ctr_destroy(context);

return NULL;
}

ctr_resource_span_set_schema_url(resource_span, "");
ctr_resource_set_dropped_attr_count(resource_span->resource, 0);

result = generate_sample_resource_minimal_attributes(resource_span->resource);

if (result != 0) {
ctr_destroy(context);

return NULL;
}

scope_span = ctr_scope_span_create(resource_span);

if (scope_span == NULL) {
ctr_destroy(context);

return NULL;
}

ctr_scope_span_set_schema_url(scope_span, "");

return context;
}

/*
* perform the following and then compare text buffers
*
Expand Down Expand Up @@ -483,6 +558,23 @@ void test_msgpack_to_cmt()
ctr_destroy(context);
}

void test_msgpack_to_ctr_with_empty_spans()
{
struct ctrace *context;
char *referece_text_buffer;

context = generate_encoder_test_data_with_empty_spans();
TEST_ASSERT(context != NULL);

referece_text_buffer = ctr_encode_text_create(context);
TEST_ASSERT(referece_text_buffer != NULL);

printf("%s\n", referece_text_buffer);
msgpack_encode_decode_and_compare(context);

ctr_destroy(context);
}

void test_simple_to_msgpack_and_back()
{
struct ctrace *ctx;
Expand Down Expand Up @@ -640,5 +732,6 @@ void test_simple_to_msgpack_and_back()
TEST_LIST = {
{"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},
{ 0 }
};

0 comments on commit d69ad8b

Please sign in to comment.