Skip to content

Commit 117ff04

Browse files
committed
lib: ctraces: upgrade to v0.4.0
Signed-off-by: Eduardo Silva <[email protected]>
1 parent d355bfd commit 117ff04

File tree

9 files changed

+73
-25
lines changed

9 files changed

+73
-25
lines changed

lib/ctraces/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ endif()
2626

2727
# CTraces Version
2828
set(CTR_VERSION_MAJOR 0)
29-
set(CTR_VERSION_MINOR 3)
30-
set(CTR_VERSION_PATCH 1)
29+
set(CTR_VERSION_MINOR 4)
30+
set(CTR_VERSION_PATCH 0)
3131
set(CTR_VERSION_STR "${CTR_VERSION_MAJOR}.${CTR_VERSION_MINOR}.${CTR_VERSION_PATCH}")
3232

3333
# Define __FILENAME__ consistently across Operating Systems

lib/ctraces/examples/otlp-encoder/otlp-encoder.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ int main()
107107
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");
108108

109109
/* add a key/value pair list */
110-
kv = cfl_kvlist_create(1);
110+
kv = cfl_kvlist_create();
111111
cfl_kvlist_insert_string(kv, "language", "c");
112112

113113
ctr_span_set_attribute_kvlist(span_root, "my-list", kv);

lib/ctraces/examples/simple-c-api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ int main()
102102
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");
103103

104104
/* add a key/value pair list */
105-
kv = cfl_kvlist_create(1);
105+
kv = cfl_kvlist_create();
106106
cfl_kvlist_insert_string(kv, "language", "c");
107107

108108
ctr_span_set_attribute_kvlist(span_root, "my-list", kv);

lib/ctraces/include/ctraces/ctr_variant_utils.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222

2323
#include <mpack/mpack.h>
2424

25+
#define CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE 100
26+
#define CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE 100
27+
#define CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT 100000
28+
2529
/* These are the only functions meant for general use,
2630
* the reason why the kvlist packing and unpacking
2731
* functions are exposed is the internal and external
@@ -226,12 +230,25 @@ static inline int unpack_cfl_array(mpack_reader_t *reader,
226230

227231
entry_count = mpack_tag_array_count(&tag);
228232

229-
internal_array = cfl_array_create(entry_count);
233+
if (entry_count >= CFL_VARIANT_UTILS_SERIALIZED_ARRAY_SIZE_LIMIT) {
234+
return -2;
235+
}
236+
237+
if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) {
238+
internal_array = cfl_array_create(CFL_VARIANT_UTILS_INITIAL_ARRAY_SIZE);
239+
}
240+
else {
241+
internal_array = cfl_array_create(entry_count);
242+
}
230243

231244
if (internal_array == NULL) {
232245
return -3;
233246
}
234247

248+
if (entry_count >= CFL_VARIANT_UTILS_MAXIMUM_FIXED_ARRAY_SIZE) {
249+
cfl_array_resizable(internal_array, CFL_TRUE);
250+
}
251+
235252
for (index = 0 ; index < entry_count ; index++) {
236253
result = unpack_cfl_variant(reader, &entry_value);
237254

lib/ctraces/src/ctr_attributes.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct ctrace_attributes *ctr_attributes_create()
2929
return NULL;
3030
}
3131

32-
attr->kv = cfl_kvlist_create(128);
32+
attr->kv = cfl_kvlist_create();
3333
if (!attr->kv) {
3434
free(attr);
3535
return NULL;

lib/ctraces/src/ctr_decode_msgpack.c

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ static int unpack_instrumentation_scope_attributes(mpack_reader_t *reader, size_
124124
return CTR_DECODE_MSGPACK_VARIANT_DECODE_ERROR;
125125
}
126126

127+
if (context->scope_span->instrumentation_scope->attr != NULL) {
128+
ctr_attributes_destroy(context->scope_span->instrumentation_scope->attr);
129+
context->scope_span->instrumentation_scope->attr = NULL;
130+
}
131+
127132
context->scope_span->instrumentation_scope->attr = attributes;
128133
}
129134

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

133138
static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_t index, void *ctx)
134139
{
140+
int result;
135141
struct ctrace_instrumentation_scope *instrumentation_scope;
136142
struct ctr_msgpack_decode_context *context = ctx;
137143
struct ctr_mpack_map_entry_callback_t callbacks[] = \
@@ -151,7 +157,12 @@ static int unpack_scope_span_instrumentation_scope(mpack_reader_t *reader, size_
151157

152158
ctr_scope_span_set_instrumentation_scope(context->scope_span, instrumentation_scope);
153159

154-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
160+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
161+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
162+
ctr_instrumentation_scope_destroy(context->scope_span->instrumentation_scope);
163+
context->scope_span->instrumentation_scope = NULL;
164+
}
165+
return result;
155166
}
156167

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

542553
static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
543554
{
555+
int result;
544556
struct ctr_msgpack_decode_context *context = ctx;
545557
struct ctr_mpack_map_entry_callback_t callbacks[] = \
546558
{
@@ -565,8 +577,14 @@ static int unpack_span(mpack_reader_t *reader, size_t index, void *ctx)
565577
if (context->span == NULL) {
566578
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
567579
}
580+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
568581

569-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
582+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
583+
ctr_span_destroy(context->span);
584+
context->span = NULL;
585+
}
586+
587+
return result;
570588
}
571589

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

592610
static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
593611
{
612+
int result;
594613
struct ctr_msgpack_decode_context *context = ctx;
595614
struct ctr_mpack_map_entry_callback_t callbacks[] = \
596615
{
@@ -606,7 +625,12 @@ static int unpack_scope_span(mpack_reader_t *reader, size_t index, void *ctx)
606625
return CTR_DECODE_MSGPACK_ALLOCATION_ERROR;
607626
}
608627

609-
return ctr_mpack_unpack_map(reader, callbacks, ctx);
628+
result = ctr_mpack_unpack_map(reader, callbacks, ctx);
629+
if (result != CTR_DECODE_MSGPACK_SUCCESS) {
630+
ctr_scope_span_destroy(context->scope_span);
631+
context->scope_span = NULL;
632+
}
633+
return result;
610634
}
611635

612636
/* Resource span callbacks */

lib/ctraces/src/ctr_decode_opentelemetry.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,9 @@ int ctr_decode_opentelemetry_create(struct ctrace **out_ctr,
571571
ctr_span_kind_set(span, otel_span->kind);
572572
ctr_span_start_ts(ctr, span, otel_span->start_time_unix_nano);
573573
ctr_span_end_ts(ctr, span, otel_span->end_time_unix_nano);
574-
ctr_span_set_status(span, otel_span->status->code, otel_span->status->message);
574+
if (otel_span->status) {
575+
ctr_span_set_status(span, otel_span->status->code, otel_span->status->message);
576+
}
575577
ctr_span_set_attributes(span, otel_span->n_attributes, otel_span->attributes);
576578
ctr_span_set_events(span, otel_span->n_events, otel_span->events);
577579
ctr_span_set_dropped_attributes_count(span, otel_span->dropped_attributes_count);

lib/ctraces/src/ctr_span.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span
3434

3535
/* allocate a spanc context */
3636
span = calloc(1, sizeof(struct ctrace_span));
37-
if (!span) {
37+
if (span == NULL) {
3838
ctr_errno();
3939
return NULL;
4040
}
@@ -45,14 +45,14 @@ struct ctrace_span *ctr_span_create(struct ctrace *ctx, struct ctrace_scope_span
4545

4646
/* name */
4747
span->name = cfl_sds_create(name);
48-
if (!span->name) {
48+
if (span->name == NULL) {
4949
free(span);
5050
return NULL;
5151
}
5252

5353
/* attributes */
5454
span->attr = ctr_attributes_create();
55-
if (!span->attr) {
55+
if (span->attr == NULL) {
5656
free(span);
5757
return NULL;
5858
}
@@ -116,7 +116,9 @@ int ctr_span_set_span_id(struct ctrace_span *span, void *buf, size_t len)
116116
if (!buf || len <= 0) {
117117
return -1;
118118
}
119-
119+
if (span->span_id != NULL) {
120+
ctr_id_destroy(span->span_id);
121+
}
120122
span->span_id = ctr_id_create(buf, len);
121123
if (!span->span_id) {
122124
return -1;
@@ -294,26 +296,29 @@ void ctr_span_destroy(struct ctrace_span *span)
294296
struct ctrace_span_status *status;
295297
struct ctrace_link *link;
296298

297-
if (span->name) {
299+
if (span->name != NULL) {
298300
cfl_sds_destroy(span->name);
299301
}
300302

301-
if (span->trace_id) {
303+
if (span->trace_id != NULL) {
302304
ctr_id_destroy(span->trace_id);
303305
}
304306

305-
if (span->span_id) {
307+
if (span->span_id != NULL) {
306308
ctr_id_destroy(span->span_id);
307309
}
308310

309-
if (span->parent_span_id) {
311+
if (span->parent_span_id != NULL) {
310312
ctr_id_destroy(span->parent_span_id);
311313
}
312314

313315
/* attributes */
314-
if (span->attr) {
316+
if (span->attr != NULL) {
315317
ctr_attributes_destroy(span->attr);
316318
}
319+
if (span->trace_state != NULL) {
320+
cfl_sds_destroy(span->trace_state);
321+
}
317322

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

330335
/* status */
331336
status = &span->status;
332-
if (status->message) {
337+
if (status->message != NULL) {
333338
cfl_sds_destroy(status->message);
334339
}
335340

@@ -346,21 +351,21 @@ struct ctrace_span_event *ctr_span_event_add_ts(struct ctrace_span *span, char *
346351
{
347352
struct ctrace_span_event *ev;
348353

349-
if (!name) {
354+
if (name == NULL) {
350355
return NULL;
351356
}
352357

353358
ev = calloc(1, sizeof(struct ctrace_span_event));
354-
if (!ev) {
359+
if (ev == NULL) {
355360
ctr_errno();
356361
return NULL;
357362
}
358363
ev->name = cfl_sds_create(name);
359-
if (!ev->name) {
364+
if (ev->name == NULL) {
360365
free(ev);
361366
return NULL;
362367
}
363-
ev->attr = ctr_attributes_create(128);
368+
ev->attr = ctr_attributes_create();
364369
ev->dropped_attr_count = 0;
365370

366371
/* if no timestamp is given, use the current time */

lib/ctraces/tests/decoding.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ void test_simple_to_msgpack_and_back()
581581
ctr_span_event_set_attribute_string(event, "syscall 3", "write()");
582582

583583
/* add a key/value pair list */
584-
kv = cfl_kvlist_create(1);
584+
kv = cfl_kvlist_create();
585585
TEST_ASSERT(kv != NULL);
586586
cfl_kvlist_insert_string(kv, "language", "c");
587587

0 commit comments

Comments
 (0)