Skip to content

Commit

Permalink
lib: ctraces: upgrade to v0.4.0
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Dec 20, 2023
1 parent fc7606f commit 956d17a
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 25 deletions.
4 changes: 2 additions & 2 deletions lib/ctraces/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ endif()

# CTraces Version
set(CTR_VERSION_MAJOR 0)
set(CTR_VERSION_MINOR 3)
set(CTR_VERSION_PATCH 1)
set(CTR_VERSION_MINOR 4)
set(CTR_VERSION_PATCH 0)
set(CTR_VERSION_STR "${CTR_VERSION_MAJOR}.${CTR_VERSION_MINOR}.${CTR_VERSION_PATCH}")

# Define __FILENAME__ consistently across Operating Systems
Expand Down
2 changes: 1 addition & 1 deletion lib/ctraces/examples/otlp-encoder/otlp-encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ int main()
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");

/* add a key/value pair list */
kv = cfl_kvlist_create(1);
kv = cfl_kvlist_create();
cfl_kvlist_insert_string(kv, "language", "c");

ctr_span_set_attribute_kvlist(span_root, "my-list", kv);
Expand Down
2 changes: 1 addition & 1 deletion lib/ctraces/examples/simple-c-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ int main()
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");

/* add a key/value pair list */
kv = cfl_kvlist_create(1);
kv = cfl_kvlist_create();
cfl_kvlist_insert_string(kv, "language", "c");

ctr_span_set_attribute_kvlist(span_root, "my-list", kv);
Expand Down
19 changes: 18 additions & 1 deletion lib/ctraces/include/ctraces/ctr_variant_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@

#include <mpack/mpack.h>

#define CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE 100
#define CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE 100
#define CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT 100000

/* These are the only functions meant for general use,
* the reason why the kvlist packing and unpacking
* functions are exposed is the internal and external
Expand Down Expand Up @@ -226,12 +230,25 @@ static inline int unpack_cfl_array(mpack_reader_t *reader,

entry_count = mpack_tag_array_count(&tag);

internal_array = cfl_array_create(entry_count);
if (entry_count >= CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT) {
return -2;
}

if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) {
internal_array = cfl_array_create(CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE);
}
else {
internal_array = cfl_array_create(entry_count);
}

if (internal_array == NULL) {
return -3;
}

if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) {
cfl_array_resizable(internal_array, CFL_TRUE);
}

for (index = 0 ; index < entry_count ; index++) {
result = unpack_cfl_variant(reader, &entry_value);

Expand Down
2 changes: 1 addition & 1 deletion lib/ctraces/src/ctr_attributes.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct ctrace_attributes *ctr_attributes_create()
return NULL;
}

attr->kv = cfl_kvlist_create(128);
attr->kv = cfl_kvlist_create();
if (!attr->kv) {
free(attr);
return NULL;
Expand Down
30 changes: 27 additions & 3 deletions lib/ctraces/src/ctr_decode_msgpack.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_
return CTR_DECODE_MSGPACK_VARIANT_DECODE_ERROR;
}

if (context->scope_span->instrumentation_scope->attr != NULL) {
ctr_attributes_destroy(context->scope_span->instrumentation_scope->attr);
context->scope_span->instrumentation_scope->attr = NULL;
}

context->scope_span->instrumentation_scope->attr = attributes;
}

Expand All @@ -132,6 +137,7 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_

static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_t index, void *ctx)
{
int result;
struct ctrace_instrumentation_scope *instrumentation_scope;
struct ctr_msgpack_decode_context *context = ctx;
struct ctr_mpack_map_entry_callback_t callbacks[] = \
Expand All @@ -151,7 +157,12 @@ static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_

ctr_scope_span_set_instrumentation_scope(context->scope_span, instrumentation_scope);

return ctr_mpack_unpack_map(reader, callbacks, ctx);
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
ctr_instrumentation_scope_destroy(context->scope_span->instrumentation_scope);
context->scope_span->instrumentation_scope = NULL;
}
return result;
}

/* Event callbacks */
Expand Down Expand Up @@ -541,6 +552,7 @@ static int unpack_span_status(mpack_reader_t *reader, size_t index, void *ctx)

static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
{
int result;
struct ctr_msgpack_decode_context *context = ctx;
struct ctr_mpack_map_entry_callback_t callbacks[] = \
{
Expand All @@ -565,8 +577,14 @@ static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
if (context->span == NULL) {
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
}
result = ctr_mpack_unpack_map(reader, callbacks, ctx);

return ctr_mpack_unpack_map(reader, callbacks, ctx);
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
ctr_span_destroy(context->span);
context->span = NULL;
}

return result;
}

/* Scope span callbacks */
Expand All @@ -591,6 +609,7 @@ static int unpack_scope_span_schema_url(mpack_reader_t *reader, size_t index, vo

static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
{
int result;
struct ctr_msgpack_decode_context *context = ctx;
struct ctr_mpack_map_entry_callback_t callbacks[] = \
{
Expand All @@ -606,7 +625,12 @@ static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
}

return ctr_mpack_unpack_map(reader, callbacks, ctx);
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
ctr_scope_span_destroy(context->scope_span);
context->scope_span = NULL;
}
return result;
}

/* Resource span callbacks */
Expand Down
4 changes: 3 additions & 1 deletion lib/ctraces/src/ctr_decode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,9 @@ 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);
ctr_span_set_status(span, otel_span->status->code, otel_span->status->message);
if (otel_span->status) {
ctr_span_set_status(span, otel_span->status->code, otel_span->status->message);
}
ctr_span_set_attributes(span, otel_span->n_attributes, otel_span->attributes);
ctr_span_set_events(span, otel_span->n_events, otel_span->events);
ctr_span_set_dropped_attributes_count(span, otel_span->dropped_attributes_count);
Expand Down
33 changes: 19 additions & 14 deletions lib/ctraces/src/ctr_span.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +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) {
if (span == NULL) {
ctr_errno();
return NULL;
}
Expand All @@ -45,14 +45,14 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span

/* name */
span->name = cfl_sds_create(name);
if (!span->name) {
if (span->name == NULL) {
free(span);
return NULL;
}

/* attributes */
span->attr = ctr_attributes_create();
if (!span->attr) {
if (span->attr == NULL) {
free(span);
return NULL;
}
Expand Down Expand Up @@ -116,7 +116,9 @@ int ctr_span_set_span_id(struct ctrace_span *span, void *buf, size_t len)
if (!buf || len <= 0) {
return -1;
}

if (span->span_id != NULL) {
ctr_id_destroy(span->span_id);
}
span->span_id = ctr_id_create(buf, len);
if (!span->span_id) {
return -1;
Expand Down Expand Up @@ -294,26 +296,29 @@ void ctr_span_destroy(struct ctrace_span *span)
struct ctrace_span_status *status;
struct ctrace_link *link;

if (span->name) {
if (span->name != NULL) {
cfl_sds_destroy(span->name);
}

if (span->trace_id) {
if (span->trace_id != NULL) {
ctr_id_destroy(span->trace_id);
}

if (span->span_id) {
if (span->span_id != NULL) {
ctr_id_destroy(span->span_id);
}

if (span->parent_span_id) {
if (span->parent_span_id != NULL) {
ctr_id_destroy(span->parent_span_id);
}

/* attributes */
if (span->attr) {
if (span->attr != NULL) {
ctr_attributes_destroy(span->attr);
}
if (span->trace_state != NULL) {
cfl_sds_destroy(span->trace_state);
}

/* events */
cfl_list_foreach_safe(head, tmp, &span->events) {
Expand All @@ -329,7 +334,7 @@ void ctr_span_destroy(struct ctrace_span *span)

/* status */
status = &span->status;
if (status->message) {
if (status->message != NULL) {
cfl_sds_destroy(status->message);
}

Expand All @@ -346,21 +351,21 @@ struct ctrace_span_event *ctr_span_event_add_ts(struct ctrace_span *span, char *
{
struct ctrace_span_event *ev;

if (!name) {
if (name == NULL) {
return NULL;
}

ev = calloc(1, sizeof(struct ctrace_span_event));
if (!ev) {
if (ev == NULL) {
ctr_errno();
return NULL;
}
ev->name = cfl_sds_create(name);
if (!ev->name) {
if (ev->name == NULL) {
free(ev);
return NULL;
}
ev->attr = ctr_attributes_create(128);
ev->attr = ctr_attributes_create();
ev->dropped_attr_count = 0;

/* if no timestamp is given, use the current time */
Expand Down
2 changes: 1 addition & 1 deletion lib/ctraces/tests/decoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void test_simple_to_msgpack_and_back()
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");

/* add a key/value pair list */
kv = cfl_kvlist_create(1);
kv = cfl_kvlist_create();
TEST_ASSERT(kv != NULL);
cfl_kvlist_insert_string(kv, "language", "c");

Expand Down

0 comments on commit 956d17a

Please sign in to comment.