mirror of
https://github.com/asterisk/asterisk.git
synced 2025-09-02 11:06:31 +00:00
res_prometheus: Fix duplicate output of metric and help text
The prometheus exposition format requires each line to be unique[1].
This is handled by struct prometheus_metric having a list of children
that is managed when registering a metric. In case the scrape callback
is used, it is the responsibility of the implementation to handle this
correctly.
Originally the bridge callback didn't handle NULL snapshots, the crash
fix lead to NULL metrics, and fixing that lead to duplicates.
The original code assumed that snapshots are not NULL and then relied on
"if (i > 0)" to establish the parent/children relationship between
metrics of the same class. This is not workerable as the first bridge
might be invisible/lacks a snapshot.
Fix this by keeping a separate array of the first metric by class.
Instead of relying on the index of the bridge, check whether the array
has an entry. Use that array for the output.
Add a test case that verifies that the help text is not duplicated.
Resolves: #642
[1] https://prometheus.io/docs/instrumenting/exposition_formats/#grouping-and-sorting
(cherry picked from commit d45c8e165f
)
This commit is contained in:
committed by
Asterisk Development Team
parent
2b69c96cf4
commit
07552cbf05
@@ -710,10 +710,23 @@ static void safe_bridge_destroy(struct ast_bridge *bridge)
|
||||
ast_bridge_destroy(bridge, 0);
|
||||
}
|
||||
|
||||
static int match_count(const char *str, const char *needle)
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
while ((str = strstr(str, needle))) {
|
||||
str += strlen(needle);
|
||||
count++;
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
AST_TEST_DEFINE(bridge_to_string)
|
||||
{
|
||||
RAII_VAR(struct ast_bridge *, bridge1, NULL, safe_bridge_destroy);
|
||||
RAII_VAR(struct ast_bridge *, bridge2, NULL, safe_bridge_destroy);
|
||||
RAII_VAR(struct ast_bridge *, bridge3, NULL, safe_bridge_destroy);
|
||||
struct ast_str *response;
|
||||
|
||||
switch (cmd) {
|
||||
@@ -736,6 +749,9 @@ AST_TEST_DEFINE(bridge_to_string)
|
||||
AST_BRIDGE_FLAG_INVISIBLE,
|
||||
"test_res_prometheus", "test_bridge_invisible", NULL);
|
||||
|
||||
bridge3 = ast_bridge_basic_new();
|
||||
ast_test_validate(test, bridge3 != NULL);
|
||||
|
||||
response = prometheus_scrape_to_string();
|
||||
if (!response) {
|
||||
return AST_TEST_FAIL;
|
||||
@@ -744,6 +760,7 @@ AST_TEST_DEFINE(bridge_to_string)
|
||||
ast_test_status_update(test, " -> Retrieved: %s\n", ast_str_buffer(response));
|
||||
ast_test_validate(test, strstr(ast_str_buffer(response), "(null)") == NULL);
|
||||
ast_test_validate(test, strstr(ast_str_buffer(response), "asterisk_bridges_channels_count{") != NULL);
|
||||
ast_test_validate(test, match_count(ast_str_buffer(response), "# HELP asterisk_bridges_channels_count Number of channels in the bridge.") == 1);
|
||||
ast_free(response);
|
||||
return AST_TEST_PASS;
|
||||
}
|
||||
|
Reference in New Issue
Block a user