Skip to content

Commit 30895be

Browse files
committed
Feedback (temporary -- will squash into above)
Signed-off-by: Alec Holmes <[email protected]>
1 parent 9f176f1 commit 30895be

File tree

6 files changed

+126
-128
lines changed

6 files changed

+126
-128
lines changed

include/fluent-bit/flb_config.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ struct flb_config {
164164
/* Logging */
165165
char *log_file;
166166
struct flb_log *log;
167-
struct flb_log_metrics *log_metrics_ctx; /* Global metrics for logging calls */
168167

169168
/* Parser Conf */
170169
char *parsers_file;

include/fluent-bit/flb_log.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ struct flb_log {
7474
pthread_t tid; /* thread ID */
7575
struct flb_worker *worker; /* non-real worker reference */
7676
struct mk_event_loop *evl;
77+
struct flb_log_metrics *metrics;
7778

7879
/* Initialization variables */
7980
int pth_init;
@@ -242,8 +243,6 @@ static inline int flb_log_suppress_check(int log_suppress_interval, const char *
242243
int flb_log_worker_init(struct flb_worker *worker);
243244
int flb_log_worker_destroy(struct flb_worker *worker);
244245
int flb_errno_print(int errnum, const char *file, int line);
245-
struct flb_log_metrics *flb_log_metrics_create();
246-
void flb_log_metrics_destroy(struct flb_log_metrics *ctx);
247246
#ifdef WIN32
248247
int flb_wsa_get_last_error_print(int errnum, const char *file, int line);
249248
#endif

src/flb_config.c

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,6 @@ struct flb_config *flb_config_init()
400400
flb_regex_init();
401401
#endif
402402

403-
/* Create internal logger metrics */
404-
config->log_metrics_ctx = flb_log_metrics_create();
405-
if (!config->log_metrics_ctx) {
406-
flb_error("[engine] could not create log metrics");
407-
flb_config_exit(config);
408-
return NULL;
409-
}
410-
411403
return config;
412404
}
413405

@@ -578,11 +570,6 @@ void flb_config_exit(struct flb_config *config)
578570
flb_config_task_map_resize(config, 0);
579571
flb_routes_empty_mask_destroy(config);
580572

581-
/* Destroy internal logger metrics */
582-
if (config->log_metrics_ctx) {
583-
flb_log_metrics_destroy(config->log_metrics_ctx);
584-
}
585-
586573
flb_free(config);
587574
}
588575

src/flb_log.c

Lines changed: 113 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -440,6 +440,28 @@ int flb_log_get_level_str(char *str)
440440
return -1;
441441
}
442442

443+
static inline const char *flb_log_message_type_str(int type)
444+
{
445+
switch (type) {
446+
case FLB_LOG_HELP:
447+
return "help";
448+
case FLB_LOG_INFO:
449+
return "info";
450+
case FLB_LOG_WARN:
451+
return "warn";
452+
case FLB_LOG_ERROR:
453+
return "error";
454+
case FLB_LOG_DEBUG:
455+
return "debug";
456+
case FLB_LOG_IDEBUG:
457+
return "debug";
458+
case FLB_LOG_TRACE:
459+
return "trace";
460+
default:
461+
return NULL;
462+
}
463+
}
464+
443465
int flb_log_set_file(struct flb_config *config, char *out)
444466
{
445467
struct flb_log *log = config->log;
@@ -456,6 +478,78 @@ int flb_log_set_file(struct flb_config *config, char *out)
456478
return 0;
457479
}
458480

481+
482+
/*
483+
* Create and register cmetrics for the runtime logger.
484+
* The caller must free the returned struct using flb_log_metrics_destroy.
485+
*/
486+
struct flb_log_metrics *flb_log_metrics_create()
487+
{
488+
struct flb_log_metrics *metrics;
489+
int i;
490+
const char *message_type_str;
491+
uint64_t ts;
492+
int ret;
493+
494+
metrics = flb_calloc(1, sizeof(struct flb_log_metrics));
495+
if (metrics == NULL) {
496+
flb_errno();
497+
return NULL;
498+
}
499+
500+
metrics->cmt = cmt_create();
501+
if (!metrics->cmt) {
502+
flb_free(metrics);
503+
return NULL;
504+
}
505+
506+
metrics->logs_total_counter = cmt_counter_create(metrics->cmt,
507+
"fluentbit",
508+
"logger",
509+
"logs_total",
510+
"Total number of logs",
511+
1, (char *[]) {"message_type"});
512+
if (metrics->logs_total_counter == NULL) {
513+
cmt_destroy(metrics->cmt);
514+
flb_free(metrics);
515+
return NULL;
516+
}
517+
518+
/*
519+
* Initialize counters for all log message types to 0.
520+
* This assumes types are contiguous starting at 1 (FLB_LOG_ERROR).
521+
*/
522+
ts = cfl_time_now();
523+
for (i = 1; ; i++) {
524+
message_type_str = flb_log_message_type_str(i);
525+
if (!message_type_str) {
526+
break;
527+
}
528+
529+
ret = cmt_counter_set(metrics->logs_total_counter,
530+
ts,
531+
0,
532+
1, (char *[]) {message_type_str});
533+
if (ret == -1) {
534+
cmt_counter_destroy(metrics->logs_total_counter);
535+
cmt_destroy(metrics->cmt);
536+
flb_free(metrics);
537+
return NULL;
538+
}
539+
}
540+
541+
return metrics;
542+
}
543+
544+
/* Frees the metrics instance and its associated resources. */
545+
void flb_log_metrics_destroy(struct flb_log_metrics *metrics)
546+
{
547+
cmt_counter_destroy(metrics->logs_total_counter);
548+
cmt_destroy(metrics->cmt);
549+
flb_free(metrics);
550+
}
551+
552+
459553
struct flb_log *flb_log_create(struct flb_config *config, int type,
460554
int level, char *out)
461555
{
@@ -509,6 +603,16 @@ struct flb_log *flb_log_create(struct flb_config *config, int type,
509603
return NULL;
510604
}
511605

606+
/* Create metrics */
607+
log->metrics = flb_log_metrics_create();
608+
if (log->metrics == NULL) {
609+
fprintf(stderr, "[log] could not create log metrics\n");
610+
mk_event_loop_destroy(log->evl);
611+
flb_free(log);
612+
config->log = NULL;
613+
return NULL;
614+
}
615+
512616
/*
513617
* Since the main process/thread might want to write log messages,
514618
* it will need a 'worker-like' context, here we create a fake worker
@@ -566,28 +670,6 @@ struct flb_log *flb_log_create(struct flb_config *config, int type,
566670
return log;
567671
}
568672

569-
static inline char *flb_log_message_type_str(int type)
570-
{
571-
switch (type) {
572-
case FLB_LOG_HELP:
573-
return "help";
574-
case FLB_LOG_INFO:
575-
return "info";
576-
case FLB_LOG_WARN:
577-
return "warn";
578-
case FLB_LOG_ERROR:
579-
return "error";
580-
case FLB_LOG_DEBUG:
581-
return "debug";
582-
case FLB_LOG_IDEBUG:
583-
return "debug";
584-
case FLB_LOG_TRACE:
585-
return "trace";
586-
default:
587-
return NULL;
588-
}
589-
}
590-
591673
int flb_log_construct(struct log_message *msg, int *ret_len,
592674
int type, const char *file, int line, const char *fmt, va_list *args)
593675
{
@@ -597,7 +679,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
597679
int total;
598680
time_t now;
599681
const char *header_color = NULL;
600-
const char *header_title = flb_log_message_type_str(type);
682+
const char *header_title;
601683
const char *bold_color = ANSI_BOLD;
602684
const char *reset_color = ANSI_RESET;
603685
struct tm result;
@@ -647,6 +729,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
647729
return -1;
648730
}
649731

732+
header_title = flb_log_message_type_str(type);
650733
len = snprintf(msg->msg, sizeof(msg->msg) - 1,
651734
"%s[%s%i/%02i/%02i %02i:%02i:%02i%s]%s [%s%5s%s] ",
652735
/* time */ /* type */
@@ -721,8 +804,8 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
721804
int ret;
722805
struct log_message msg = {0};
723806
va_list args;
724-
char *msg_type_str = flb_log_message_type_str(type);
725-
uint64_t ts = cfl_time_now();
807+
const char *msg_type_str;
808+
uint64_t ts;
726809

727810
struct flb_worker *w;
728811
struct flb_config *config;
@@ -738,8 +821,10 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
738821
w = flb_worker_get();
739822
if (w) {
740823
config = w->config;
741-
if (config && config->log_metrics_ctx) {
742-
cmt_counter_inc(config->log_metrics_ctx->logs_total_counter, ts, 1, (char *[]) {msg_type_str}); // Ignoring inc error
824+
if (config != NULL && config->log != NULL) {
825+
msg_type_str = flb_log_message_type_str(type);
826+
ts = cfl_time_now();
827+
cmt_counter_inc(config->log->metrics->logs_total_counter, ts, 1, (char *[]) {msg_type_str}); // Ignoring inc error
743828
}
744829

745830
n = flb_pipe_write_all(w->log[1], &msg, sizeof(msg));
@@ -801,79 +886,8 @@ int flb_log_destroy(struct flb_log *log, struct flb_config *config)
801886
}
802887
flb_log_worker_destroy(log->worker);
803888
flb_free(log->worker);
889+
flb_log_metrics_destroy(log->metrics);
804890
flb_free(log);
805891

806892
return 0;
807893
}
808-
809-
/*
810-
* Create and register cmetrics for the runtime logger.
811-
* The caller must free the returned struct using flb_log_metrics_destroy.
812-
*/
813-
struct flb_log_metrics *flb_log_metrics_create()
814-
{
815-
struct flb_log_metrics *lm;
816-
int i;
817-
char *message_type_str;
818-
uint64_t ts;
819-
820-
lm = flb_calloc(1, sizeof(struct flb_log_metrics));
821-
if (!lm) {
822-
flb_errno();
823-
goto error;
824-
}
825-
826-
lm->cmt = cmt_create();
827-
if (!lm->cmt) {
828-
goto error;
829-
}
830-
831-
lm->logs_total_counter = cmt_counter_create(lm->cmt,
832-
"fluentbit", "logger", "logs_total",
833-
"Total number of logs",
834-
1, (char *[]) {"message_type"});
835-
if (!lm->logs_total_counter) {
836-
goto error;
837-
}
838-
839-
/*
840-
* Initialize counters for all log message types to 0.
841-
* This assumes types are contiguous starting at 1 (FLB_LOG_ERROR).
842-
*/
843-
i = 1;
844-
ts = cfl_time_now();
845-
for (i = 1; ; i++) {
846-
message_type_str = flb_log_message_type_str(i);
847-
if (!message_type_str) {
848-
break;
849-
}
850-
851-
if (cmt_counter_set(lm->logs_total_counter, ts, 0, 1, (char *[]) {message_type_str}) == -1) {
852-
goto error;
853-
}
854-
}
855-
856-
return lm;
857-
858-
error:
859-
if (lm && lm->logs_total_counter) {
860-
cmt_counter_destroy(lm->logs_total_counter);
861-
}
862-
863-
if (lm && lm->cmt) {
864-
cmt_destroy(lm->cmt);
865-
}
866-
867-
if (lm) {
868-
flb_free(lm);
869-
}
870-
871-
return NULL;
872-
}
873-
874-
void flb_log_metrics_destroy(struct flb_log_metrics *lm)
875-
{
876-
cmt_counter_destroy(lm->logs_total_counter);
877-
cmt_destroy(lm->cmt);
878-
flb_free(lm);
879-
}

src/flb_metrics_exporter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,8 +299,8 @@ struct cmt *flb_me_get_cmetrics(struct flb_config *ctx)
299299
}
300300
}
301301

302-
if (ctx->log_metrics_ctx) {
303-
ret = cmt_cat(cmt, ctx->log_metrics_ctx->cmt);
302+
if (ctx->log != NULL) {
303+
ret = cmt_cat(cmt, ctx->log->metrics->cmt);
304304
if (ret == -1) {
305305
flb_error("[metrics exporter] could not append global log_metrics_ctx");
306306
cmt_destroy(cmt);

tests/runtime/core_internal_logger.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ void http_client_ctx_destroy(struct http_client_ctx *http_ctx)
7979
int assert_internal_log_metrics(struct http_client_ctx *http_ctx, int fail_test)
8080
{
8181
struct flb_http_client *http_client;
82-
int ret;
8382
size_t b_sent;
8483
struct flb_regex *regex;
8584

@@ -97,8 +96,8 @@ int assert_internal_log_metrics(struct http_client_ctx *http_ctx, int fail_test)
9796
TEST_ASSERT(flb_http_do(http_client, &b_sent) == 0);
9897

9998
if (http_client->resp.status != 200 && !fail_test) {
100-
ret = -1;
101-
goto cleanup;
99+
flb_http_client_destroy(http_client);
100+
return -1;
102101
}
103102

104103
TEST_MSG(http_client->resp.payload);
@@ -108,26 +107,26 @@ int assert_internal_log_metrics(struct http_client_ctx *http_ctx, int fail_test)
108107
}
109108

110109
/* There be no errors logged */
111-
if (!TEST_CHECK(strstr(http_client->resp.payload,
112-
"fluentbit_logger_logs_total{message_type=\"error\"} 0") != NULL)) {
110+
if (!TEST_CHECK(
111+
strstr(http_client->resp.payload,
112+
"fluentbit_logger_logs_total{message_type=\"error\"} 0")
113+
!= NULL)) {
113114
TEST_MSG("response payload: %s", http_client->resp.payload);
114115
}
115116

116117
/* The process startup should have logged at least 1 info log */
117-
regex = flb_regex_create("fluentbit_logger_logs_total\\{message_type=\"info\"\\} [1-9]+[0-9]*");
118+
regex = flb_regex_create(
119+
"fluentbit_logger_logs_total\\{message_type=\"info\"\\} [1-9]+[0-9]*");
118120
TEST_CHECK(regex != NULL);
119121
if (!TEST_CHECK(flb_regex_match(
120122
regex, http_client->resp.payload, http_client->resp.payload_size))) {
121123
TEST_MSG("response payload: %s\n", http_client->resp.payload);
122124
}
123-
flb_regex_destroy(regex);
124125

125-
ret = 0;
126-
127-
cleanup:
126+
flb_regex_destroy(regex);
128127
flb_http_client_destroy(http_client);
129128

130-
return ret;
129+
return 0;
131130
}
132131

133132
/*

0 commit comments

Comments
 (0)