Repository navigation
Expand file tree
/
Copy pathflb_log.c
More file actions
1339 lines (1130 loc) · 36.1 KB
/
Copy pathflb_log.c
File metadata and controls
1339 lines (1130 loc) · 36.1 KB
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-2026 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 <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <stdarg.h>
#include <inttypes.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <monkey/mk_core.h>
#include <fluent-bit/flb_log.h>
#include <fluent-bit/flb_pipe.h>
#include <fluent-bit/flb_config.h>
#include <fluent-bit/flb_worker.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_time.h>
#include <cmetrics/cmetrics.h>
#include <cmetrics/cmt_counter.h>
#ifdef WIN32
#include <winsock.h>
#include <winbase.h>
#endif
#ifdef FLB_HAVE_AWS_ERROR_REPORTER
#include <fluent-bit/aws/flb_aws_error_reporter.h>
extern struct flb_aws_error_reporter *error_reporter;
#endif
FLB_TLS_DEFINE(struct flb_log, flb_log_ctx)
/* Simple structure to dispatch messages to the log collector */
struct log_message {
size_t size;
char msg[4096 - sizeof(size_t)];
};
static int log_queue_enqueue(struct flb_log *log, int type,
const struct flb_time *timestamp,
const char *msg, size_t size);
static struct flb_log_record *log_queue_dequeue(struct flb_log *log);
static void log_queue_destroy(struct flb_log *log);
static void log_queue_report_drops(struct flb_log *log);
static void log_queue_increment_counter(struct cmt_counter *counter, uint64_t value);
static int log_open_sink(struct flb_log *log);
static void log_close_sink(struct flb_log *log);
static inline const char *flb_log_message_type_str(int type);
static struct flb_log_record *log_queue_pop(struct flb_log_queue *queue);
static void log_queue_cleanup(struct flb_log_queue *queue);
static int log_prepare_header(char *buf, size_t size, int type,
const struct flb_time *timestamp);
static int log_render_message(struct log_message *msg, int type,
const struct flb_time *timestamp,
const char *body, size_t body_size);
static struct flb_log_record *log_record_clone(int type,
const struct flb_time *timestamp,
const char *msg, size_t size);
static int log_pipeline_signal(struct flb_log *log);
static void log_pipeline_mirror(struct flb_log *log, struct flb_log_record *record);
static inline int64_t flb_log_consume_signal(struct flb_log *context)
{
int64_t signal_value;
int ret;
ret = flb_pipe_r(context->ch_mng[0], &signal_value, sizeof(signal_value));
if (ret <= 0) {
flb_pipe_error();
return -1;
}
return signal_value;
}
static inline int flb_log_enqueue_signal(struct flb_log *context,
int64_t signal_value)
{
int ret;
ret = flb_pipe_w(context->ch_mng[1], &signal_value, sizeof(signal_value));
if (ret <= 0) {
flb_pipe_error();
ret = 1;
}
else {
ret = 0;
}
return ret;
}
static inline int log_push(struct log_message *msg, struct flb_log *log)
{
int ret = -1;
if (log->type == FLB_LOG_STDERR) {
return write(STDERR_FILENO, msg->msg, msg->size);
}
else if (log->type == FLB_LOG_FILE) {
if (log->out_fd == -1 && log_open_sink(log) == -1) {
return write(STDERR_FILENO, msg->msg, msg->size);
}
ret = write(log->out_fd, msg->msg, msg->size);
}
return ret;
}
static void log_queue_increment_counter(struct cmt_counter *counter, uint64_t value)
{
int ret;
if (counter == NULL || value == 0) {
return;
}
ret = cmt_counter_add(counter, cfl_time_now(), value, 0, NULL);
if (ret == -1) {
fprintf(stderr, "[log] failed to update logger transport counter\n");
}
}
static int log_open_sink(struct flb_log *log)
{
log->out_fd = open(log->out, O_CREAT | O_WRONLY | O_APPEND, 0666);
if (log->out_fd == -1) {
fprintf(stderr, "[log] error opening log file %s. Using stderr.\n",
log->out);
return -1;
}
return 0;
}
static void log_close_sink(struct flb_log *log)
{
if (log->out_fd != -1) {
close(log->out_fd);
log->out_fd = -1;
}
}
static int log_prepare_header(char *buf, size_t size, int type,
const struct flb_time *timestamp)
{
const char *header_color = NULL;
const char *header_title;
const char *bold_color = ANSI_BOLD;
const char *reset_color = ANSI_RESET;
struct tm tm_result;
struct tm *current;
switch (type) {
case FLB_LOG_HELP:
header_color = ANSI_CYAN;
break;
case FLB_LOG_INFO:
header_color = ANSI_GREEN;
break;
case FLB_LOG_WARN:
header_color = ANSI_YELLOW;
break;
case FLB_LOG_ERROR:
header_color = ANSI_RED;
break;
case FLB_LOG_DEBUG:
header_color = ANSI_YELLOW;
break;
case FLB_LOG_IDEBUG:
header_color = ANSI_CYAN;
break;
case FLB_LOG_TRACE:
header_color = ANSI_BLUE;
break;
}
#ifdef FLB_LOG_NO_CONTROL_CHARS
header_color = "";
bold_color = "";
reset_color = "";
#else
if (!isatty(STDERR_FILENO)) {
header_color = "";
bold_color = "";
reset_color = "";
}
#endif
current = localtime_r(×tamp->tm.tv_sec, &tm_result);
if (current == NULL) {
return -1;
}
header_title = flb_log_message_type_str(type);
return snprintf(buf, size,
"%s[%s%i/%02i/%02i %02i:%02i:%02i.%03ld%s]%s [%s%5s%s] ",
bold_color, reset_color,
current->tm_year + 1900,
current->tm_mon + 1,
current->tm_mday,
current->tm_hour,
current->tm_min,
current->tm_sec,
(long) (timestamp->tm.tv_nsec / 1000000),
bold_color, reset_color,
header_color, header_title, reset_color);
}
static int log_render_message(struct log_message *msg, int type,
const struct flb_time *timestamp,
const char *body, size_t body_size)
{
int header_length;
size_t available;
size_t copy_length;
header_length = log_prepare_header(msg->msg, sizeof(msg->msg) - 1, type, timestamp);
if (header_length < 0 || header_length >= sizeof(msg->msg) - 1) {
return -1;
}
available = (sizeof(msg->msg) - 2) - header_length;
copy_length = body_size;
if (copy_length > available) {
copy_length = available;
}
memcpy(msg->msg + header_length, body, copy_length);
msg->msg[header_length + copy_length] = '\n';
msg->msg[header_length + copy_length + 1] = '\0';
msg->size = header_length + copy_length + 1;
return 0;
}
static struct flb_log_record *log_record_clone(int type,
const struct flb_time *timestamp,
const char *msg, size_t size)
{
struct flb_log_record *record;
record = flb_malloc(sizeof(struct flb_log_record) + size);
if (record == NULL) {
flb_errno();
return NULL;
}
record->type = type;
record->timestamp_sec = timestamp->tm.tv_sec;
record->timestamp_nsec = timestamp->tm.tv_nsec;
record->size = size;
record->next = NULL;
memcpy(record->msg, msg, size);
return record;
}
void flb_log_record_destroy(struct flb_log_record *record)
{
if (record != NULL) {
flb_free(record);
}
}
static struct flb_log_record *log_queue_pop(struct flb_log_queue *queue)
{
struct flb_log_record *record;
pthread_mutex_lock(&queue->mutex);
record = queue->head;
if (record != NULL) {
queue->head = record->next;
if (queue->head == NULL) {
queue->tail = NULL;
queue->signal_pending = FLB_FALSE;
}
queue->length--;
record->next = NULL;
}
else {
queue->signal_pending = FLB_FALSE;
}
pthread_mutex_unlock(&queue->mutex);
return record;
}
static void log_queue_cleanup(struct flb_log_queue *queue)
{
struct flb_log_record *record;
while ((record = log_queue_pop(queue)) != NULL) {
flb_log_record_destroy(record);
}
}
static int log_pipeline_signal(struct flb_log *log)
{
int ret;
ret = flb_pipe_w(log->pipeline_ch[1], ".", 1);
if (ret <= 0) {
flb_pipe_error();
return -1;
}
return 0;
}
static void log_pipeline_mirror(struct flb_log *log, struct flb_log_record *record)
{
int signal_pending;
struct flb_time timestamp;
struct flb_log_record *copy;
if (log->pipeline_enabled == FLB_FALSE) {
return;
}
timestamp.tm.tv_sec = record->timestamp_sec;
timestamp.tm.tv_nsec = record->timestamp_nsec;
copy = log_record_clone(record->type, ×tamp, record->msg, record->size);
if (copy == NULL) {
return;
}
pthread_mutex_lock(&log->pipeline_queue.mutex);
if (log->pipeline_queue.length >= log->pipeline_queue.limit) {
pthread_mutex_unlock(&log->pipeline_queue.mutex);
flb_log_record_destroy(copy);
return;
}
if (log->pipeline_queue.tail == NULL) {
log->pipeline_queue.head = copy;
log->pipeline_queue.tail = copy;
}
else {
log->pipeline_queue.tail->next = copy;
log->pipeline_queue.tail = copy;
}
log->pipeline_queue.length++;
signal_pending = log->pipeline_queue.signal_pending;
if (signal_pending == FLB_FALSE) {
log->pipeline_queue.signal_pending = FLB_TRUE;
}
pthread_mutex_unlock(&log->pipeline_queue.mutex);
if (signal_pending == FLB_FALSE && log_pipeline_signal(log) != 0) {
pthread_mutex_lock(&log->pipeline_queue.mutex);
log->pipeline_queue.signal_pending = FLB_FALSE;
pthread_mutex_unlock(&log->pipeline_queue.mutex);
}
}
static inline int log_push_record(struct flb_log_record *record, struct flb_log *log)
{
struct log_message msg;
struct flb_time timestamp;
timestamp.tm.tv_sec = record->timestamp_sec;
timestamp.tm.tv_nsec = record->timestamp_nsec;
if (log_render_message(&msg, record->type, ×tamp,
record->msg, record->size) != 0) {
fprintf(stderr, "[log] could not render log record\n");
return -1;
}
return log_push(&msg, log);
}
static int log_queue_enqueue(struct flb_log *log, int type,
const struct flb_time *timestamp,
const char *msg, size_t size)
{
int signal_pending;
struct flb_log_record *record;
record = log_record_clone(type, timestamp, msg, size);
if (record == NULL) {
return -1;
}
pthread_mutex_lock(&log->queue_mutex);
if (log->queue_length >= log->queue_limit) {
log->dropped_records++;
pthread_mutex_unlock(&log->queue_mutex);
flb_free(record);
log_queue_increment_counter(log->metrics->queue_drop_counter, 1);
return -1;
}
if (log->queue_tail == NULL) {
log->queue_head = record;
log->queue_tail = record;
}
else {
log->queue_tail->next = record;
log->queue_tail = record;
}
log->queue_length++;
signal_pending = log->queue_signal_pending;
if (signal_pending == FLB_FALSE) {
log->queue_signal_pending = FLB_TRUE;
}
pthread_mutex_unlock(&log->queue_mutex);
if (signal_pending == FLB_FALSE) {
if (flb_log_enqueue_signal(log, FLB_LOG_MNG_DRAIN_SIGNAL) != 0) {
pthread_mutex_lock(&log->queue_mutex);
log->queue_signal_pending = FLB_FALSE;
pthread_mutex_unlock(&log->queue_mutex);
return -1;
}
}
log_queue_increment_counter(log->metrics->queue_enqueue_counter, 1);
return 0;
}
static struct flb_log_record *log_queue_dequeue(struct flb_log *log)
{
struct flb_log_record *record;
pthread_mutex_lock(&log->queue_mutex);
record = log->queue_head;
if (record != NULL) {
log->queue_head = record->next;
if (log->queue_head == NULL) {
log->queue_tail = NULL;
log->queue_signal_pending = FLB_FALSE;
}
log->queue_length--;
record->next = NULL;
}
else {
log->queue_signal_pending = FLB_FALSE;
}
pthread_mutex_unlock(&log->queue_mutex);
return record;
}
static void log_queue_destroy(struct flb_log *log)
{
struct flb_log_record *record;
while ((record = log_queue_dequeue(log)) != NULL) {
flb_free(record);
}
}
static void log_queue_report_drops(struct flb_log *log)
{
int len;
size_t dropped_records;
struct log_message msg = {0};
pthread_mutex_lock(&log->queue_mutex);
dropped_records = log->dropped_records;
log->dropped_records = 0;
pthread_mutex_unlock(&log->queue_mutex);
if (dropped_records == 0) {
return;
}
len = snprintf(msg.msg,
sizeof(msg.msg),
"[log] dropped %zu log records due to logger queue saturation\n",
dropped_records);
if (len <= 0) {
return;
}
msg.size = (size_t) len;
log_push(&msg, log);
}
/* Central collector of messages */
static void log_worker_collector(void *data)
{
int run = FLB_TRUE;
struct flb_log *log = data;
struct mk_event *event = NULL;
struct flb_log_record *record;
int64_t signal_value;
FLB_TLS_INIT(flb_log_ctx);
FLB_TLS_SET(flb_log_ctx, log);
mk_utils_worker_rename("flb-logger");
/* Signal the caller */
pthread_mutex_lock(&log->pth_mutex);
log->pth_init = FLB_TRUE;
pthread_cond_signal(&log->pth_cond);
pthread_mutex_unlock(&log->pth_mutex);
while (run) {
mk_event_wait(log->evl);
mk_event_foreach(event, log->evl) {
if (event->type == FLB_LOG_MNG) {
signal_value = flb_log_consume_signal(log);
if (signal_value == FLB_LOG_MNG_TERMINATION_SIGNAL) {
run = FLB_FALSE;
}
else if (signal_value == FLB_LOG_MNG_DRAIN_SIGNAL ||
signal_value == FLB_LOG_MNG_REFRESH_SIGNAL) {
while ((record = log_queue_dequeue(log)) != NULL) {
log_queue_report_drops(log);
log_pipeline_mirror(log, record);
log_push_record(record, log);
log_queue_increment_counter(log->metrics->queue_drain_counter, 1);
flb_free(record);
}
log_queue_report_drops(log);
}
}
}
}
while ((record = log_queue_dequeue(log)) != NULL) {
log_queue_report_drops(log);
log_pipeline_mirror(log, record);
log_push_record(record, log);
log_queue_increment_counter(log->metrics->queue_drain_counter, 1);
flb_free(record);
}
log_queue_report_drops(log);
pthread_exit(NULL);
}
struct flb_log_cache *flb_log_cache_create(int timeout_seconds, int size)
{
int i;
struct flb_log_cache *cache;
struct flb_log_cache_entry *entry;
if (size <= 0) {
return NULL;
}
cache = flb_calloc(1, sizeof(struct flb_log_cache));
if (!cache) {
flb_errno();
return NULL;
}
cache->timeout = timeout_seconds;
mk_list_init(&cache->entries);
for (i = 0; i < size; i++) {
entry = flb_calloc(1, sizeof(struct flb_log_cache_entry));
if (!entry) {
flb_errno();
flb_log_cache_destroy(cache);
return NULL;
}
entry->buf = flb_sds_create_size(FLB_LOG_CACHE_TEXT_BUF_SIZE);
if (!entry->buf) {
flb_errno();
flb_free(entry);
flb_log_cache_destroy(cache);
return NULL;
}
entry->timestamp = 0; /* unset for now */
mk_list_add(&entry->_head, &cache->entries);
}
return cache;
}
void flb_log_cache_destroy(struct flb_log_cache *cache)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_log_cache_entry *entry;
if (!cache) {
return;
}
mk_list_foreach_safe(head, tmp, &cache->entries) {
entry = mk_list_entry(head, struct flb_log_cache_entry, _head);
flb_sds_destroy(entry->buf);
mk_list_del(&entry->_head);
flb_free(entry);
}
flb_free(cache);
}
struct flb_log_cache_entry *flb_log_cache_exists(struct flb_log_cache *cache, char *msg_buf, size_t msg_size)
{
size_t size;
struct mk_list *head;
struct flb_log_cache_entry *entry;
if (msg_size <= 1) {
return NULL;
}
/* number of bytes to compare */
size = msg_size / 2;
mk_list_foreach(head, &cache->entries) {
entry = mk_list_entry(head, struct flb_log_cache_entry, _head);
if (entry->timestamp == 0) {
continue;
}
if (flb_sds_len(entry->buf) < size) {
continue;
}
if (strncmp(entry->buf, msg_buf, size) == 0) {
return entry;
}
}
return NULL;
}
/* returns an unused entry or the oldest one */
struct flb_log_cache_entry *flb_log_cache_get_target(struct flb_log_cache *cache, uint64_t ts)
{
struct mk_list *head;
struct flb_log_cache_entry *entry;
struct flb_log_cache_entry *target = NULL;
mk_list_foreach(head, &cache->entries) {
entry = mk_list_entry(head, struct flb_log_cache_entry, _head);
/* unused entry */
if (entry->timestamp == 0) {
return entry;
}
/* expired entry */
if (entry->timestamp + entry->interval <= ts) {
return entry;
}
/* keep a reference to the oldest entry to sacrifice it */
if (!target || entry->timestamp < target->timestamp) {
target = entry;
}
}
return target;
}
/*
* should the incoming message to be suppressed because already one similar exists in
* the cache ?
*
* if no similar message exists, then the incoming message is added to the cache.
*/
int flb_log_cache_check_suppress_interval(struct flb_log_cache *cache,
char *msg_buf, size_t msg_size,
int interval_seconds)
{
uint64_t now = 0;
flb_sds_t buf;
struct flb_log_cache_entry *entry;
if (msg_size > INT_MAX) {
return FLB_FALSE;
}
now = time(NULL);
entry = flb_log_cache_exists(cache, msg_buf, msg_size);
/* if no similar message found, add the incoming message to the cache */
if (!entry) {
/* look for an unused entry or the oldest one */
entry = flb_log_cache_get_target(cache, now);
/* if no target entry is available just return, do not suppress the message */
if (!entry) {
return FLB_FALSE;
}
/* add the message to the cache */
buf = flb_sds_copy(entry->buf, msg_buf, msg_size);
if (!buf) {
return FLB_FALSE;
}
entry->buf = buf;
entry->timestamp = now;
entry->interval = interval_seconds;
return FLB_FALSE;
}
else {
if (entry->timestamp + interval_seconds > now) {
entry->interval = interval_seconds;
return FLB_TRUE;
}
else {
entry->timestamp = now;
entry->interval = interval_seconds;
return FLB_FALSE;
}
}
return FLB_TRUE;
}
int flb_log_cache_check_suppress(struct flb_log_cache *cache,
char *msg_buf, size_t msg_size)
{
return flb_log_cache_check_suppress_interval(cache, msg_buf, msg_size,
cache->timeout);
}
int flb_log_worker_destroy(struct flb_worker *worker)
{
(void) worker;
return 0;
}
int flb_log_worker_init(struct flb_worker *worker)
{
struct flb_log_cache *cache;
/* Log cache to reduce noise */
cache = flb_log_cache_create(10, FLB_LOG_CACHE_ENTRIES);
if (!cache) {
return -1;
}
worker->log_cache = cache;
return 0;
}
int flb_log_set_level(struct flb_config *config, int level)
{
config->log->level = level;
return 0;
}
int flb_log_get_level_str(char *str)
{
if (strcasecmp(str, "off") == 0) {
return FLB_LOG_OFF;
}
else if (strcasecmp(str, "error") == 0) {
return FLB_LOG_ERROR;
}
else if (strcasecmp(str, "warn") == 0 || strcasecmp(str, "warning") == 0) {
return FLB_LOG_WARN;
}
else if (strcasecmp(str, "info") == 0) {
return FLB_LOG_INFO;
}
else if (strcasecmp(str, "debug") == 0) {
return FLB_LOG_DEBUG;
}
else if (strcasecmp(str, "trace") == 0) {
return FLB_LOG_TRACE;
}
return -1;
}
static inline const char *flb_log_message_type_str(int type)
{
switch (type) {
case FLB_LOG_HELP:
return "help";
case FLB_LOG_INFO:
return "info";
case FLB_LOG_WARN:
return "warn";
case FLB_LOG_ERROR:
return "error";
case FLB_LOG_DEBUG:
return "debug";
case FLB_LOG_IDEBUG:
return "debug";
case FLB_LOG_TRACE:
return "trace";
default:
return NULL;
}
}
int flb_log_set_file(struct flb_config *config, char *out)
{
struct flb_log *log = config->log;
if (out) {
log_close_sink(log);
log->type = FLB_LOG_FILE;
log->out = out;
if (log_open_sink(log) == -1) {
log->type = FLB_LOG_STDERR;
log->out = NULL;
}
}
else {
log_close_sink(log);
log->type = FLB_LOG_STDERR;
log->out = NULL;
}
return 0;
}
int flb_log_pipeline_enable(struct flb_config *config)
{
int ret;
struct flb_log *log;
log = config->log;
if (log == NULL) {
return -1;
}
if (log->pipeline_enabled == FLB_TRUE) {
return 0;
}
ret = flb_pipe_create(log->pipeline_ch);
if (ret != 0) {
return -1;
}
flb_pipe_set_nonblocking(log->pipeline_ch[0]);
flb_pipe_set_nonblocking(log->pipeline_ch[1]);
log->pipeline_enabled = FLB_TRUE;
return 0;
}
void flb_log_pipeline_disable(struct flb_config *config)
{
struct flb_log *log;
if (config == NULL || config->log == NULL) {
return;
}
log = config->log;
if (log->pipeline_enabled == FLB_FALSE) {
return;
}
log->pipeline_enabled = FLB_FALSE;
log_queue_cleanup(&log->pipeline_queue);
flb_pipe_destroy(log->pipeline_ch);
log->pipeline_ch[0] = -1;
log->pipeline_ch[1] = -1;
}
flb_pipefd_t flb_log_pipeline_get_event_fd(struct flb_config *config)
{
if (config == NULL || config->log == NULL) {
return -1;
}
return config->log->pipeline_ch[0];
}
struct flb_log_record *flb_log_pipeline_dequeue(struct flb_config *config)
{
if (config == NULL || config->log == NULL) {
return NULL;
}
return log_queue_pop(&config->log->pipeline_queue);
}
/* Frees the metrics instance and its associated resources. */
void flb_log_metrics_destroy(struct flb_log_metrics *metrics)
{
if (metrics == NULL) {
return;
}
if (metrics->cmt != NULL) {
cmt_destroy(metrics->cmt);
}
flb_free(metrics);
}
/*
* Create and register cmetrics for the runtime logger.
* The caller must free the returned struct using flb_log_metrics_destroy.
*/
struct flb_log_metrics *flb_log_metrics_create()
{
struct flb_log_metrics *metrics;
int log_message_type;
const char *message_type_str;
uint64_t ts;
int ret;
metrics = flb_calloc(1, sizeof(struct flb_log_metrics));
if (metrics == NULL) {
flb_errno();
return NULL;
}
metrics->cmt = cmt_create();
if (metrics->cmt == NULL) {
flb_log_metrics_destroy(metrics);
return NULL;
}
metrics->logs_total_counter = cmt_counter_create(metrics->cmt,
"fluentbit",
"logger",
"logs_total",
"Total number of logs",
1, (char *[]) {"message_type"});
if (metrics->logs_total_counter == NULL) {
flb_log_metrics_destroy(metrics);
return NULL;
}
metrics->queue_enqueue_counter = cmt_counter_create(metrics->cmt,
"fluentbit",
"logger",
"queue_enqueued_total",
"Total number of logger queue enqueues",
0, NULL);
if (metrics->queue_enqueue_counter == NULL) {
flb_log_metrics_destroy(metrics);
return NULL;
}
metrics->queue_drop_counter = cmt_counter_create(metrics->cmt,
"fluentbit",
"logger",
"queue_dropped_total",
"Total number of logger queue drops",
0, NULL);
if (metrics->queue_drop_counter == NULL) {
flb_log_metrics_destroy(metrics);
return NULL;
}
metrics->queue_drain_counter = cmt_counter_create(metrics->cmt,
"fluentbit",
"logger",
"queue_drained_total",
"Total number of logger queue records drained",
0, NULL);
if (metrics->queue_drain_counter == NULL) {
flb_log_metrics_destroy(metrics);
return NULL;
}
/* Initialize counters for log message types to 0. */
ts = cfl_time_now();
for (log_message_type = FLB_LOG_ERROR; log_message_type <= FLB_LOG_TRACE; log_message_type++) {
message_type_str = flb_log_message_type_str(log_message_type);
if (!message_type_str) {
break;