forked from twitter/pelikan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for logging stats (in CSV format with each entry as name:value) to a file
- Loading branch information
1 parent
2168fec
commit 4acc53a
Showing
4 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <cc_metric.h> | ||
#include <cc_option.h> | ||
|
||
|
||
#define STATS_LOG_FILE NULL /* default log file */ | ||
#define STATS_LOG_NBUF 0 /* default log buf size */ | ||
|
||
/* name type default description */ | ||
#define STATSLOG_OPTION(ACTION) \ | ||
ACTION( stats_log_file, OPTION_TYPE_STR, NULL, "file storing stats" )\ | ||
ACTION( stats_log_nbuf, OPTION_TYPE_UINT, STATS_LOG_NBUF, "stats log buf size" ) | ||
|
||
typedef struct { | ||
STATSLOG_OPTION(OPTION_DECLARE) | ||
} stats_log_options_st; | ||
|
||
|
||
/* dump stats as CSV records into a log file, this allows metrics to be captured | ||
* locally without setting up an observability infrastructure | ||
*/ | ||
void stats_log_setup(stats_log_options_st *options); | ||
void stats_log_teardown(void); | ||
|
||
void stats_log(struct metric metrics[], unsigned int nmetric); | ||
|
||
void stats_log_flush(void); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
set(SOURCE | ||
${SOURCE} | ||
stats/cc_metric.c | ||
stats/cc_stats_log.c | ||
PARENT_SCOPE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <cc_stats_log.h> | ||
|
||
#include <cc_debug.h> | ||
#include <cc_log.h> | ||
#include <cc_metric.h> | ||
|
||
#define STATS_LOG_MODULE_NAME "util::stats_log" | ||
#define STATS_LOG_FMT "%s: %s, " | ||
#define PRINT_BUF_LEN 64 | ||
|
||
static struct logger *slog = NULL; | ||
static bool stats_log_init = false; | ||
|
||
static char buf[PRINT_BUF_LEN]; | ||
|
||
|
||
void | ||
stats_log_setup(stats_log_options_st *options) | ||
{ | ||
size_t log_nbuf = STATS_LOG_NBUF; | ||
char *filename = STATS_LOG_FILE; | ||
|
||
log_info("set up the %s module", STATS_LOG_MODULE_NAME); | ||
|
||
if (stats_log_init) { | ||
log_warn("%s has already been setup, overwrite", STATS_LOG_MODULE_NAME); | ||
if (slog != NULL) { | ||
log_destroy(&slog); | ||
} | ||
} | ||
|
||
if (options != NULL) { | ||
filename = option_str(&options->stats_log_file); | ||
log_nbuf = option_uint(&options->stats_log_nbuf); | ||
} | ||
|
||
if (filename != NULL) { | ||
slog = log_create(filename, log_nbuf); | ||
if (slog == NULL) { | ||
log_warn("Could not create logger"); | ||
} | ||
} | ||
|
||
stats_log_init = true; | ||
} | ||
|
||
void | ||
stats_log_teardown(void) | ||
{ | ||
log_info("tear down the %s module", STATS_LOG_MODULE_NAME); | ||
|
||
if (!stats_log_init) { | ||
log_warn("%s has never been setup", STATS_LOG_MODULE_NAME); | ||
} | ||
|
||
if (slog != NULL) { | ||
log_destroy(&slog); | ||
} | ||
|
||
stats_log_init = false; | ||
} | ||
|
||
void | ||
stats_log(struct metric metrics[], unsigned int nmetric) | ||
{ | ||
unsigned int i; | ||
|
||
if (slog == NULL) { | ||
return; | ||
} | ||
|
||
for (i = 0; i < nmetric; i++, metrics++) { | ||
int len = 0; | ||
|
||
len = metric_print(buf, PRINT_BUF_LEN, STATS_LOG_FMT, metrics); | ||
log_write(slog, buf, len); | ||
} | ||
} | ||
|
||
void | ||
stats_log_flush(void) | ||
{ | ||
if (slog == NULL) { | ||
return; | ||
} | ||
log_flush(slog); | ||
} | ||
|