From a7d106bc92b233b342ba81954d2f4d840389452c Mon Sep 17 00:00:00 2001 From: Florian Bezannier Date: Tue, 18 Oct 2022 15:05:00 +0200 Subject: [PATCH] out_opentelemetry: add gzip compression --- build/.empty | 0 plugins/out_opentelemetry/opentelemetry.c | 28 +++++++++++++++++-- plugins/out_opentelemetry/opentelemetry.h | 3 ++ .../out_opentelemetry/opentelemetry_conf.c | 9 ++++++ 4 files changed, 37 insertions(+), 3 deletions(-) delete mode 100644 build/.empty diff --git a/build/.empty b/build/.empty deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/plugins/out_opentelemetry/opentelemetry.c b/plugins/out_opentelemetry/opentelemetry.c index bfeceddac1c..738be674176 100644 --- a/plugins/out_opentelemetry/opentelemetry.c +++ b/plugins/out_opentelemetry/opentelemetry.c @@ -28,6 +28,7 @@ #include #include +#include #include #include @@ -55,6 +56,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; @@ -64,10 +68,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); @@ -107,7 +122,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) { /* @@ -606,6 +623,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 81693bb5e65..2c177145b96 100644 --- a/plugins/out_opentelemetry/opentelemetry.h +++ b/plugins/out_opentelemetry/opentelemetry.h @@ -62,6 +62,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 ff928b60622..060f9f94bec 100644 --- a/plugins/out_opentelemetry/opentelemetry_conf.c +++ b/plugins/out_opentelemetry/opentelemetry_conf.c @@ -141,6 +141,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)); @@ -214,6 +215,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; }