Skip to content

Commit 701bf1f

Browse files
committed
engine: expose internal logging call counts as internal metrics
Internal logger calls increment a new v2 metric exposed by the HTTP server Prometheus scrape endpoint. There is one time series per log message type. Signed-off-by: Alec Holmes <[email protected]>
1 parent 0ae7e15 commit 701bf1f

File tree

8 files changed

+328
-11
lines changed

8 files changed

+328
-11
lines changed

include/fluent-bit/flb_config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ 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 */
167168

168169
/* Parser Conf */
169170
char *parsers_file;

include/fluent-bit/flb_log.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <fluent-bit/flb_worker.h>
2929
#include <fluent-bit/flb_config.h>
3030
#include <fluent-bit/flb_sds.h>
31+
#include <cmetrics/cmetrics.h>
32+
#include <cmetrics/cmt_counter.h>
3133
#include <inttypes.h>
3234
#include <errno.h>
3335
#include <stdarg.h>
@@ -92,6 +94,14 @@ struct flb_log_cache {
9294
struct mk_list entries; /* list for entries */
9395
};
9496

97+
/* Global metrics for logging calls. */
98+
struct flb_log_metrics {
99+
struct cmt *cmt;
100+
101+
/* cmetrics */
102+
struct cmt_counter *logs_total_counter; /* total number of logs (by message type) */
103+
};
104+
95105
/*
96106
* This function is used by plugins interface to check if an incoming log message
97107
* should be logged or not based in the log levels defined.
@@ -232,6 +242,8 @@ static inline int flb_log_suppress_check(int log_suppress_interval, const char *
232242
int flb_log_worker_init(struct flb_worker *worker);
233243
int flb_log_worker_destroy(struct flb_worker *worker);
234244
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);
235247
#ifdef WIN32
236248
int flb_wsa_get_last_error_print(int errnum, const char *file, int line);
237249
#endif

src/flb_config.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,14 @@ 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+
403411
return config;
404412
}
405413

@@ -570,6 +578,11 @@ void flb_config_exit(struct flb_config *config)
570578
flb_config_task_map_resize(config, 0);
571579
flb_routes_empty_mask_destroy(config);
572580

581+
/* Destroy internal logger metrics */
582+
if (config->log_metrics_ctx) {
583+
flb_log_metrics_destroy(config->log_metrics_ctx);
584+
}
585+
573586
flb_free(config);
574587
}
575588

src/flb_log.c

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include <fluent-bit/flb_config.h>
3333
#include <fluent-bit/flb_worker.h>
3434
#include <fluent-bit/flb_mem.h>
35+
#include <cmetrics/cmetrics.h>
36+
#include <cmetrics/cmt_counter.h>
3537

3638
#ifdef WIN32
3739
#include <winsock.h>
@@ -564,6 +566,28 @@ struct flb_log *flb_log_create(struct flb_config *config, int type,
564566
return log;
565567
}
566568

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+
567591
int flb_log_construct(struct log_message *msg, int *ret_len,
568592
int type, const char *file, int line, const char *fmt, va_list *args)
569593
{
@@ -573,39 +597,32 @@ int flb_log_construct(struct log_message *msg, int *ret_len,
573597
int total;
574598
time_t now;
575599
const char *header_color = NULL;
576-
const char *header_title = NULL;
600+
const char *header_title = flb_log_message_type_str(type);
577601
const char *bold_color = ANSI_BOLD;
578602
const char *reset_color = ANSI_RESET;
579603
struct tm result;
580604
struct tm *current;
581605

582606
switch (type) {
583607
case FLB_LOG_HELP:
584-
header_title = "help";
585608
header_color = ANSI_CYAN;
586609
break;
587610
case FLB_LOG_INFO:
588-
header_title = "info";
589611
header_color = ANSI_GREEN;
590612
break;
591613
case FLB_LOG_WARN:
592-
header_title = "warn";
593614
header_color = ANSI_YELLOW;
594615
break;
595616
case FLB_LOG_ERROR:
596-
header_title = "error";
597617
header_color = ANSI_RED;
598618
break;
599619
case FLB_LOG_DEBUG:
600-
header_title = "debug";
601620
header_color = ANSI_YELLOW;
602621
break;
603622
case FLB_LOG_IDEBUG:
604-
header_title = "debug";
605623
header_color = ANSI_CYAN;
606624
break;
607625
case FLB_LOG_TRACE:
608-
header_title = "trace";
609626
header_color = ANSI_BLUE;
610627
break;
611628
}
@@ -704,8 +721,11 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
704721
int ret;
705722
struct log_message msg = {0};
706723
va_list args;
724+
char *msg_type_str = flb_log_message_type_str(type);
725+
uint64_t ts = cfl_time_now();
707726

708727
struct flb_worker *w;
728+
struct flb_config *config;
709729

710730
va_start(args, fmt);
711731
ret = flb_log_construct(&msg, &len, type, file, line, fmt, &args);
@@ -717,6 +737,11 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...)
717737

718738
w = flb_worker_get();
719739
if (w) {
740+
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
743+
}
744+
720745
n = flb_pipe_write_all(w->log[1], &msg, sizeof(msg));
721746

722747
if (n == -1) {
@@ -780,3 +805,75 @@ int flb_log_destroy(struct flb_log *log, struct flb_config *config)
780805

781806
return 0;
782807
}
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,15 @@ 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);
304+
if (ret == -1) {
305+
flb_error("[metrics exporter] could not append global log_metrics_ctx");
306+
cmt_destroy(cmt);
307+
return NULL;
308+
}
309+
}
310+
302311
/* Pipeline metrics: input, filters, outputs */
303312
mk_list_foreach(head, &ctx->inputs) {
304313
i = mk_list_entry(head, struct flb_input_instance, _head);

src/flb_sds.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <stdarg.h>
3434
#include <ctype.h>
3535

36-
static flb_sds_t sds_alloc(size_t size)
36+
static flb_sds_t flb_sds_alloc_internal(size_t size)
3737
{
3838
void *buf;
3939
flb_sds_t s;
@@ -60,7 +60,7 @@ flb_sds_t flb_sds_create_len(const char *str, int len)
6060
flb_sds_t s;
6161
struct flb_sds *head;
6262

63-
s = sds_alloc(len);
63+
s = flb_sds_alloc_internal(len);
6464
if (!s) {
6565
return NULL;
6666
}
@@ -91,7 +91,7 @@ flb_sds_t flb_sds_create(const char *str)
9191

9292
flb_sds_t flb_sds_create_size(size_t size)
9393
{
94-
return sds_alloc(size);
94+
return flb_sds_alloc_internal(size);
9595
}
9696

9797
/* Increase SDS buffer size 'len' bytes */

tests/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ endmacro()
2525

2626
# Core
2727
FLB_RT_CORE_TEST(FLB_COROUTINE_TIMEOUT "core-timeout.c")
28+
FLB_RT_CORE_TEST(FLB_INTERNAL_LOGGER "core_internal_logger.c")
2829

2930
FLB_RT_TEST(FLB_CHUNK_TRACE "core_chunk_trace.c")
3031

0 commit comments

Comments
 (0)