Skip to content
This repository has been archived by the owner on Feb 25, 2023. It is now read-only.

Commit

Permalink
Stats to file (twitter#186)
Browse files Browse the repository at this point in the history
* add support for logging stats (in CSV format with each entry as name:value) to a file
  • Loading branch information
thinkingfish authored Jan 17, 2019
1 parent 2168fec commit 4acc53a
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 1 deletion.
38 changes: 38 additions & 0 deletions include/cc_stats_log.h
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

1 change: 0 additions & 1 deletion src/cc_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <cc_log.h>
#include <cc_mm.h>
#include <cc_print.h>
#include <time/cc_wheel.h>

#include <ctype.h>
#include <errno.h>
Expand Down
1 change: 1 addition & 0 deletions src/stats/CMakeLists.txt
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)
88 changes: 88 additions & 0 deletions src/stats/cc_stats_log.c
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);
}

0 comments on commit 4acc53a

Please sign in to comment.