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
8 changes: 8 additions & 0 deletions include/ctraces/ctr_decode_opentelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@
#ifndef CTR_DECODE_OPENTELEMETRY_H
#define CTR_DECODE_OPENTELEMETRY_H

#define CTR_DECODE_OPENTELEMETRY_SUCCESS 0
#define CTR_DECODE_OPENTELEMETRY_INSUFFICIENT_DATA -1
#define CTR_DECODE_OPENTELEMETRY_INVALID_ARGUMENT -2
#define CTR_DECODE_OPENTELEMETRY_CORRUPTED_DATA -3
#define CTR_DECODE_OPENTELEMETRY_INVALID_PAYLOAD -4
#define CTR_DECODE_OPENTELEMETRY_ALLOCATION_ERROR -5


typedef enum {
CTR_OPENTELEMETRY_TYPE_ATTRIBUTE = 0,
CTR_OPENTELEMETRY_TYPE_ARRAY = 1,
Expand Down
39 changes: 34 additions & 5 deletions src/ctr_decode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,11 +525,15 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,
Opentelemetry__Proto__Trace__V1__ScopeSpans *otel_scope_span;
Opentelemetry__Proto__Trace__V1__Span *otel_span;

if (*offset >= in_size) {
return CTR_DECODE_OPENTELEMETRY_INSUFFICIENT_DATA;
}

service_request = opentelemetry__proto__collector__trace__v1__export_trace_service_request__unpack(NULL,
in_size - *offset,
(unsigned char *) &in_buf[*offset]);
if (service_request == NULL) {
return -1;
return CTR_DECODE_OPENTELEMETRY_CORRUPTED_DATA;
}

ctr = ctr_create(NULL);
Expand All @@ -539,7 +543,7 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,
if (otel_resource_span == NULL) {
opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);
ctr_destroy(ctr);
return -1;
return CTR_DECODE_OPENTELEMETRY_INVALID_PAYLOAD;
}

/* resource span */
Expand All @@ -554,12 +558,23 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,

for (scope_span_index = 0; scope_span_index < otel_resource_span->n_scope_spans; scope_span_index++) {
otel_scope_span = otel_resource_span->scope_spans[scope_span_index];

if (otel_scope_span == NULL) {
opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);
return -1;
ctr_destroy(ctr);

return CTR_DECODE_OPENTELEMETRY_INVALID_PAYLOAD;
}

scope_span = ctr_scope_span_create(resource_span);

if (scope_span == NULL) {
opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);
ctr_destroy(ctr);

return CTR_DECODE_OPENTELEMETRY_ALLOCATION_ERROR;
}

ctr_scope_span_set_schema_url(scope_span, otel_scope_span->schema_url);

if (otel_scope_span->scope != NULL) {
Expand All @@ -568,13 +583,23 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,

for (span_index = 0; span_index < otel_scope_span->n_spans; span_index++) {
otel_span = otel_scope_span->spans[span_index];

if (otel_span == NULL) {
opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);
return -1;
ctr_destroy(ctr);

return CTR_DECODE_OPENTELEMETRY_INVALID_PAYLOAD;
}

span = ctr_span_create(ctr, scope_span, otel_span->name, NULL);

if (span == NULL) {
opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);
ctr_destroy(ctr);

return CTR_DECODE_OPENTELEMETRY_ALLOCATION_ERROR;
}

/* copy data from otel span to ctraces span representation */
ctr_span_set_trace_id(span, otel_span->trace_id.data, otel_span->trace_id.len);
ctr_span_set_span_id(span, otel_span->span_id.data, otel_span->span_id.len);
Expand All @@ -587,6 +612,7 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,
ctr_span_kind_set(span, otel_span->kind);
ctr_span_start_ts(ctr, span, otel_span->start_time_unix_nano);
ctr_span_end_ts(ctr, span, otel_span->end_time_unix_nano);

if (otel_span->status) {
ctr_span_set_status(span, otel_span->status->code, otel_span->status->message);
}
Expand All @@ -603,10 +629,13 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,
}
}

*offset += opentelemetry__proto__collector__trace__v1__export_trace_service_request__get_packed_size(service_request);

opentelemetry__proto__collector__trace__v1__export_trace_service_request__free_unpacked(service_request, NULL);

*out_ctr = ctr;

return 0;
return CTR_DECODE_OPENTELEMETRY_SUCCESS;
}

void ctr_decode_opentelemetry_destroy(struct ctrace *ctr)
Expand Down
8 changes: 7 additions & 1 deletion src/ctr_span.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span

/* allocate a spanc context */
span = calloc(1, sizeof(struct ctrace_span));

if (span == NULL) {
ctr_errno();
return NULL;
Expand All @@ -47,23 +48,27 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span
span->name = cfl_sds_create(name);
if (span->name == NULL) {
free(span);

return NULL;
}

/* attributes */
span->attr = ctr_attributes_create();
if (span->attr == NULL) {
cfl_sds_destroy(span->name);
free(span);

return NULL;
}

cfl_list_init(&span->events);
cfl_list_init(&span->links);

/* dropped attributes count */
span->dropped_attr_count = 0;

/* if a parent context was given, populate the span parent id */
if (parent && parent->span_id) {
if (parent != NULL && parent->span_id != NULL) {
ctr_span_set_parent_span_id_with_cid(span, parent->span_id);
}

Expand All @@ -78,6 +83,7 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span

/* always start a span by default, the start can be overriden later if needed */
ctr_span_start(ctx, span);

return span;
}

Expand Down
Loading