-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
loki.c
1895 lines (1603 loc) · 54.7 KB
/
loki.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2015-2024 The Fluent Bit Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <fluent-bit/flb_output_plugin.h>
#include <fluent-bit/flb_version.h>
#include <fluent-bit/flb_pack.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_time.h>
#include <fluent-bit/flb_http_client.h>
#include <fluent-bit/flb_ra_key.h>
#include <fluent-bit/flb_thread_storage.h>
#include <fluent-bit/record_accessor/flb_ra_parser.h>
#include <fluent-bit/flb_mp.h>
#include <fluent-bit/flb_log_event_decoder.h>
#include <fluent-bit/flb_gzip.h>
#include <ctype.h>
#include <sys/stat.h>
#include "loki.h"
struct flb_loki_dynamic_tenant_id_entry {
flb_sds_t value;
struct cfl_list _head;
};
pthread_once_t initialization_guard = PTHREAD_ONCE_INIT;
FLB_TLS_DEFINE(struct flb_loki_dynamic_tenant_id_entry,
thread_local_tenant_id);
void initialize_thread_local_storage()
{
FLB_TLS_INIT(thread_local_tenant_id);
}
static struct flb_loki_dynamic_tenant_id_entry *dynamic_tenant_id_create() {
struct flb_loki_dynamic_tenant_id_entry *entry;
entry = (struct flb_loki_dynamic_tenant_id_entry *) \
flb_calloc(1, sizeof(struct flb_loki_dynamic_tenant_id_entry));
if (entry != NULL) {
entry->value = NULL;
cfl_list_entry_init(&entry->_head);
}
return entry;
}
static void dynamic_tenant_id_destroy(struct flb_loki_dynamic_tenant_id_entry *entry) {
if (entry != NULL) {
if (entry->value != NULL) {
flb_sds_destroy(entry->value);
entry->value = NULL;
}
if (!cfl_list_entry_is_orphan(&entry->_head)) {
cfl_list_del(&entry->_head);
}
flb_free(entry);
}
}
static void flb_loki_kv_init(struct mk_list *list)
{
mk_list_init(list);
}
static inline void safe_sds_cat(flb_sds_t *buf, const char *str, int len)
{
flb_sds_t tmp;
tmp = flb_sds_cat(*buf, str, len);
if (tmp) {
*buf = tmp;
}
}
static inline void normalize_cat(struct flb_ra_parser *rp, flb_sds_t *name)
{
int sub;
int len;
char tmp[64];
struct mk_list *s_head;
struct flb_ra_key *key;
struct flb_ra_subentry *entry;
/* Iterate record accessor keys */
key = rp->key;
if (rp->type == FLB_RA_PARSER_STRING) {
safe_sds_cat(name, key->name, flb_sds_len(key->name));
}
else if (rp->type == FLB_RA_PARSER_KEYMAP) {
safe_sds_cat(name, key->name, flb_sds_len(key->name));
if (mk_list_size(key->subkeys) > 0) {
safe_sds_cat(name, "_", 1);
}
sub = 0;
mk_list_foreach(s_head, key->subkeys) {
entry = mk_list_entry(s_head, struct flb_ra_subentry, _head);
if (sub > 0) {
safe_sds_cat(name, "_", 1);
}
if (entry->type == FLB_RA_PARSER_STRING) {
safe_sds_cat(name, entry->str, flb_sds_len(entry->str));
}
else if (entry->type == FLB_RA_PARSER_ARRAY_ID) {
len = snprintf(tmp, sizeof(tmp) -1, "%d",
entry->array_id);
safe_sds_cat(name, tmp, len);
}
sub++;
}
}
}
static flb_sds_t normalize_ra_key_name(struct flb_loki *ctx,
struct flb_record_accessor *ra)
{
int c = 0;
flb_sds_t name;
struct mk_list *head;
struct flb_ra_parser *rp;
name = flb_sds_create_size(128);
if (!name) {
return NULL;
}
mk_list_foreach(head, &ra->list) {
rp = mk_list_entry(head, struct flb_ra_parser, _head);
if (c > 0) {
flb_sds_cat(name, "_", 1);
}
normalize_cat(rp, &name);
c++;
}
return name;
}
void flb_loki_kv_destroy(struct flb_loki_kv *kv)
{
/* destroy key and value */
flb_sds_destroy(kv->key);
if (kv->val_type == FLB_LOKI_KV_STR) {
flb_sds_destroy(kv->str_val);
}
else if (kv->val_type == FLB_LOKI_KV_RA) {
flb_ra_destroy(kv->ra_val);
}
if (kv->ra_key) {
flb_ra_destroy(kv->ra_key);
}
if (kv->key_normalized) {
flb_sds_destroy(kv->key_normalized);
}
flb_free(kv);
}
int flb_loki_kv_append(struct flb_loki *ctx, char *key, char *val)
{
int ra_count = 0;
int k_len;
int ret;
struct flb_loki_kv *kv;
if (!key) {
return -1;
}
if (!val && key[0] != '$') {
return -1;
}
kv = flb_calloc(1, sizeof(struct flb_loki_kv));
if (!kv) {
flb_errno();
return -1;
}
k_len = strlen(key);
if (key[0] == '$' && k_len >= 2 && isdigit(key[1])) {
flb_plg_error(ctx->ins,
"key name for record accessor cannot start with a number: %s",
key);
flb_free(kv);
return -1;
}
kv->key = flb_sds_create(key);
if (!kv->key) {
flb_free(kv);
return -1;
}
/*
* If the key starts with a '$', it means its a record accessor pattern and
* the key value pair will be formed using the key name and it proper value.
*/
if (key[0] == '$' && val == NULL) {
kv->ra_key = flb_ra_create(key, FLB_TRUE);
if (!kv->ra_key) {
flb_plg_error(ctx->ins,
"invalid key record accessor pattern for key '%s'",
key);
flb_loki_kv_destroy(kv);
return -1;
}
/* Normalize 'key name' using record accessor pattern */
kv->key_normalized = normalize_ra_key_name(ctx, kv->ra_key);
if (!kv->key_normalized) {
flb_plg_error(ctx->ins,
"could not normalize key pattern name '%s'\n",
kv->ra_key->pattern);
flb_loki_kv_destroy(kv);
return -1;
}
/* remove record keys placed as stream labels via 'labels' and 'label_keys' */
ret = flb_slist_add(&ctx->remove_keys_derived, key);
if (ret < 0) {
flb_loki_kv_destroy(kv);
return -1;
}
ra_count++;
}
else if (val[0] == '$') {
/* create a record accessor context */
kv->val_type = FLB_LOKI_KV_RA;
kv->ra_val = flb_ra_create(val, FLB_TRUE);
if (!kv->ra_val) {
flb_plg_error(ctx->ins,
"invalid record accessor pattern for key '%s': %s",
key, val);
flb_loki_kv_destroy(kv);
return -1;
}
ret = flb_slist_add(&ctx->remove_keys_derived, val);
if (ret < 0) {
flb_loki_kv_destroy(kv);
return -1;
}
ra_count++;
}
else {
kv->val_type = FLB_LOKI_KV_STR;
kv->str_val = flb_sds_create(val);
if (!kv->str_val) {
flb_loki_kv_destroy(kv);
return -1;
}
}
mk_list_add(&kv->_head, &ctx->labels_list);
/* return the number of record accessor values */
return ra_count;
}
static void flb_loki_kv_exit(struct flb_loki *ctx)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_loki_kv *kv;
mk_list_foreach_safe(head, tmp, &ctx->labels_list) {
kv = mk_list_entry(head, struct flb_loki_kv, _head);
/* unlink and destroy */
mk_list_del(&kv->_head);
flb_loki_kv_destroy(kv);
}
}
/* Pack a label key, it also perform sanitization of the characters */
static int pack_label_key(msgpack_packer *mp_pck, char *key, int key_len)
{
int i;
int k_len = key_len;
int is_digit = FLB_FALSE;
char *p;
size_t prev_size;
/* Normalize key name using the packed value */
if (isdigit(*key)) {
is_digit = FLB_TRUE;
k_len++;
}
/* key: pack the length */
msgpack_pack_str(mp_pck, k_len);
if (is_digit) {
msgpack_pack_str_body(mp_pck, "_", 1);
}
/* save the current offset */
prev_size = ((msgpack_sbuffer *) mp_pck->data)->size;
/* Pack the key name */
msgpack_pack_str_body(mp_pck, key, key_len);
/* 'p' will point to where the key was written */
p = (char *) (((msgpack_sbuffer*) mp_pck->data)->data + prev_size);
/* and sanitize the key characters */
for (i = 0; i < key_len; i++) {
if (!isalnum(p[i]) && p[i] != '_') {
p[i] = '_';
}
}
return 0;
}
static flb_sds_t pack_labels(struct flb_loki *ctx,
msgpack_packer *mp_pck,
char *tag, int tag_len,
msgpack_object *map)
{
int i;
flb_sds_t ra_val;
struct mk_list *head;
struct flb_ra_value *rval = NULL;
struct flb_loki_kv *kv;
msgpack_object k;
msgpack_object v;
struct flb_mp_map_header mh;
/* Initialize dynamic map header */
flb_mp_map_header_init(&mh, mp_pck);
mk_list_foreach(head, &ctx->labels_list) {
kv = mk_list_entry(head, struct flb_loki_kv, _head);
/* record accessor key/value pair */
if (kv->ra_key != NULL && kv->ra_val == NULL) {
ra_val = flb_ra_translate(kv->ra_key, tag, tag_len, *(map), NULL);
if (!ra_val || flb_sds_len(ra_val) == 0) {
/* if no value is retruned or if it's empty, just skip it */
flb_plg_warn(ctx->ins,
"empty record accessor key translation for pattern: %s",
kv->ra_key->pattern);
}
else {
/* Pack the key and value */
flb_mp_map_header_append(&mh);
/* We skip the first '$' character since it won't be valid in Loki */
pack_label_key(mp_pck, kv->key_normalized,
flb_sds_len(kv->key_normalized));
msgpack_pack_str(mp_pck, flb_sds_len(ra_val));
msgpack_pack_str_body(mp_pck, ra_val, flb_sds_len(ra_val));
}
if (ra_val) {
flb_sds_destroy(ra_val);
}
continue;
}
/*
* The code is a bit duplicated to be able to manage the exception of an
* invalid or empty value, on that case the k/v is skipped.
*/
if (kv->val_type == FLB_LOKI_KV_STR) {
flb_mp_map_header_append(&mh);
msgpack_pack_str(mp_pck, flb_sds_len(kv->key));
msgpack_pack_str_body(mp_pck, kv->key, flb_sds_len(kv->key));
msgpack_pack_str(mp_pck, flb_sds_len(kv->str_val));
msgpack_pack_str_body(mp_pck, kv->str_val, flb_sds_len(kv->str_val));
}
else if (kv->val_type == FLB_LOKI_KV_RA) {
/* record accessor type */
ra_val = flb_ra_translate(kv->ra_val, tag, tag_len, *(map), NULL);
if (!ra_val || flb_sds_len(ra_val) == 0) {
flb_plg_debug(ctx->ins, "could not translate record accessor");
}
else {
flb_mp_map_header_append(&mh);
msgpack_pack_str(mp_pck, flb_sds_len(kv->key));
msgpack_pack_str_body(mp_pck, kv->key, flb_sds_len(kv->key));
msgpack_pack_str(mp_pck, flb_sds_len(ra_val));
msgpack_pack_str_body(mp_pck, ra_val, flb_sds_len(ra_val));
}
if (ra_val) {
flb_sds_destroy(ra_val);
}
}
}
if (ctx->auto_kubernetes_labels == FLB_TRUE) {
rval = flb_ra_get_value_object(ctx->ra_k8s, *map);
if (rval && rval->o.type == MSGPACK_OBJECT_MAP) {
for (i = 0; i < rval->o.via.map.size; i++) {
k = rval->o.via.map.ptr[i].key;
v = rval->o.via.map.ptr[i].val;
if (k.type != MSGPACK_OBJECT_STR || v.type != MSGPACK_OBJECT_STR) {
continue;
}
/* append the key/value pair */
flb_mp_map_header_append(&mh);
/* Pack key */
pack_label_key(mp_pck, (char *) k.via.str.ptr, k.via.str.size);
/* Pack the value */
msgpack_pack_str(mp_pck, v.via.str.size);
msgpack_pack_str_body(mp_pck, v.via.str.ptr, v.via.str.size);
}
}
if (rval) {
flb_ra_key_value_destroy(rval);
}
}
/* Check if we added any label, if no one has been set, set the defaul 'job' */
if (mh.entries == 0) {
/* pack the default entry */
flb_mp_map_header_append(&mh);
msgpack_pack_str(mp_pck, 3);
msgpack_pack_str_body(mp_pck, "job", 3);
msgpack_pack_str(mp_pck, 10);
msgpack_pack_str_body(mp_pck, "fluent-bit", 10);
}
flb_mp_map_header_end(&mh);
return 0;
}
static int create_label_map_entry(struct flb_loki *ctx,
struct flb_sds_list *list, msgpack_object *val, int *ra_used)
{
msgpack_object key;
flb_sds_t label_key;
flb_sds_t val_str;
int i;
int len;
int ret;
if (ctx == NULL || list == NULL || val == NULL || ra_used == NULL) {
return -1;
}
switch (val->type) {
case MSGPACK_OBJECT_STR:
label_key = flb_sds_create_len(val->via.str.ptr, val->via.str.size);
if (label_key == NULL) {
flb_errno();
return -1;
}
val_str = flb_ra_create_str_from_list(list);
if (val_str == NULL) {
flb_plg_error(ctx->ins, "[%s] flb_ra_create_from_list failed", __FUNCTION__);
flb_sds_destroy(label_key);
return -1;
}
/* for debugging
printf("label_key=%s val_str=%s\n", label_key, val_str);
*/
ret = flb_loki_kv_append(ctx, label_key, val_str);
flb_sds_destroy(label_key);
flb_sds_destroy(val_str);
if (ret == -1) {
return -1;
}
*ra_used = *ra_used + 1;
break;
case MSGPACK_OBJECT_MAP:
len = val->via.map.size;
for (i=0; i<len; i++) {
key = val->via.map.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "[%s] key is not string", __FUNCTION__);
return -1;
}
ret = flb_sds_list_add(list, (char*)key.via.str.ptr, key.via.str.size);
if (ret < 0) {
flb_plg_error(ctx->ins, "[%s] flb_sds_list_add failed", __FUNCTION__);
return -1;
}
ret = create_label_map_entry(ctx, list, &val->via.map.ptr[i].val, ra_used);
if (ret < 0) {
return -1;
}
ret = flb_sds_list_del_last_entry(list);
if (ret < 0) {
flb_plg_error(ctx->ins, "[%s] flb_sds_list_del_last_entry failed", __FUNCTION__);
return -1;
}
}
break;
default:
flb_plg_error(ctx->ins, "[%s] value type is not str or map. type=%d", __FUNCTION__, val->type);
return -1;
}
return 0;
}
static int create_label_map_entries(struct flb_loki *ctx,
char *msgpack_buf, size_t msgpack_size, int *ra_used)
{
struct flb_sds_list *list = NULL;
msgpack_unpacked result;
size_t off = 0;
int i;
int len;
int ret;
msgpack_object key;
if (ctx == NULL || msgpack_buf == NULL || ra_used == NULL) {
return -1;
}
msgpack_unpacked_init(&result);
while(msgpack_unpack_next(&result, msgpack_buf, msgpack_size, &off) == MSGPACK_UNPACK_SUCCESS) {
if (result.data.type != MSGPACK_OBJECT_MAP) {
flb_plg_error(ctx->ins, "[%s] data type is not map", __FUNCTION__);
msgpack_unpacked_destroy(&result);
return -1;
}
len = result.data.via.map.size;
for (i=0; i<len; i++) {
list = flb_sds_list_create();
if (list == NULL) {
flb_plg_error(ctx->ins, "[%s] flb_sds_list_create failed", __FUNCTION__);
msgpack_unpacked_destroy(&result);
return -1;
}
key = result.data.via.map.ptr[i].key;
if (key.type != MSGPACK_OBJECT_STR) {
flb_plg_error(ctx->ins, "[%s] key is not string", __FUNCTION__);
flb_sds_list_destroy(list);
msgpack_unpacked_destroy(&result);
return -1;
}
ret = flb_sds_list_add(list, (char*)key.via.str.ptr, key.via.str.size);
if (ret < 0) {
flb_plg_error(ctx->ins, "[%s] flb_sds_list_add failed", __FUNCTION__);
flb_sds_list_destroy(list);
msgpack_unpacked_destroy(&result);
return -1;
}
ret = create_label_map_entry(ctx, list, &result.data.via.map.ptr[i].val, ra_used);
if (ret < 0) {
flb_plg_error(ctx->ins, "[%s] create_label_map_entry failed", __FUNCTION__);
flb_sds_list_destroy(list);
msgpack_unpacked_destroy(&result);
return -1;
}
flb_sds_list_destroy(list);
list = NULL;
}
}
msgpack_unpacked_destroy(&result);
return 0;
}
static int read_label_map_path_file(struct flb_output_instance *ins, flb_sds_t path,
char **out_buf, size_t *out_size)
{
int ret;
int root_type;
char *buf = NULL;
char *msgp_buf = NULL;
FILE *fp = NULL;
struct stat st;
size_t file_size;
size_t ret_size;
ret = access(path, R_OK);
if (ret < 0) {
flb_errno();
flb_plg_error(ins, "can't access %s", path);
return -1;
}
ret = stat(path, &st);
if (ret < 0) {
flb_errno();
flb_plg_error(ins, "stat failed %s", path);
return -1;
}
file_size = st.st_size;
fp = fopen(path, "r");
if (fp == NULL) {
flb_plg_error(ins, "can't open %s", path);
return -1;
}
buf = flb_malloc(file_size);
if (buf == NULL) {
flb_plg_error(ins, "malloc failed");
fclose(fp);
return -1;
}
ret_size = fread(buf, 1, file_size, fp);
if (ret_size < file_size && feof(fp) != 0) {
flb_plg_error(ins, "fread failed");
fclose(fp);
flb_free(buf);
return -1;
}
ret = flb_pack_json(buf, file_size, &msgp_buf, &ret_size, &root_type, NULL);
if (ret < 0) {
flb_plg_error(ins, "flb_pack_json failed");
fclose(fp);
flb_free(buf);
return -1;
}
*out_buf = msgp_buf;
*out_size = ret_size;
fclose(fp);
flb_free(buf);
return 0;
}
static int load_label_map_path(struct flb_loki *ctx, flb_sds_t path, int *ra_used)
{
int ret;
char *msgpack_buf = NULL;
size_t msgpack_size;
ret = read_label_map_path_file(ctx->ins, path, &msgpack_buf, &msgpack_size);
if (ret < 0) {
return -1;
}
ret = create_label_map_entries(ctx, msgpack_buf, msgpack_size, ra_used);
if (ret < 0) {
flb_free(msgpack_buf);
return -1;
}
if (msgpack_buf != NULL) {
flb_free(msgpack_buf);
}
return 0;
}
static int parse_labels(struct flb_loki *ctx)
{
int ret;
int ra_used = 0;
char *p;
flb_sds_t key;
flb_sds_t val;
struct mk_list *head;
struct flb_slist_entry *entry;
flb_loki_kv_init(&ctx->labels_list);
if (ctx->labels) {
mk_list_foreach(head, ctx->labels) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
/* record accessor label key ? */
if (entry->str[0] == '$') {
ret = flb_loki_kv_append(ctx, entry->str, NULL);
if (ret == -1) {
return -1;
}
else if (ret > 0) {
ra_used++;
}
continue;
}
p = strchr(entry->str, '=');
if (!p) {
flb_plg_error(ctx->ins, "invalid key value pair on '%s'",
entry->str);
return -1;
}
key = flb_sds_create_size((p - entry->str) + 1);
flb_sds_cat(key, entry->str, p - entry->str);
val = flb_sds_create(p + 1);
if (!key) {
flb_plg_error(ctx->ins,
"invalid key value pair on '%s'",
entry->str);
return -1;
}
if (!val || flb_sds_len(val) == 0) {
flb_plg_error(ctx->ins,
"invalid key value pair on '%s'",
entry->str);
flb_sds_destroy(key);
return -1;
}
ret = flb_loki_kv_append(ctx, key, val);
flb_sds_destroy(key);
flb_sds_destroy(val);
if (ret == -1) {
return -1;
}
else if (ret > 0) {
ra_used++;
}
}
}
/* Append label keys set in the configuration */
if (ctx->label_keys) {
mk_list_foreach(head, ctx->label_keys) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
if (entry->str[0] != '$') {
flb_plg_error(ctx->ins,
"invalid label key, the name must start with '$'");
return -1;
}
ret = flb_loki_kv_append(ctx, entry->str, NULL);
if (ret == -1) {
return -1;
}
else if (ret > 0) {
ra_used++;
}
}
}
/* label_map_path */
if (ctx->label_map_path) {
ret = load_label_map_path(ctx, ctx->label_map_path, &ra_used);
if (ret != 0) {
flb_plg_error(ctx->ins, "failed to load label_map_path");
}
}
if (ctx->auto_kubernetes_labels == FLB_TRUE) {
ctx->ra_k8s = flb_ra_create("$kubernetes['labels']", FLB_TRUE);
if (!ctx->ra_k8s) {
flb_plg_error(ctx->ins,
"could not create record accessor for Kubernetes labels");
return -1;
}
}
/*
* If the variable 'ra_used' is greater than zero, means that record accessor is
* being used to compose the stream labels.
*/
ctx->ra_used = ra_used;
return 0;
}
static int key_is_duplicated(struct mk_list *list, char *str, int len)
{
struct mk_list *head;
struct flb_slist_entry *entry;
mk_list_foreach(head, list) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
if (flb_sds_len(entry->str) == len &&
strncmp(entry->str, str, len) == 0) {
return FLB_TRUE;
}
}
return FLB_FALSE;
}
static int prepare_remove_keys(struct flb_loki *ctx)
{
int ret;
int len;
int size;
char *tmp;
struct mk_list *head;
struct flb_slist_entry *entry;
struct mk_list *patterns;
patterns = &ctx->remove_keys_derived;
/* Add remove keys set in the configuration */
if (ctx->remove_keys) {
mk_list_foreach(head, ctx->remove_keys) {
entry = mk_list_entry(head, struct flb_slist_entry, _head);
if (entry->str[0] != '$') {
tmp = flb_malloc(flb_sds_len(entry->str) + 2);
if (!tmp) {
flb_errno();
continue;
}
else {
tmp[0] = '$';
len = flb_sds_len(entry->str);
memcpy(tmp + 1, entry->str, len);
tmp[len + 1] = '\0';
len++;
}
}
else {
tmp = entry->str;
len = flb_sds_len(entry->str);
}
ret = key_is_duplicated(patterns, tmp, len);
if (ret == FLB_TRUE) {
if (entry->str != tmp) {
flb_free(tmp);
}
continue;
}
ret = flb_slist_add_n(patterns, tmp, len);
if (entry->str != tmp) {
flb_free(tmp);
}
if (ret < 0) {
return -1;
}
}
size = mk_list_size(patterns);
flb_plg_debug(ctx->ins, "remove_mpa size: %d", size);
if (size > 0) {
ctx->remove_mpa = flb_mp_accessor_create(patterns);
if (ctx->remove_mpa == NULL) {
return -1;
}
}
}
return 0;
}
static void loki_config_destroy(struct flb_loki *ctx)
{
if (ctx->u) {
flb_upstream_destroy(ctx->u);
}
if (ctx->ra_k8s) {
flb_ra_destroy(ctx->ra_k8s);
}
if (ctx->ra_tenant_id_key) {
flb_ra_destroy(ctx->ra_tenant_id_key);
}
if (ctx->remove_mpa) {
flb_mp_accessor_destroy(ctx->remove_mpa);
}
flb_slist_destroy(&ctx->remove_keys_derived);
flb_loki_kv_exit(ctx);
flb_free(ctx);
}
static struct flb_loki *loki_config_create(struct flb_output_instance *ins,
struct flb_config *config)
{
int ret;
int io_flags = 0;
struct flb_loki *ctx;
struct flb_upstream *upstream;
char *compress;
/* Create context */
ctx = flb_calloc(1, sizeof(struct flb_loki));
if (!ctx) {
flb_errno();
return NULL;
}
ctx->ins = ins;
flb_loki_kv_init(&ctx->labels_list);
/* Register context with plugin instance */
flb_output_set_context(ins, ctx);
/* Set networking defaults */
flb_output_net_default(FLB_LOKI_HOST, FLB_LOKI_PORT, ins);
/* Load config map */
ret = flb_output_config_map_set(ins, (void *) ctx);
if (ret == -1) {
return NULL;
}
/* Initialize final remove_keys list */
flb_slist_create(&ctx->remove_keys_derived);
/* Parse labels */
ret = parse_labels(ctx);
if (ret == -1) {
return NULL;
}
/* Load remove keys */
ret = prepare_remove_keys(ctx);
if (ret == -1) {
return NULL;
}
/* tenant_id_key */
if (ctx->tenant_id_key_config) {
ctx->ra_tenant_id_key = flb_ra_create(ctx->tenant_id_key_config, FLB_FALSE);
if (!ctx->ra_tenant_id_key) {
flb_plg_error(ctx->ins,
"could not create record accessor for Tenant ID");
}
}
/* Compress (gzip) */
compress = (char *) flb_output_get_property("compress", ins);
ctx->compress_gzip = FLB_FALSE;
if (compress) {
if (strcasecmp(compress, "gzip") == 0) {
ctx->compress_gzip = FLB_TRUE;
}
}
/* Line Format */
if (strcasecmp(ctx->line_format, "json") == 0) {
ctx->out_line_format = FLB_LOKI_FMT_JSON;
}
else if (strcasecmp(ctx->line_format, "key_value") == 0) {
ctx->out_line_format = FLB_LOKI_FMT_KV;
}
else {
flb_plg_error(ctx->ins, "invalid 'line_format' value: %s",
ctx->line_format);
return NULL;
}
/* use TLS ? */
if (ins->use_tls == FLB_TRUE) {
io_flags = FLB_IO_TLS;
}
else {
io_flags = FLB_IO_TCP;
}
if (ins->host.ipv6 == FLB_TRUE) {
io_flags |= FLB_IO_IPV6;
}
/* Create Upstream connection context */
upstream = flb_upstream_create(config,
ins->host.name,
ins->host.port,
io_flags,
ins->tls);
if (!upstream) {
return NULL;
}
ctx->u = upstream;
flb_output_upstream_set(ctx->u, ins);