return 0;
} /* int format_name */
-int format_values(char *ret, size_t ret_len, /* {{{ */
- metric_single_t const *m, bool store_rates) {
- ret[0] = 0;
- strbuf_t buf = STRBUF_CREATE_FIXED(ret, ret_len);
+int format_values(strbuf_t *buf, metric_t const *m, bool store_rates) {
+ strbuf_printf(buf, "%.3f", CDTIME_T_TO_DOUBLE(m->time));
- strbuf_printf(&buf, "%.3f", CDTIME_T_TO_DOUBLE(m->time));
-
- if (m->value_type == DS_TYPE_GAUGE)
- strbuf_printf(&buf, ":" GAUGE_FORMAT, m->value.gauge);
+ if (m->family->type == METRIC_TYPE_GAUGE)
+ strbuf_printf(buf, ":" GAUGE_FORMAT, m->value.gauge);
else if (store_rates) {
gauge_t rates = NAN;
int status = uc_get_rate(m, &rates);
WARNING("format_values: uc_get_rate failed.");
return status;
}
- strbuf_printf(&buf, ":" GAUGE_FORMAT, rates);
- } else if (m->value_type == DS_TYPE_COUNTER)
- strbuf_printf(&buf, ":%" PRIu64, (uint64_t)m->value.counter);
- else if (m->value_type == DS_TYPE_DERIVE)
- strbuf_printf(&buf, ":%" PRIi64, m->value.derive);
- else {
- ERROR("format_values: Unknown data source type: %i", m->value_type);
+ strbuf_printf(buf, ":" GAUGE_FORMAT, rates);
+ } else if (m->family->type == METRIC_TYPE_COUNTER) {
+ strbuf_printf(buf, ":%" PRIu64, m->value.counter);
+ } else if (m->family->type == DS_TYPE_DERIVE) {
+ strbuf_printf(buf, ":%" PRIi64, m->value.derive);
+ } else {
+ ERROR("format_values: Unknown metric type: %d", m->family->type);
return -1;
}
return 0;
} /* }}} int format_values */
-int format_values_vl(char *ret, size_t ret_len, /* {{{ */
- const data_set_t *ds, const value_list_t *vl,
- bool store_rates) {
- metrics_list_t *ml = NULL;
- assert(0 == strcmp(ds->type, vl->type));
- int retval = plugin_convert_values_to_metrics(vl, &ml);
- if (retval != 0) {
- return retval;
- }
- metrics_list_t *index_p = ml;
- while (index_p != NULL) {
- retval = format_values(ret, ret_len, &index_p->metric, store_rates);
- if (retval != 0) {
- destroy_metrics_list(ml);
- return retval;
- }
- ret[strlen(ret)] = '\n';
- ret_len -= strlen(ret) + 1;
- index_p = index_p->next_p;
- }
- destroy_metrics_list(ml);
- return 0;
-}
-
int parse_identifier(char *str, char **ret_host, char **ret_plugin,
char **ret_type, char **ret_data_source,
char *default_host) {
} /* int notification_init */
int notification_init_metric(notification_t *n, int severity,
- char const *message, metric_single_t const *m) {
+ char const *message, metric_t const *m) {
if ((n == NULL) || (message == NULL) || (m == NULL)) {
return EINVAL;
}
return 0;
}
- /* Counter and absolute can't handle negative rates. Reset "last time"
- * to zero, so that the next valid rate will re-initialize the
- * structure. */
+ /* Counter can't handle negative rates. Reset "last time" to zero, so that
+ * the next valid rate will re-initialize the structure. */
if ((rate < 0.0) && (ds_type == DS_TYPE_COUNTER)) {
memset(state, 0, sizeof(*state));
return EINVAL;
#define FORMAT_VL(ret, ret_len, vl) \
format_name(ret, ret_len, (vl)->host, (vl)->plugin, (vl)->plugin_instance, \
(vl)->type, (vl)->type_instance)
-int format_values(char *ret, size_t ret_len, metric_single_t const *m,
- bool store_rates);
-int format_values_vl(char *ret, size_t ret_len, const data_set_t *ds,
- const value_list_t *vl, bool store_rates);
+int format_values(strbuf_t *buf, metric_t const *m, bool store_rates);
int parse_identifier(char *str, char **ret_host, char **ret_plugin,
char **ret_type, char **ret_data_source,
#endif
int notification_init_metric(notification_t *n, int severity,
- const char *message, metric_single_t const *m);
+ const char *message, metric_t const *m);
int notification_init(notification_t *n, int severity, const char *message,
const char *host, const char *plugin,
DEF_TEST(format_values) {
struct {
- int ds_type;
+ metric_type_t type;
value_t value;
char const *want;
} cases[] = {
- {DS_TYPE_GAUGE, (value_t){.gauge = 47.11}, "1592558427.435:47.11"},
- {DS_TYPE_GAUGE, (value_t){.gauge = NAN}, "1592558427.435:nan"},
- {DS_TYPE_DERIVE, (value_t){.derive = 42}, "1592558427.435:42"},
- {DS_TYPE_COUNTER, (value_t){.counter = 18446744073709551615LLU},
+ {METRIC_TYPE_GAUGE, (value_t){.gauge = 47.11}, "1592558427.435:47.11"},
+ {METRIC_TYPE_GAUGE, (value_t){.gauge = NAN}, "1592558427.435:nan"},
+ {METRIC_TYPE_COUNTER, (value_t){.counter = 18446744073709551615LLU},
"1592558427.435:18446744073709551615"},
};
for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {
- char buf[1024];
-
- data_set_t ds = {
- .type = "testing",
- .ds_num = 1,
- .ds =
- &(data_source_t){
- .type = cases[i].ds_type,
- },
- };
- value_list_t vl = {
- .type = "testing",
- .values = &cases[i].value,
- .values_len = 1,
- .time = 1709996590700628541,
- };
-
- EXPECT_EQ_INT(0, format_values(buf, sizeof(buf), &ds, &vl, false));
+ metric_family_t fam =
+ {
+ .name = "testing",
+ .type = cases[i].type,
+ } metric_t m = {
+ .family = &fam,
+ .value = cases[i].value,
+ .time = MS_TO_CDTIME_T(1592558427435),
+ };
+ metric_list_add(&fam.metric, m);
+
+ strbuf_t buf = STRBUF_CREATE;
+
+ EXPECT_EQ_INT(0, format_values(&buf, &m, false));
EXPECT_EQ_STR(cases[i].want, buf);
+
+ STRBUF_DESTROY(buf);
}
return 0;