-/* $OpenBSD: ometric.c,v 1.2 2022/11/01 13:35:09 claudio Exp $ */
+/* $OpenBSD: ometric.c,v 1.3 2022/11/30 10:15:01 claudio Exp $ */
/*
* Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
}
}
-static void
-ometric_output_labels(const struct olabels *ol)
+static int
+ometric_output_labels(FILE *out, const struct olabels *ol)
{
struct olabel *l;
const char *comma = "";
- if (ol == NULL) {
- printf(" ");
- return;
- }
+ if (ol == NULL)
+ return fprintf(out, " ");
- printf("{");
+ if (fprintf(out, "{") < 0)
+ return -1;
while (ol != NULL) {
STAILQ_FOREACH(l, &ol->labels, entry) {
- printf("%s%s=\"%s\"", comma, l->key, l->value);
+ if (fprintf(out, "%s%s=\"%s\"", comma, l->key,
+ l->value) < 0)
+ return -1;
comma = ",";
}
ol = ol->next;
}
- printf("} ");
+ return fprintf(out, "} ");
}
-static void
-ometric_output_value(const struct ovalue *ov)
+static int
+ometric_output_value(FILE *out, const struct ovalue *ov)
{
switch (ov->valtype) {
case OVT_INTEGER:
- printf("%llu", ov->value.i);
- return;
+ return fprintf(out, "%llu", ov->value.i);
case OVT_DOUBLE:
- printf("%g", ov->value.f);
- return;
+ return fprintf(out, "%g", ov->value.f);
}
+ return -1;
}
/*
* Output all metric values with TYPE and optional HELP strings.
*/
-void
-ometric_output_all(void)
+int
+ometric_output_all(FILE *out)
{
struct ometric *om;
struct ovalue *ov;
STAILQ_FOREACH(om, &ometrics, entry) {
if (om->help)
- printf("# HELP %s %s\n", om->name, om->help);
- printf("# TYPE %s %s\n", om->name, ometric_type(om->type));
+ if (fprintf(out, "# HELP %s %s\n", om->name,
+ om->help) < 0)
+ return -1;
+
+ if (fprintf(out, "# TYPE %s %s\n", om->name,
+ ometric_type(om->type)) < 0)
+ return -1;
STAILQ_FOREACH(ov, &om->vals, entry) {
- printf("%s", om->name);
- ometric_output_labels(ov->labels);
- ometric_output_value(ov);
- printf("\n");
+ if (fprintf(out, "%s", om->name) < 0)
+ return -1;
+ if (ometric_output_labels(out, ov->labels) < 0)
+ return -1;
+ if (ometric_output_value(out, ov) < 0)
+ return -1;
+ if (fprintf(out, "\n") < 0)
+ return -1;
}
}
- printf("# EOF\n");
+ if (fprintf(out, "# EOF\n") < 0)
+ return -1;
+ return 0;
}
/*
-/* $OpenBSD: ometric.h,v 1.1 2022/10/17 12:01:19 claudio Exp $ */
+/* $OpenBSD: ometric.h,v 1.2 2022/11/30 10:15:01 claudio Exp $ */
/*
* Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
struct olabels *olabels_new(const char * const *, const char **);
void olabels_free(struct olabels *);
-void ometric_output_all(void);
+int ometric_output_all(FILE *);
-/* XXX how to pass attributes */
/* functions to set gauge and counter metrics */
void ometric_set_int(struct ometric *, uint64_t, struct olabels *);
void ometric_set_float(struct ometric *, double, struct olabels *);
-/* $OpenBSD: output_ometric.c,v 1.3 2022/11/07 11:33:24 mbuhl Exp $ */
+/* $OpenBSD: output_ometric.c,v 1.4 2022/11/30 10:15:01 claudio Exp $ */
/*
* Copyright (c) 2022 Claudio Jeker <claudio@openbsd.org>
values[3] = NULL;
ol = olabels_new(keys, values);
-
ometric_set_info(bgpd_info, NULL, NULL, ol);
-
olabels_free(ol);
/*
(double)elapsed_time.tv_usec / 1000000;
ometric_set_float(bgpd_scrape_time, scrape, NULL);
- ometric_output_all();
+ ometric_output_all(stdout);
ometric_free_all();
}