Skip to content

Commit 5550487

Browse files
out_stackdriver: add static labels defined in configuration (#5176)
A feature that mirrors the out_loki exporter functionality to set to set static labels during configuration. This as a list of comma separate key=value pairs, e.g. label_a=value_a,label_b=value_b. The record_accesor wasn't implemented since the functionality can be recreated using label_keys. The expected behavior is that elements set with labels and label_keys are combined to set the LogEntry Labels in the LogExplorer. Signed-off-by: Francisco Valente <[email protected]>
1 parent ac3e0ea commit 5550487

File tree

4 files changed

+297
-16
lines changed

4 files changed

+297
-16
lines changed

plugins/out_stackdriver/stackdriver.c

Lines changed: 78 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <fluent-bit/flb_oauth2.h>
2626
#include <fluent-bit/flb_regex.h>
2727
#include <fluent-bit/flb_pthread.h>
28+
#include <fluent-bit/flb_kv.h>
2829

2930
#include <msgpack.h>
3031

@@ -911,12 +912,12 @@ static int process_local_resource_id(struct flb_stackdriver *ctx,
911912
}
912913

913914
/*
914-
* parse_labels
915+
* get_payload_labels
915916
* - Iterate throught the original payload (obj) and find out the entry that matches
916917
* the labels_key
917918
* - Used to convert all labels under labels_key to root-level `labels` field
918919
*/
919-
static msgpack_object *parse_labels(struct flb_stackdriver *ctx, msgpack_object *obj)
920+
static msgpack_object *get_payload_labels(struct flb_stackdriver *ctx, msgpack_object *obj)
920921
{
921922
int i;
922923
int len;
@@ -940,6 +941,50 @@ static msgpack_object *parse_labels(struct flb_stackdriver *ctx, msgpack_object
940941
return NULL;
941942
}
942943

944+
static void pack_labels(struct flb_stackdriver *ctx,
945+
msgpack_packer *mp_pck,
946+
msgpack_object *payload_labels_ptr)
947+
{
948+
int i;
949+
int ret;
950+
int labels_size = 0;
951+
char *val;
952+
struct mk_list *head;
953+
struct flb_kv *list_kv;
954+
msgpack_object_kv *obj_kv = NULL;
955+
956+
/* Determine size of labels map */
957+
labels_size = mk_list_size(&ctx->config_labels);
958+
if (payload_labels_ptr != NULL &&
959+
payload_labels_ptr->type == MSGPACK_OBJECT_MAP) {
960+
labels_size += payload_labels_ptr->via.map.size;
961+
}
962+
963+
msgpack_pack_map(mp_pck, labels_size);
964+
965+
/* pack labels from the payload */
966+
if (payload_labels_ptr != NULL &&
967+
payload_labels_ptr->type == MSGPACK_OBJECT_MAP) {
968+
969+
for (i = 0; i < payload_labels_ptr->via.map.size; i++) {
970+
obj_kv = &payload_labels_ptr->via.map.ptr[i];
971+
msgpack_pack_object(mp_pck, obj_kv->key);
972+
msgpack_pack_object(mp_pck, obj_kv->val);
973+
}
974+
}
975+
976+
/* pack labels set in configuration */
977+
/* in msgpack duplicate keys are overriden by the last set */
978+
/* static label keys override payload labels */
979+
mk_list_foreach(head, &ctx->config_labels){
980+
list_kv = mk_list_entry(head, struct flb_kv, _head);
981+
msgpack_pack_str(mp_pck, flb_sds_len(list_kv->key));
982+
msgpack_pack_str_body(mp_pck, list_kv->key, flb_sds_len(list_kv->key));
983+
msgpack_pack_str(mp_pck, flb_sds_len(list_kv->val));
984+
msgpack_pack_str_body(mp_pck, list_kv->val, flb_sds_len(list_kv->val));
985+
}
986+
}
987+
943988
static void cb_results(const char *name, const char *value,
944989
size_t vlen, void *data)
945990
{
@@ -1496,6 +1541,10 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
14961541
/* Count number of records */
14971542
array_size = total_records;
14981543

1544+
/* Parameters for labels */
1545+
msgpack_object *payload_labels_ptr;
1546+
int labels_size = 0;
1547+
14991548
/*
15001549
* Search each entry and validate insertId.
15011550
* Reject the entry if insertId is invalid.
@@ -1975,17 +2024,26 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
19752024
entry_size += 1;
19762025
}
19772026

1978-
/* Extract labels */
1979-
labels_ptr = parse_labels(ctx, obj);
1980-
if (labels_ptr != NULL) {
1981-
if (labels_ptr->type != MSGPACK_OBJECT_MAP) {
1982-
flb_plg_error(ctx->ins, "the type of labels should be map");
1983-
flb_sds_destroy(operation_id);
1984-
flb_sds_destroy(operation_producer);
1985-
msgpack_unpacked_destroy(&result);
1986-
msgpack_sbuffer_destroy(&mp_sbuf);
1987-
return NULL;
1988-
}
2027+
/* Extract payload labels */
2028+
payload_labels_ptr = get_payload_labels(ctx, obj);
2029+
if (payload_labels_ptr != NULL &&
2030+
payload_labels_ptr->type != MSGPACK_OBJECT_MAP) {
2031+
flb_plg_error(ctx->ins, "the type of payload labels should be map");
2032+
flb_sds_destroy(operation_id);
2033+
flb_sds_destroy(operation_producer);
2034+
msgpack_unpacked_destroy(&result);
2035+
msgpack_sbuffer_destroy(&mp_sbuf);
2036+
return -1;
2037+
}
2038+
2039+
/* Number of parsed labels */
2040+
labels_size = mk_list_size(&ctx->config_labels);
2041+
if (payload_labels_ptr != NULL &&
2042+
payload_labels_ptr->type == MSGPACK_OBJECT_MAP) {
2043+
labels_size += payload_labels_ptr->via.map.size;
2044+
}
2045+
2046+
if (labels_size > 0) {
19892047
entry_size += 1;
19902048
}
19912049

@@ -2043,10 +2101,10 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
20432101
}
20442102

20452103
/* labels */
2046-
if (labels_ptr != NULL) {
2104+
if (labels_size > 0) {
20472105
msgpack_pack_str(&mp_pck, 6);
20482106
msgpack_pack_str_body(&mp_pck, "labels", 6);
2049-
msgpack_pack_object(&mp_pck, *labels_ptr);
2107+
pack_labels(ctx, &mp_pck, payload_labels_ptr);
20502108
}
20512109

20522110
/* Clean up id and producer if operation extracted */
@@ -2453,6 +2511,11 @@ static struct flb_config_map config_map[] = {
24532511
0, FLB_TRUE, offsetof(struct flb_stackdriver, task_id),
24542512
"Set the resource task id"
24552513
},
2514+
{
2515+
FLB_CONFIG_MAP_CLIST, "labels", NULL,
2516+
0, FLB_TRUE, offsetof(struct flb_stackdriver, labels),
2517+
"Set the labels"
2518+
},
24562519
{
24572520
FLB_CONFIG_MAP_STR, "labels_key", DEFAULT_LABELS_KEY,
24582521
0, FLB_TRUE, offsetof(struct flb_stackdriver, labels_key),

plugins/out_stackdriver/stackdriver.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ struct flb_stackdriver {
131131
flb_sds_t node_name;
132132
bool is_k8s_resource_type;
133133

134-
flb_sds_t labels_key;
135134
flb_sds_t local_resource_id;
136135
flb_sds_t tag_prefix;
137136
/* shadow tag_prefix for safe deallocation */
138137
flb_sds_t tag_prefix_k8s;
139138

139+
/* labels */
140+
flb_sds_t labels_key;
141+
struct mk_list *labels;
142+
struct mk_list config_labels;
143+
140144
/* generic resources */
141145
flb_sds_t location;
142146
flb_sds_t namespace_id;

plugins/out_stackdriver/stackdriver_conf.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <fluent-bit/flb_utils.h>
2525
#include <fluent-bit/flb_jsmn.h>
2626
#include <fluent-bit/flb_sds.h>
27+
#include <fluent-bit/flb_kv.h>
2728

2829
#include <sys/types.h>
2930
#include <sys/stat.h>
@@ -167,6 +168,63 @@ static int read_credentials_file(const char *cred_file, struct flb_stackdriver *
167168
return 0;
168169
}
169170

171+
/*
172+
* parse_configuration_labels
173+
* - Parse labels set in configuration
174+
* - Returns the number of configuration labels
175+
*/
176+
177+
static int parse_configuration_labels(struct flb_stackdriver *ctx)
178+
{
179+
int ret;
180+
char *p;
181+
flb_sds_t key;
182+
flb_sds_t val;
183+
struct mk_list *head;
184+
struct flb_slist_entry *entry;
185+
msgpack_object_kv *kv = NULL;
186+
187+
if (ctx->labels) {
188+
mk_list_foreach(head, ctx->labels) {
189+
entry = mk_list_entry(head, struct flb_slist_entry, _head);
190+
191+
p = strchr(entry->str, '=');
192+
if (!p) {
193+
flb_plg_error(ctx->ins, "invalid key value pair on '%s'",
194+
entry->str);
195+
return -1;
196+
}
197+
198+
key = flb_sds_create_size((p - entry->str) + 1);
199+
flb_sds_cat(key, entry->str, p - entry->str);
200+
val = flb_sds_create(p + 1);
201+
if (!key) {
202+
flb_plg_error(ctx->ins,
203+
"invalid key value pair on '%s'",
204+
entry->str);
205+
return -1;
206+
}
207+
if (!val || flb_sds_len(val) == 0) {
208+
flb_plg_error(ctx->ins,
209+
"invalid key value pair on '%s'",
210+
entry->str);
211+
flb_sds_destroy(key);
212+
return -1;
213+
}
214+
215+
ret = flb_kv_item_create(&ctx->config_labels, key, val);
216+
flb_sds_destroy(key);
217+
flb_sds_destroy(val);
218+
219+
if (ret == -1) {
220+
return -1;
221+
}
222+
}
223+
}
224+
225+
return mk_list_size(&ctx->config_labels);
226+
}
227+
170228
struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *ins,
171229
struct flb_config *config)
172230
{
@@ -192,6 +250,16 @@ struct flb_stackdriver *flb_stackdriver_conf_create(struct flb_output_instance *
192250
return NULL;
193251
}
194252

253+
/* labels */
254+
flb_kv_init(&ctx->config_labels);
255+
ret = parse_configuration_labels((void *)ctx);
256+
if (ret == -1) {
257+
flb_plg_error(ins, "unable to parse configuration labels");
258+
flb_kv_release(&ctx->config_labels);
259+
flb_free(ctx);
260+
return NULL;
261+
}
262+
195263
/* Lookup metadata server URL */
196264
if (ctx->metadata_server == NULL) {
197265
tmp = getenv("METADATA_SERVER");
@@ -533,6 +601,7 @@ int flb_stackdriver_conf_destroy(struct flb_stackdriver *ctx)
533601
flb_sds_destroy(ctx->tag_prefix_k8s);
534602
}
535603

604+
flb_kv_release(&ctx->config_labels);
536605
flb_free(ctx);
537606

538607
return 0;

0 commit comments

Comments
 (0)