Skip to content

Commit

Permalink
out_opentelemetry: fixed memory leaks
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Alminana <[email protected]>
  • Loading branch information
leonardo-albertovich authored and edsiper committed Sep 5, 2024
1 parent ea58ab8 commit 4d50c61
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
4 changes: 2 additions & 2 deletions plugins/out_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ static int process_metrics(struct flb_event_chunk *event_chunk,
result = opentelemetry_http_post(ctx, buf, flb_sds_len(buf),
event_chunk->tag,
flb_sds_len(event_chunk->tag),
ctx->metrics_uri);
ctx->metrics_uri_sanitized);

/* Debug http_post() result statuses */
if (result == FLB_OK) {
Expand Down Expand Up @@ -416,7 +416,7 @@ static int process_traces(struct flb_event_chunk *event_chunk,
result = opentelemetry_http_post(ctx, buf, flb_sds_len(buf),
event_chunk->tag,
flb_sds_len(event_chunk->tag),
ctx->traces_uri);
ctx->traces_uri_sanitized);

/* Debug http_post() result statuses */
if (result == FLB_OK) {
Expand Down
4 changes: 4 additions & 0 deletions plugins/out_opentelemetry/opentelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ struct opentelemetry_context {
int proxy_port;

/* HTTP URI */
char *traces_uri_sanitized;
char *metrics_uri_sanitized;
char *logs_uri_sanitized;
char *traces_uri;
char *metrics_uri;
char *logs_uri;

char *host;
int port;

Expand Down
67 changes: 40 additions & 27 deletions plugins/out_opentelemetry/opentelemetry_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,49 @@ struct opentelemetry_context *flb_opentelemetry_context_create(struct flb_output
return NULL;
}

logs_uri = sanitize_uri(ctx->logs_uri);
traces_uri = sanitize_uri(ctx->traces_uri);
metrics_uri = sanitize_uri(ctx->metrics_uri);

ctx->u = upstream;
ctx->host = ins->host.name;
ctx->port = ins->host.port;

ctx->logs_uri_sanitized = sanitize_uri(ctx->logs_uri);
ctx->traces_uri_sanitized = sanitize_uri(ctx->traces_uri);
ctx->metrics_uri_sanitized = sanitize_uri(ctx->metrics_uri);

/* Logs Properties */
if (logs_uri == NULL) {
if (ctx->logs_uri_sanitized == NULL) {
flb_plg_trace(ctx->ins,
"Could not allocate memory for sanitized "
"log endpoint uri");

flb_opentelemetry_context_destroy(ctx);

return NULL;
}
else {
ctx->logs_uri = logs_uri;

if (ctx->traces_uri_sanitized == NULL) {
flb_plg_trace(ctx->ins,
"Could not allocate memory for sanitized "
"trace endpoint uri");

flb_opentelemetry_context_destroy(ctx);

return NULL;
}

if (ctx->metrics_uri_sanitized == NULL) {
flb_plg_trace(ctx->ins,
"Could not allocate memory for sanitized "
"metric endpoint uri");

flb_opentelemetry_context_destroy(ctx);

return NULL;
}

/* list of 'logs_body_key' */
ret = log_body_key_list_create(ctx);
if (ret != 0) {
flb_opentelemetry_context_destroy(ctx);

return NULL;
}

Expand All @@ -364,25 +384,6 @@ struct opentelemetry_context *flb_opentelemetry_context_create(struct flb_output
return NULL;
}

if (traces_uri == NULL) {
flb_plg_trace(ctx->ins,
"Could not allocate memory for sanitized "
"trace endpoint uri");
}
else {
ctx->traces_uri = traces_uri;
}

if (metrics_uri == NULL) {
flb_plg_trace(ctx->ins,
"Could not allocate memory for sanitized "
"metric endpoint uri");
}
else {
ctx->metrics_uri = metrics_uri;
}


/* Set instance flags into upstream */
flb_output_upstream_set(ctx->u, ins);

Expand Down Expand Up @@ -554,6 +555,18 @@ void flb_opentelemetry_context_destroy(struct opentelemetry_context *ctx)
flb_upstream_destroy(ctx->u);
}

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

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

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

/* release log_body_key_list */
log_body_key_list_destroy(ctx);

Expand Down
2 changes: 1 addition & 1 deletion plugins/out_opentelemetry/opentelemetry_logs.c
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ static int logs_flush_to_otel(struct opentelemetry_context *ctx, struct flb_even
ret = opentelemetry_http_post(ctx, body, len,
event_chunk->tag,
flb_sds_len(event_chunk->tag),
ctx->logs_uri);
ctx->logs_uri_sanitized);
flb_free(body);

return ret;
Expand Down

0 comments on commit 4d50c61

Please sign in to comment.