Skip to content

Commit

Permalink
encoding: opentelemetry: fix leaks on resource and scope handling
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Oct 10, 2024
1 parent 4651386 commit 1fe97e4
Showing 1 changed file with 28 additions and 42 deletions.
70 changes: 28 additions & 42 deletions src/cmt_encode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics **
static void destroy_scope_metric_list(
Opentelemetry__Proto__Metrics__V1__ScopeMetrics **metric_list);

struct cfl_kvlist *fetch_metadata_kvlist_key(
struct cfl_kvlist *kvlist, char *key)
struct cfl_kvlist *fetch_metadata_kvlist_key(struct cfl_kvlist *kvlist, char *key)
{
struct cfl_variant *entry_variant;
struct cfl_kvlist *entry_kvlist;
Expand Down Expand Up @@ -1015,16 +1014,6 @@ static Opentelemetry__Proto__Metrics__V1__ResourceMetrics **
return metric_list;
}

static void destroy_scope_metrics(
Opentelemetry__Proto__Metrics__V1__ScopeMetrics *metric)
{
if (metric != NULL) {
destroy_metric_list(metric->metrics);

free(metric);
}
}

void destroy_instrumentation_scope(Opentelemetry__Proto__Common__V1__InstrumentationScope *scope)
{
if (scope->name != NULL) {
Expand All @@ -1042,6 +1031,20 @@ void destroy_instrumentation_scope(Opentelemetry__Proto__Common__V1__Instrumenta
free(scope);
}

static void destroy_scope_metrics(Opentelemetry__Proto__Metrics__V1__ScopeMetrics *metric)
{
if (metric != NULL) {
if (metric->schema_url != NULL) {
cfl_sds_destroy(metric->schema_url);
}
if (metric->scope != NULL) {
destroy_instrumentation_scope(metric->scope);
}
destroy_metric_list(metric->metrics);
free(metric);
}
}

static Opentelemetry__Proto__Common__V1__InstrumentationScope *
initialize_instrumentation_scope(
struct cfl_kvlist *scope_root,
Expand All @@ -1065,9 +1068,7 @@ static Opentelemetry__Proto__Common__V1__InstrumentationScope *
return NULL;
}

scope = \
calloc(1, sizeof(Opentelemetry__Proto__Common__V1__InstrumentationScope));

scope = calloc(1, sizeof(Opentelemetry__Proto__Common__V1__InstrumentationScope));
if (scope == NULL) {
*error_detection_flag = CMT_TRUE;

Expand Down Expand Up @@ -1109,19 +1110,17 @@ static Opentelemetry__Proto__Common__V1__InstrumentationScope *
return scope;
}

static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *
initialize_scope_metrics(
struct cfl_kvlist *scope_metrics_root,
size_t metric_element_count)
static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *initialize_scope_metrics(
struct cfl_kvlist *scope_metrics_root,
size_t metric_element_count)
{
int error_detection_flag;
Opentelemetry__Proto__Metrics__V1__ScopeMetrics *scope_metrics;
struct cfl_kvlist *metadata;

metadata = fetch_metadata_kvlist_key(scope_metrics_root, "metadata");

scope_metrics = \
calloc(1, sizeof(Opentelemetry__Proto__Metrics__V1__ScopeMetrics));
scope_metrics = calloc(1, sizeof(Opentelemetry__Proto__Metrics__V1__ScopeMetrics));

if (scope_metrics == NULL) {
return NULL;
Expand All @@ -1132,9 +1131,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *
error_detection_flag = CMT_FALSE;

if (metric_element_count > 0) {
scope_metrics->metrics = \
initialize_metric_list(metric_element_count);

scope_metrics->metrics = initialize_metric_list(metric_element_count);
if (scope_metrics->metrics == NULL) {
error_detection_flag = CMT_TRUE;
}
Expand All @@ -1147,10 +1144,8 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *
scope_metrics->schema_url = fetch_metadata_string_key(metadata, "schema_url", &error_detection_flag);
}

if (error_detection_flag &&
scope_metrics != NULL) {
if (error_detection_flag && scope_metrics != NULL) {
destroy_scope_metrics(scope_metrics);

scope_metrics = NULL;
}

Expand Down Expand Up @@ -1195,9 +1190,7 @@ static void destroy_scope_metric_list(
}
}

static Opentelemetry__Proto__Metrics__V1__ScopeMetrics **
initialize_scope_metrics_list(
size_t element_count)
static Opentelemetry__Proto__Metrics__V1__ScopeMetrics **initialize_scope_metrics_list(size_t element_count)
{
Opentelemetry__Proto__Metrics__V1__ScopeMetrics **metric_list;

Expand All @@ -1207,8 +1200,7 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics **
return metric_list;
}

static void destroy_attribute(
Opentelemetry__Proto__Common__V1__KeyValue *attribute)
static void destroy_attribute(Opentelemetry__Proto__Common__V1__KeyValue *attribute)
{
if (attribute != NULL) {
if (attribute->value != NULL) {
Expand Down Expand Up @@ -2078,10 +2070,8 @@ static void destroy_opentelemetry_context(
}
}

static Opentelemetry__Proto__Resource__V1__Resource *
initialize_resource(
struct cfl_kvlist *resource_root,
int *error_detection_flag)
static Opentelemetry__Proto__Resource__V1__Resource *initialize_resource(struct cfl_kvlist *resource_root,
int *error_detection_flag)
{
struct cfl_kvlist *attributes;
struct cfl_kvlist *metadata;
Expand All @@ -2096,17 +2086,13 @@ static Opentelemetry__Proto__Resource__V1__Resource *
attributes = fetch_metadata_kvlist_key(resource_root, "attributes");
metadata = fetch_metadata_kvlist_key(resource_root, "metadata");

if (cfl_kvlist_count(attributes) == 0 &&
cfl_kvlist_count(metadata) == 0) {
if (cfl_kvlist_count(attributes) == 0 && cfl_kvlist_count(metadata) == 0) {
return NULL;
}

resource = \
calloc(1, sizeof(Opentelemetry__Proto__Resource__V1__Resource));

resource = calloc(1, sizeof(Opentelemetry__Proto__Resource__V1__Resource));
if (resource == NULL) {
*error_detection_flag = CMT_TRUE;

return NULL;
}

Expand Down

0 comments on commit 1fe97e4

Please sign in to comment.