-
Notifications
You must be signed in to change notification settings - Fork 1.7k
engine: expose internal logging call counts as internal metrics #10326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
b89fdac
e8d522f
8ff4ffb
a48e7ab
6388e14
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,8 @@ | |
#include <fluent-bit/flb_config.h> | ||
#include <fluent-bit/flb_worker.h> | ||
#include <fluent-bit/flb_mem.h> | ||
#include <cmetrics/cmetrics.h> | ||
#include <cmetrics/cmt_counter.h> | ||
|
||
#ifdef WIN32 | ||
#include <winsock.h> | ||
|
@@ -438,6 +440,28 @@ int flb_log_get_level_str(char *str) | |
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; | ||
|
@@ -454,6 +478,73 @@ int flb_log_set_file(struct flb_config *config, char *out) | |
return 0; | ||
} | ||
|
||
/* Frees the metrics instance and its associated resources. */ | ||
void flb_log_metrics_destroy(struct flb_log_metrics *metrics) | ||
{ | ||
if (metrics != NULL && metrics->cmt != NULL) { | ||
cmt_destroy(metrics->cmt); | ||
} | ||
if (metrics != NULL) { | ||
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; | ||
leonardo-albertovich marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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; | ||
} | ||
|
||
/* 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; | ||
} | ||
|
||
ret = cmt_counter_set(metrics->logs_total_counter, | ||
ts, | ||
0, | ||
1, (char *[]) {message_type_str}); | ||
if (ret == -1) { | ||
flb_log_metrics_destroy(metrics); | ||
return NULL; | ||
} | ||
} | ||
|
||
return metrics; | ||
} | ||
|
||
struct flb_log *flb_log_create(struct flb_config *config, int type, | ||
int level, char *out) | ||
{ | ||
|
@@ -507,6 +598,16 @@ struct flb_log *flb_log_create(struct flb_config *config, int type, | |
return NULL; | ||
} | ||
|
||
/* Create metrics */ | ||
log->metrics = flb_log_metrics_create(); | ||
if (log->metrics == NULL) { | ||
fprintf(stderr, "[log] could not create log metrics\n"); | ||
mk_event_loop_destroy(log->evl); | ||
flb_free(log); | ||
config->log = NULL; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While looking at this I noticed that I've checked the client code and verified that there are no threading issues, however, I wanted to use this as an example of bad logic for the new contributors. The problem is that the uninitialized log context is set to You don't need to change anything here, just look at it and if you have any questions ask them. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense to me. |
||
return NULL; | ||
} | ||
|
||
/* | ||
* Since the main process/thread might want to write log messages, | ||
* it will need a 'worker-like' context, here we create a fake worker | ||
|
@@ -573,39 +674,32 @@ int flb_log_construct(struct log_message *msg, int *ret_len, | |
int total; | ||
time_t now; | ||
const char *header_color = NULL; | ||
const char *header_title = NULL; | ||
const char *header_title; | ||
const char *bold_color = ANSI_BOLD; | ||
const char *reset_color = ANSI_RESET; | ||
struct tm result; | ||
struct tm *current; | ||
|
||
switch (type) { | ||
case FLB_LOG_HELP: | ||
header_title = "help"; | ||
header_color = ANSI_CYAN; | ||
break; | ||
case FLB_LOG_INFO: | ||
header_title = "info"; | ||
header_color = ANSI_GREEN; | ||
break; | ||
case FLB_LOG_WARN: | ||
header_title = "warn"; | ||
header_color = ANSI_YELLOW; | ||
break; | ||
case FLB_LOG_ERROR: | ||
header_title = "error"; | ||
header_color = ANSI_RED; | ||
break; | ||
case FLB_LOG_DEBUG: | ||
header_title = "debug"; | ||
header_color = ANSI_YELLOW; | ||
break; | ||
case FLB_LOG_IDEBUG: | ||
header_title = "debug"; | ||
header_color = ANSI_CYAN; | ||
break; | ||
case FLB_LOG_TRACE: | ||
header_title = "trace"; | ||
header_color = ANSI_BLUE; | ||
break; | ||
} | ||
|
@@ -630,6 +724,7 @@ int flb_log_construct(struct log_message *msg, int *ret_len, | |
return -1; | ||
} | ||
|
||
header_title = flb_log_message_type_str(type); | ||
len = snprintf(msg->msg, sizeof(msg->msg) - 1, | ||
"%s[%s%i/%02i/%02i %02i:%02i:%02i%s]%s [%s%5s%s] ", | ||
/* time */ /* type */ | ||
|
@@ -704,8 +799,11 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...) | |
int ret; | ||
struct log_message msg = {0}; | ||
va_list args; | ||
const char *msg_type_str; | ||
uint64_t ts; | ||
|
||
struct flb_worker *w; | ||
struct flb_config *config; | ||
|
||
va_start(args, fmt); | ||
ret = flb_log_construct(&msg, &len, type, file, line, fmt, &args); | ||
|
@@ -717,6 +815,25 @@ void flb_log_print(int type, const char *file, int line, const char *fmt, ...) | |
|
||
w = flb_worker_get(); | ||
if (w) { | ||
config = w->config; | ||
if (config != NULL && config->log != NULL) { | ||
msg_type_str = flb_log_message_type_str(type); | ||
if (msg_type_str == NULL) { | ||
msg_type_str = "unknown"; | ||
} | ||
|
||
ts = cfl_time_now(); | ||
ret = cmt_counter_inc(config->log->metrics->logs_total_counter, | ||
ts, | ||
1, (char *[]) {msg_type_str}); | ||
if (ret == -1) { | ||
/* Not using flb_log_debug to avoid recursing into this same function. */ | ||
fprintf(stderr, | ||
"[log] failed to increment log total counter for message type '%s' (error=%d)\n", | ||
msg_type_str, ret); | ||
} | ||
} | ||
|
||
n = flb_pipe_write_all(w->log[1], &msg, sizeof(msg)); | ||
|
||
if (n == -1) { | ||
|
@@ -776,6 +893,7 @@ int flb_log_destroy(struct flb_log *log, struct flb_config *config) | |
} | ||
flb_log_worker_destroy(log->worker); | ||
flb_free(log->worker); | ||
flb_log_metrics_destroy(log->metrics); | ||
flb_free(log); | ||
|
||
return 0; | ||
|
Uh oh!
There was an error while loading. Please reload this page.