Metrics: Refactor to drop usage of strv

This addresses Daan's feedback on #39202
This commit is contained in:
Yaping Li
2026-02-11 12:43:28 +01:00
committed by Zbigniew Jędrzejewski-Szmek
parent 5b501abfc2
commit a0f2d74cba
4 changed files with 49 additions and 73 deletions
+21 -10
View File
@@ -25,7 +25,7 @@ static int unit_active_state_build_json(MetricFamilyContext *context, void *user
context,
unit->id,
unit_active_state_to_string(unit_active_state(unit)),
/* field_pairs= */ NULL);
/* fields= */ NULL);
if (r < 0)
return r;
}
@@ -50,7 +50,7 @@ static int unit_load_state_build_json(MetricFamilyContext *context, void *userda
context,
unit->id,
unit_load_state_to_string(unit->load_state),
/* field_pairs= */ NULL);
/* fields= */ NULL);
if (r < 0)
return r;
}
@@ -66,7 +66,7 @@ static int nrestarts_build_json(MetricFamilyContext *context, void *userdata) {
LIST_FOREACH(units_by_type, unit, manager->units_by_type[UNIT_SERVICE]) {
r = metric_build_send_unsigned(
context, unit->id, SERVICE(unit)->n_restarts, /* field_pairs= */ NULL);
context, unit->id, SERVICE(unit)->n_restarts, /* fields= */ NULL);
if (r < 0)
return r;
}
@@ -81,16 +81,21 @@ static int units_by_type_total_build_json(MetricFamilyContext *context, void *us
assert(context);
for (UnitType type = 0; type < _UNIT_TYPE_MAX; type++) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *fields = NULL;
uint64_t counter = 0;
LIST_FOREACH(units_by_type, _u, manager->units_by_type[type])
counter++;
r = sd_json_buildo(&fields, SD_JSON_BUILD_PAIR_STRING("type", unit_type_to_string(type)));
if (r < 0)
return r;
r = metric_build_send_unsigned(
context,
/* object= */ NULL,
counter,
STRV_MAKE("type", unit_type_to_string(type)));
fields);
if (r < 0)
return r;
}
@@ -117,11 +122,17 @@ static int units_by_state_total_build_json(MetricFamilyContext *context, void *u
}
for (UnitActiveState state = 0; state < _UNIT_ACTIVE_STATE_MAX; state++) {
_cleanup_(sd_json_variant_unrefp) sd_json_variant *fields = NULL;
r = sd_json_buildo(&fields, SD_JSON_BUILD_PAIR_STRING("state", unit_active_state_to_string(state)));
if (r < 0)
return r;
r = metric_build_send_unsigned(
context,
/* object= */ NULL,
counters[state],
STRV_MAKE("state", unit_active_state_to_string(state)));
fields);
if (r < 0)
return r;
}
@@ -135,31 +146,31 @@ const MetricFamily metric_family_table[] = {
.name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "nrestarts",
.description = "Per unit metric: number of restarts",
.type = METRIC_FAMILY_TYPE_COUNTER,
.generate_cb = nrestarts_build_json,
.generate = nrestarts_build_json,
},
{
.name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "unit_active_state",
.description = "Per unit metric: active state",
.type = METRIC_FAMILY_TYPE_STRING,
.generate_cb = unit_active_state_build_json,
.generate = unit_active_state_build_json,
},
{
.name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "unit_load_state",
.description = "Per unit metric: load state",
.type = METRIC_FAMILY_TYPE_STRING,
.generate_cb = unit_load_state_build_json,
.generate = unit_load_state_build_json,
},
{
.name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "units_by_state_total",
.description = "Total number of units of different state",
.type = METRIC_FAMILY_TYPE_GAUGE,
.generate_cb = units_by_state_total_build_json,
.generate = units_by_state_total_build_json,
},
{
.name = METRIC_IO_SYSTEMD_MANAGER_PREFIX "units_by_type_total",
.description = "Total number of units of different types",
.type = METRIC_FAMILY_TYPE_GAUGE,
.generate_cb = units_by_type_total_build_json,
.generate = units_by_type_total_build_json,
},
{}
};