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

Commit

Permalink
add stats functions to print just name or value
Browse files Browse the repository at this point in the history
  • Loading branch information
Yao Yue committed Jan 15, 2019
1 parent 2168fec commit 286eece
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
2 changes: 2 additions & 0 deletions include/cc_metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ struct metric {
};

void metric_reset(struct metric sarr[], unsigned int nmetric);
size_t metric_print_name(char *buf, size_t nbuf, char *fmt, struct metric *m);
size_t metric_print_value(char *buf, size_t nbuf, char *fmt, struct metric *m);
size_t metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m);
void metric_describe_all(struct metric metrics[], unsigned int nmetric);

Expand Down
56 changes: 43 additions & 13 deletions src/stats/cc_metric.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,38 +58,68 @@ metric_reset(struct metric sarr[], unsigned int n)
}
}

size_t
metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m)
static void
_metric_value(char *buf, struct metric *m)
{
char val_buf[VALUE_PRINT_LEN];

if (m == NULL) {
return 0;
}

switch(m->type) {
case METRIC_COUNTER:
/**
* not using cc_print_uint64, since it would complicate implementation
* and negatively impact readability, and since this function should not
* be called often enough to make it absolutely performance critical.
*/
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%llu", __atomic_load_n(
&m->counter, __ATOMIC_RELAXED));
cc_scnprintf(buf, VALUE_PRINT_LEN, "%llu", __atomic_load_n(&m->counter,
__ATOMIC_RELAXED));
break;

case METRIC_GAUGE:
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%lld", __atomic_load_n(
&m->gauge, __ATOMIC_RELAXED));
cc_scnprintf(buf, VALUE_PRINT_LEN, "%lld", __atomic_load_n(&m->gauge,
__ATOMIC_RELAXED));
break;

case METRIC_FPN:
cc_scnprintf(val_buf, VALUE_PRINT_LEN, "%f", m->fpn);
cc_scnprintf(buf, VALUE_PRINT_LEN, "%f", m->fpn);
break;

default:
NOT_REACHED();
}
}

size_t
metric_print_name(char *buf, size_t nbuf, char *fmt, struct metric *m)
{
if (m == NULL) {
return 0;
}

return cc_scnprintf(buf, nbuf, fmt, m->name);
}

size_t
metric_print_value(char *buf, size_t nbuf, char *fmt, struct metric *m)
{
char val_buf[VALUE_PRINT_LEN];

if (m == NULL) {
return 0;
}

_metric_value(val_buf, m);

return cc_scnprintf(buf, nbuf, fmt, val_buf);
}

size_t
metric_print(char *buf, size_t nbuf, char *fmt, struct metric *m)
{
char val_buf[VALUE_PRINT_LEN];

if (m == NULL) {
return 0;
}

_metric_value(val_buf, m);

return cc_scnprintf(buf, nbuf, fmt, m->name, val_buf);
}
Expand Down

0 comments on commit 286eece

Please sign in to comment.