diff --git a/plugins/out_opentelemetry/opentelemetry.c b/plugins/out_opentelemetry/opentelemetry.c index 3ea956a02e6..f5d3060d64b 100644 --- a/plugins/out_opentelemetry/opentelemetry.c +++ b/plugins/out_opentelemetry/opentelemetry.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -140,6 +141,9 @@ static int http_post(struct opentelemetry_context *ctx, struct flb_config_map_val *mv; struct flb_slist_entry *key = NULL; struct flb_slist_entry *val = NULL; + void *final_body = NULL; + size_t final_body_len = 0; + int compressed = FLB_FALSE; /* Get upstream context and connection */ u = ctx->u; @@ -149,10 +153,21 @@ static int http_post(struct opentelemetry_context *ctx, u->tcp_host, u->tcp_port); return FLB_RETRY; } - + if (ctx->compress_gzip == FLB_TRUE) { + ret = flb_gzip_compress((void *) body, body_len, + &final_body, &final_body_len); + if (ret == -1) { + flb_plg_error(ctx->ins, "cannot gzip payload, disabling compression"); + } else { + compressed = FLB_TRUE; + } + } else { + final_body = body; + final_body_len = body_len; + } /* Create HTTP client context */ c = flb_http_client(u_conn, FLB_HTTP_POST, uri, - body, body_len, + final_body, final_body_len, ctx->host, ctx->port, ctx->proxy, 0); @@ -192,7 +207,9 @@ static int http_post(struct opentelemetry_context *ctx, key->str, flb_sds_len(key->str), val->str, flb_sds_len(val->str)); } - + if (compressed == FLB_TRUE) { + flb_http_set_content_encoding_gzip(c); + } ret = flb_http_do(c, &b_sent); if (ret == 0) { /* @@ -239,6 +256,13 @@ static int http_post(struct opentelemetry_context *ctx, out_ret = FLB_RETRY; } + /* + * If the payload buffer is different than incoming records in body, means + * we generated a different payload and must be freed. + */ + if (final_body != body) { + flb_free(final_body); + } /* Destroy HTTP client context */ flb_http_client_destroy(c); @@ -1022,6 +1046,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct opentelemetry_context, log_response_payload), "Specify if the response paylod should be logged or not" }, + { + FLB_CONFIG_MAP_STR, "compress", NULL, + 0, FLB_FALSE, 0, + "Set payload compression mechanism. Option available is 'gzip'" + }, /* EOF */ {0} }; diff --git a/plugins/out_opentelemetry/opentelemetry.h b/plugins/out_opentelemetry/opentelemetry.h index 08275bdc066..97c3040de27 100644 --- a/plugins/out_opentelemetry/opentelemetry.h +++ b/plugins/out_opentelemetry/opentelemetry.h @@ -69,6 +69,9 @@ struct opentelemetry_context { /* instance context */ struct flb_output_instance *ins; + + /* Compression mode (gzip) */ + int compress_gzip; }; #endif diff --git a/plugins/out_opentelemetry/opentelemetry_conf.c b/plugins/out_opentelemetry/opentelemetry_conf.c index 91097ef4f5c..ec989465b4e 100644 --- a/plugins/out_opentelemetry/opentelemetry_conf.c +++ b/plugins/out_opentelemetry/opentelemetry_conf.c @@ -132,6 +132,7 @@ struct opentelemetry_context *flb_opentelemetry_context_create( char *logs_uri = NULL; struct flb_upstream *upstream; struct opentelemetry_context *ctx = NULL; + char *tmp = NULL; /* Allocate plugin context */ ctx = flb_calloc(1, sizeof(struct opentelemetry_context)); @@ -232,6 +233,14 @@ struct opentelemetry_context *flb_opentelemetry_context_create( /* Set instance flags into upstream */ flb_output_upstream_set(ctx->u, ins); + tmp = flb_output_get_property("compress", ins); + ctx->compress_gzip = FLB_FALSE; + if (tmp) { + if (strcasecmp(tmp, "gzip") == 0) { + ctx->compress_gzip = FLB_TRUE; + } + } + return ctx; }