Skip to content
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

Stats to file #186

Merged
merged 6 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}