Skip to content

Commit 02df2e1

Browse files
authored
Fix appending custom values to specs (#187)
Fixes a bug introduced at #181 when we introduced maps as specs, as histograms and summaries would append values as if they were still lists.
1 parent bf6fd38 commit 02df2e1

File tree

4 files changed

+46
-13
lines changed

4 files changed

+46
-13
lines changed

src/metrics/prometheus_histogram.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ validate_histogram_spec(Spec) ->
520520
validate_histogram_labels(Labels),
521521
RBuckets = prometheus_metric_spec:get_value(buckets, Spec, default),
522522
Buckets = prometheus_buckets:new(RBuckets),
523-
[{data, Buckets} | Spec].
523+
prometheus_metric_spec:add_value(data, Buckets, Spec).
524524

525525
validate_histogram_labels(Labels) ->
526526
[raise_error_if_le_label_found(Label) || Label <- Labels].

src/metrics/prometheus_quantile_summary.erl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,14 +457,12 @@ validate_summary_spec(Spec) ->
457457
validate_summary_labels(Labels),
458458
{Invariant, QNs} = invariant_and_quantiles_from_spec(Spec),
459459
CompressLimit = compress_limit_from_spec(Spec),
460-
[
461-
{data, #{
462-
quantiles => QNs,
463-
invariant => Invariant,
464-
compress_limit => CompressLimit
465-
}}
466-
| Spec
467-
].
460+
Data = #{
461+
quantiles => QNs,
462+
invariant => Invariant,
463+
compress_limit => CompressLimit
464+
},
465+
prometheus_metric_spec:add_value(data, Data, Spec).
468466

469467
validate_summary_labels(Labels) ->
470468
[raise_error_if_quantile_label_found(Label) || Label <- Labels].

test/eunit/metric/prometheus_histogram_tests.erl

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
prometheus_format_test_() ->
88
{foreach, fun prometheus_eunit_common:start/0, fun prometheus_eunit_common:stop/1, [
9-
fun test_registration/1,
9+
fun test_registration_as_list/1,
10+
fun test_registration_as_map/1,
1011
fun test_errors/1,
1112
fun test_buckets/1,
1213
fun test_observe/1,
@@ -23,7 +24,26 @@ prometheus_format_test_() ->
2324
fun test_collector3/1
2425
]}.
2526

26-
test_registration(_) ->
27+
test_registration_as_list(_) ->
28+
Name = request_duration,
29+
SpecWithRegistry = #{
30+
name => Name,
31+
buckets => [100, 300, 500, 750, 1000],
32+
help => "",
33+
registry => qwe
34+
},
35+
[
36+
?_assertEqual(
37+
true,
38+
prometheus_histogram:declare(SpecWithRegistry)
39+
),
40+
?_assertError(
41+
{mf_already_exists, {qwe, Name}, "Consider using declare instead."},
42+
prometheus_histogram:new(SpecWithRegistry)
43+
)
44+
].
45+
46+
test_registration_as_map(_) ->
2747
Name = request_duration,
2848
SpecWithRegistry = [
2949
{name, Name},

test/eunit/metric/prometheus_quantile_summary_tests.erl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
prometheus_format_test_() ->
1010
{foreach, fun prometheus_eunit_common:start/0, fun prometheus_eunit_common:stop/1, [
11-
fun test_registration/1,
11+
fun test_registration_as_list/1,
12+
fun test_registration_as_map/1,
1213
fun test_errors/1,
1314
fun test_observe/1,
1415
fun test_observe_quantiles/1,
@@ -36,7 +37,7 @@ test_merge_logic_when_fetching_value(_) ->
3637
collect_monitors(Monitors),
3738
[?_assertMatch({_, _, _}, prometheus_quantile_summary:value(Name))].
3839

39-
test_registration(_) ->
40+
test_registration_as_list(_) ->
4041
Name = orders_summary,
4142
SpecWithRegistry = [{name, Name}, {help, ""}, {registry, qwe}],
4243
[
@@ -50,6 +51,20 @@ test_registration(_) ->
5051
)
5152
].
5253

54+
test_registration_as_map(_) ->
55+
Name = orders_summary,
56+
SpecWithRegistry = #{name => Name, help => "", registry => qwe},
57+
[
58+
?_assertEqual(
59+
true,
60+
prometheus_quantile_summary:declare(SpecWithRegistry)
61+
),
62+
?_assertError(
63+
{mf_already_exists, {qwe, Name}, "Consider using declare instead."},
64+
prometheus_quantile_summary:new(SpecWithRegistry)
65+
)
66+
].
67+
5368
test_errors(_) ->
5469
prometheus_quantile_summary:new([{name, db_query_duration}, {labels, [repo]}, {help, ""}]),
5570
%% basic name/labels/help validations test

0 commit comments

Comments
 (0)