Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

out_opentelemetry: metrics handling fixes #9481

Merged
merged 2 commits into from
Oct 11, 2024
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
2 changes: 1 addition & 1 deletion lib/cmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# CMetrics Version
set(CMT_VERSION_MAJOR 0)
set(CMT_VERSION_MINOR 9)
set(CMT_VERSION_PATCH 6)
set(CMT_VERSION_PATCH 7)
set(CMT_VERSION_STR "${CMT_VERSION_MAJOR}.${CMT_VERSION_MINOR}.${CMT_VERSION_PATCH}")

# Include helpers
Expand Down
73 changes: 30 additions & 43 deletions lib/cmetrics/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,21 @@ 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 (is_string_releaseable(metric->schema_url)) {
cfl_sds_destroy(metric->schema_url);
metric->schema_url = NULL;
}
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 +1069,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,20 +1111,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 @@ -1145,12 +1142,11 @@ static Opentelemetry__Proto__Metrics__V1__ScopeMetrics *

if (!error_detection_flag && metadata != NULL) {
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 +1191,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 +1201,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 +2071,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 +2087,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
2 changes: 1 addition & 1 deletion plugins/out_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ static int process_metrics(struct flb_event_chunk *event_chunk,
diff = off;

/* concat buffer */
flb_sds_cat_safe(&buf, encoded_chunk, flb_sds_len(encoded_chunk));
flb_sds_cat_safe(&buf, encoded_chunk, cfl_sds_len(encoded_chunk));

/* release */
cmt_encode_opentelemetry_destroy(encoded_chunk);
Expand Down
6 changes: 3 additions & 3 deletions plugins/out_opentelemetry/opentelemetry_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -555,15 +555,15 @@ void flb_opentelemetry_context_destroy(struct opentelemetry_context *ctx)
flb_upstream_destroy(ctx->u);
}

if (ctx->logs_uri_sanitized != NULL) {
if (ctx->logs_uri_sanitized != NULL && ctx->logs_uri_sanitized != ctx->logs_uri) {
flb_free(ctx->logs_uri_sanitized);
}

if (ctx->traces_uri_sanitized != NULL) {
if (ctx->traces_uri_sanitized != NULL && ctx->traces_uri_sanitized != ctx->traces_uri) {
flb_free(ctx->traces_uri_sanitized);
}

if (ctx->metrics_uri_sanitized != NULL) {
if (ctx->metrics_uri_sanitized != NULL && ctx->metrics_uri_sanitized != ctx->metrics_uri) {
flb_free(ctx->metrics_uri_sanitized);
}

Expand Down
Loading