}
DEF_TEST(format_metric_family) {
+ hostname_set("example.com");
+
struct {
char const *name;
prometheus_metric_family_t pfam;
},
.want = "# HELP unit_test_total\n"
"# TYPE unit_test_total counter\n"
- "unit_test_total 42\n"
+ "unit_test_total{job=\"example.com\",instance=\"\"} 42\n"
"\n",
},
{
},
.want = "# HELP unittest\n"
"# TYPE unittest gauge\n"
- "unittest{foo=\"bar\"} 42\n"
+ "unittest{job=\"example.com\",instance=\"\",foo=\"bar\"} 42\n"
"\n",
},
{
},
.want = "# HELP unit_test\n"
"# TYPE unit_test untyped\n"
- "unit_test{metric_name=\"unit.test\"} 42\n"
+ "unit_test{job=\"example.com\",instance=\"\",metric_name="
+ "\"unit.test\"} 42\n"
"\n",
},
{
}
DEF_TEST(target_info) {
+ hostname_set("example.com");
+
struct {
char const *name;
label_set_t *resources;
.resources_num = 1,
.want = "# HELP target_info Target metadata\n"
"# TYPE target_info gauge\n"
- "target_info{foo=\"bar\"} 1\n\n",
+ "target_info{job=\"example.com\",instance=\"\",foo=\"bar\"} "
+ "1\n\n",
},
{
.name = "identical resources get deduplicated",
.resources_num = 2,
.want = "# HELP target_info Target metadata\n"
"# TYPE target_info gauge\n"
- "target_info{foo=\"bar\"} 1\n\n",
+ "target_info{job=\"example.com\",instance=\"\",foo=\"bar\"} "
+ "1\n\n",
},
{
.name = "service.name gets translated to job",
.resources_num = 1,
.want = "# HELP target_info Target metadata\n"
"# TYPE target_info gauge\n"
- "target_info{job=\"unittest\"} 1\n\n",
+ "target_info{job=\"unittest\",instance=\"\"} 1\n\n",
},
{
.name = "service.instance.id gets translated to instance",
.resources_num = 1,
.want = "# HELP target_info Target metadata\n"
"# TYPE target_info gauge\n"
- "target_info{instance=\"42\"} 1\n\n",
+ "target_info{job=\"example.com\",instance=\"42\"} 1\n\n",
},
{
.name = "multiple resources",
"# collectd/write_prometheus " PACKAGE_VERSION " at example.com\n",
// clang-format on
},
+ {
+ .name = "job defaults to hostname_g, instance defaults to an empty "
+ "string",
+ .fams =
+ &(metric_family_t){
+ .name = "unit.test",
+ .type = METRIC_TYPE_GAUGE,
+ .resource =
+ {
+ .ptr =
+ (label_pair_t[]){
+ {"host.name", "example.org"},
+ },
+ .num = 1,
+ },
+ .metric =
+ {
+ .ptr =
+ &(metric_t){
+ .value.gauge = 42,
+ },
+ .num = 1,
+ },
+ },
+ .fams_num = 1,
+ // clang-format off
+ .want =
+ "# HELP target_info Target metadata\n"
+ "# TYPE target_info gauge\n"
+ "target_info{job=\"example.com\",instance=\"\",host_name=\"example.org\"} 1\n"
+ "\n"
+ "# HELP unit_test\n"
+ "# TYPE unit_test gauge\n"
+ "unit_test{job=\"example.com\",instance=\"\"} 42\n"
+ "\n"
+ "# collectd/write_prometheus " PACKAGE_VERSION " at example.com\n",
+ // clang-format on
+ },
};
for (size_t i = 0; i < STATIC_ARRAY_SIZE(cases); i++) {