From 23f529476634f34379b7830ba92a797d563971d4 Mon Sep 17 00:00:00 2001 From: Alan Richards Date: Tue, 26 Sep 2023 21:04:04 +0000 Subject: [PATCH] out_loki: add ability to add arbitrary http headers and adjust the URI path Following pattern from prometheus_remote_write, this adds the option to add arbitrary http headers. It also adds the ability to manually set the URI path. Previously it was hardcoded as `/loki/api/v1/push`. Existing behaviour is maintained and this is an optional override. Tested against a Loki endpoint and logging reverse proxy. Signed-off-by: Alan Richards --- plugins/out_loki/loki.c | 36 +++++++++++++++++++++++++++++++++++- plugins/out_loki/loki.h | 4 ++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/plugins/out_loki/loki.c b/plugins/out_loki/loki.c index d93a3f9aad6..ab9c4aae8a9 100644 --- a/plugins/out_loki/loki.c +++ b/plugins/out_loki/loki.c @@ -1515,6 +1515,11 @@ static void cb_loki_flush(struct flb_event_chunk *event_chunk, struct flb_connection *u_conn; struct flb_http_client *c; struct flb_loki_dynamic_tenant_id_entry *dynamic_tenant_id; + struct mk_list *head; + struct flb_config_map_val *mv; + struct flb_slist_entry *key = NULL; + struct flb_slist_entry *val = NULL; + char * uri; dynamic_tenant_id = FLB_TLS_GET(thread_local_tenant_id); @@ -1577,8 +1582,15 @@ static void cb_loki_flush(struct flb_event_chunk *event_chunk, FLB_OUTPUT_RETURN(FLB_RETRY); } + /* Optional URI to replace default */ + if (ctx->uri) { + uri = ctx->uri; + } else { + uri = FLB_LOKI_URI; + } + /* Create HTTP client context */ - c = flb_http_client(u_conn, FLB_HTTP_POST, FLB_LOKI_URI, + c = flb_http_client(u_conn, FLB_HTTP_POST, uri, out_buf, out_size, ctx->tcp_host, ctx->tcp_port, NULL, 0); @@ -1604,6 +1616,16 @@ static void cb_loki_flush(struct flb_event_chunk *event_chunk, flb_http_bearer_auth(c, ctx->bearer_token); } + /* Arbitrary additional headers */ + flb_config_map_foreach(head, mv, ctx->headers) { + key = mk_list_entry_first(mv->val.list, struct flb_slist_entry, _head); + val = mk_list_entry_last(mv->val.list, struct flb_slist_entry, _head); + + flb_http_add_header(c, + key->str, flb_sds_len(key->str), + val->str, flb_sds_len(val->str)); + } + /* Add Content-Type header */ flb_http_add_header(c, FLB_LOKI_CT, sizeof(FLB_LOKI_CT) - 1, @@ -1805,6 +1827,18 @@ static struct flb_config_map config_map[] = { "Set bearer token auth" }, + { + FLB_CONFIG_MAP_SLIST_1, "header", NULL, + FLB_CONFIG_MAP_MULT, FLB_TRUE, offsetof(struct flb_loki, headers), + "Add a HTTP header key/value pair. Multiple headers can be set" + }, + + { + FLB_CONFIG_MAP_STR, "uri", NULL, + 0, FLB_TRUE, offsetof(struct flb_loki, uri), + "Specify an optional HTTP URI for the target web server, e.g: /something" + }, + { FLB_CONFIG_MAP_STR, "compress", NULL, 0, FLB_FALSE, 0, diff --git a/plugins/out_loki/loki.h b/plugins/out_loki/loki.h index 2011cee3ded..58008da33b0 100644 --- a/plugins/out_loki/loki.h +++ b/plugins/out_loki/loki.h @@ -77,6 +77,7 @@ struct flb_loki { /* Private */ int tcp_port; char *tcp_host; + char *uri; int out_line_format; int ra_used; /* number of record accessor label keys */ struct flb_record_accessor *ra_k8s; /* kubernetes record accessor */ @@ -93,6 +94,9 @@ struct flb_loki { /* Plugin instance */ struct flb_output_instance *ins; + + /* Arbitrary HTTP headers */ + struct mk_list *headers; }; #endif