diff --git a/lib/ctraces/include/ctraces/ctr_attributes.h b/lib/ctraces/include/ctraces/ctr_attributes.h index 1b14a4f40b9..e03eca57bf7 100644 --- a/lib/ctraces/include/ctraces/ctr_attributes.h +++ b/lib/ctraces/include/ctraces/ctr_attributes.h @@ -27,10 +27,12 @@ struct ctrace_attributes { struct cfl_kvlist *kv; + unsigned int ref_count; }; struct ctrace_attributes *ctr_attributes_create(); void ctr_attributes_destroy(struct ctrace_attributes *attr); +struct ctrace_attributes *ctr_attributes_acquire(struct ctrace_attributes *attr); int ctr_attributes_count(struct ctrace_attributes *attr); int ctr_attributes_set_string(struct ctrace_attributes *attr, char *key, char *value); int ctr_attributes_set_bool(struct ctrace_attributes *attr, char *key, int b); diff --git a/lib/ctraces/src/ctr_attributes.c b/lib/ctraces/src/ctr_attributes.c index 67caebb88b7..3fdc3381900 100644 --- a/lib/ctraces/src/ctr_attributes.c +++ b/lib/ctraces/src/ctr_attributes.c @@ -35,17 +35,38 @@ struct ctrace_attributes *ctr_attributes_create() return NULL; } + attr->ref_count = 1; + return attr; } void ctr_attributes_destroy(struct ctrace_attributes *attr) { + if (!attr) { + return; + } + + if (attr->ref_count > 1) { + attr->ref_count--; + return; + } + if (attr->kv) { cfl_kvlist_destroy(attr->kv); } free(attr); } +struct ctrace_attributes *ctr_attributes_acquire(struct ctrace_attributes *attr) +{ + if (!attr) { + return NULL; + } + + attr->ref_count++; + return attr; +} + int ctr_attributes_count(struct ctrace_attributes *attr) { return cfl_kvlist_count(attr->kv); diff --git a/plugins/processor_sampling/sampling_tail.c b/plugins/processor_sampling/sampling_tail.c index c2d9fe76e9d..99c90b71456 100644 --- a/plugins/processor_sampling/sampling_tail.c +++ b/plugins/processor_sampling/sampling_tail.c @@ -57,10 +57,6 @@ static struct flb_config_map settings_config_map[] = { {0} }; -static struct cfl_array *copy_array(struct cfl_array *array); -static struct cfl_variant *copy_variant(struct cfl_variant *val); -static struct cfl_kvlist *copy_kvlist(struct cfl_kvlist *kv); - /* delete a list ctrace entry */ static void list_ctrace_delete_entry(struct sampling *ctx, struct sampling_ctrace_entry *ctrace_entry) { @@ -96,176 +92,6 @@ static void list_ctrace_delete_all(struct sampling *ctx, struct sampling_setting } } -static struct cfl_kvlist *copy_kvlist(struct cfl_kvlist *kv) -{ - struct cfl_kvlist *kvlist = NULL; - struct cfl_kvpair *pair; - struct cfl_variant *v; - struct cfl_list *head; - - kvlist = cfl_kvlist_create(); - if (!kvlist) { - return NULL; - } - - cfl_list_foreach(head, &kv->list) { - pair = cfl_list_entry(head, struct cfl_kvpair, _head); - v = copy_variant(pair->val); - if (!v) { - cfl_kvlist_destroy(kvlist); - return NULL; - } - cfl_kvlist_insert(kvlist, pair->key, v); - } - - return kvlist; -} - -static struct cfl_variant *copy_variant(struct cfl_variant *val) -{ - struct cfl_kvlist *kvlist; - struct cfl_array *array; - struct cfl_variant *var = NULL; - - switch (val->type) { - case CFL_VARIANT_STRING: - var = cfl_variant_create_from_string_s(val->data.as_string, - cfl_variant_size_get(val), - CFL_FALSE); - break; - case CFL_VARIANT_BYTES: - var = cfl_variant_create_from_bytes(val->data.as_bytes, - cfl_variant_size_get(val), - CFL_FALSE); - break; - case CFL_VARIANT_BOOL: - var = cfl_variant_create_from_bool(val->data.as_bool); - break; - case CFL_VARIANT_INT: - var = cfl_variant_create_from_int64(val->data.as_int64); - break; - case CFL_VARIANT_UINT: - var = cfl_variant_create_from_uint64(val->data.as_uint64); - break; - case CFL_VARIANT_DOUBLE: - var = cfl_variant_create_from_double(val->data.as_double); - break; - case CFL_VARIANT_NULL: - var = cfl_variant_create_from_null(); - break; - case CFL_VARIANT_ARRAY: - array = copy_array(val->data.as_array); - if (!array) { - return NULL; - } - var = cfl_variant_create_from_array(array); - break; - case CFL_VARIANT_KVLIST: - kvlist = copy_kvlist(val->data.as_kvlist); - if (!kvlist) { - return NULL; - } - var = cfl_variant_create_from_kvlist(kvlist); - break; - default: - var = NULL; - } - - return var; -} - -static struct cfl_array *copy_array(struct cfl_array *array) -{ - int i; - struct cfl_array *copy; - struct cfl_variant *v ; - - copy = cfl_array_create(array->entry_count); - if (!copy) { - return NULL; - } - - for (i = 0; i < array->entry_count; i++) { - v = copy_variant(array->entries[i]); - if (!v) { - cfl_array_destroy(copy); - return NULL; - } - cfl_array_append(copy, v); - } - - return copy; -} - -struct ctrace_attributes *copy_attributes(struct sampling *ctx, struct ctrace_attributes *attr) -{ - int ret = -1; - struct cfl_list *head; - struct cfl_kvpair *pair; - struct cfl_array *array; - struct cfl_kvlist *kvlist; - struct ctrace_attributes *attr_copy; - - attr_copy = ctr_attributes_create(); - if (!attr_copy) { - return NULL; - } - - cfl_list_foreach(head, &attr->kv->list) { - pair = cfl_list_entry(head, struct cfl_kvpair, _head); - - if (pair->val->type == CFL_VARIANT_STRING) { - ret = ctr_attributes_set_string(attr_copy, pair->key, pair->val->data.as_string); - } - else if (pair->val->type == CFL_VARIANT_BOOL) { - ret = ctr_attributes_set_bool(attr_copy, pair->key, pair->val->data.as_bool); - } - else if (pair->val->type == CFL_VARIANT_INT) { - ret = ctr_attributes_set_int64(attr_copy, pair->key, pair->val->data.as_int64); - } - else if (pair->val->type == CFL_VARIANT_DOUBLE) { - ret = ctr_attributes_set_double(attr_copy, pair->key, pair->val->data.as_double); - } - else if (pair->val->type == CFL_VARIANT_ARRAY) { - array = copy_array(pair->val->data.as_array); - if (!array) { - flb_plg_error(ctx->ins, "could not copy array attribute"); - ctr_attributes_destroy(attr_copy); - return NULL; - } - - ret = ctr_attributes_set_array(attr_copy, pair->key, array); - if (ret != 0) { - cfl_array_destroy(array); - } - } - else if (pair->val->type == CFL_VARIANT_KVLIST) { - kvlist = copy_kvlist(pair->val->data.as_kvlist); - if (!kvlist) { - flb_plg_error(ctx->ins, "could not copy kvlist attribute"); - ctr_attributes_destroy(attr_copy); - return NULL; - } - ret = ctr_attributes_set_kvlist(attr_copy, pair->key, kvlist); - if (ret != 0) { - cfl_kvlist_destroy(kvlist); - } - } - else { - flb_plg_error(ctx->ins, "unsupported attribute type %i", pair->val->type); - ctr_attributes_destroy(attr_copy); - return NULL; - } - } - - if (ret != 0) { - ctr_attributes_destroy(attr_copy); - return NULL; - } - - return attr_copy; -}; - static struct ctrace *reconcile_and_create_ctrace(struct sampling *ctx, struct sampling_settings *settings, struct trace_entry *t_entry) { struct cfl_list *tmp; @@ -312,11 +138,9 @@ static struct ctrace *reconcile_and_create_ctrace(struct sampling *ctx, struct s } /* resource attributes */ - if (span->scope_span->resource_span->resource->attr) { - attr = copy_attributes(ctx, span->scope_span->resource_span->resource->attr); - if (attr) { - ctr_resource_set_attributes(resource, attr); - } + attr = span->scope_span->resource_span->resource->attr; + if (attr) { + ctr_resource_set_attributes(resource, ctr_attributes_acquire(attr)); } /* resource dropped attributes count */ @@ -344,7 +168,7 @@ static struct ctrace *reconcile_and_create_ctrace(struct sampling *ctx, struct s if (span->scope_span->instrumentation_scope) { attr = NULL; if (span->scope_span->instrumentation_scope->attr) { - attr = copy_attributes(ctx, span->scope_span->instrumentation_scope->attr); + attr = ctr_attributes_acquire(span->scope_span->instrumentation_scope->attr); } instrumentation_scope = ctr_instrumentation_scope_create(span->scope_span->instrumentation_scope->name,