Skip to content

Commit

Permalink
out_loki: add ability to add arbitrary http headers and adjust the UR…
Browse files Browse the repository at this point in the history
…I 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 <[email protected]>
  • Loading branch information
alarobric authored and Alan Richards committed Sep 26, 2023
1 parent adf20cc commit 23f5294
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
36 changes: 35 additions & 1 deletion plugins/out_loki/loki.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions plugins/out_loki/loki.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -93,6 +94,9 @@ struct flb_loki {

/* Plugin instance */
struct flb_output_instance *ins;

/* Arbitrary HTTP headers */
struct mk_list *headers;
};

#endif

0 comments on commit 23f5294

Please sign in to comment.