Skip to content

Commit

Permalink
use prometheus_histogram:declare/1 instead prometheus_histogram:new/1 (
Browse files Browse the repository at this point in the history
…#173)

When a RADIUS request comes to eradius or we try to send a request
from it - new histogram metric could be created. There could be
a situation when a couple of RADIUS requests with the same name
and similar labels come to eradius which may lead to crash because
of races during creation of prometheus metric.

This commit replaces prometheus_histogram:new/1 with
prometheus_histogram:declare/1 API as the first one throws an exception
in a case of metric already exists.
  • Loading branch information
0xAX committed Dec 17, 2020
1 parent 984b0cf commit 5926af5
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
locals_not_used, deprecated_function_calls,
deprecated_functions]}.

{xref_ignores, [{prometheus_histogram, new, 1},
{xref_ignores, [{prometheus_histogram, declare, 1},
{prometheus_histogram, observe, 3}]}.

%% == Cover ==
Expand Down
8 changes: 4 additions & 4 deletions src/eradius_counter.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ observe(Name, {{ClientName, ClientIP, _}, {ServerName, ServerIP, ServerPort}} =
prometheus_histogram:observe(Name, [ServerIP, ServerPort, ServerName, ClientName, ClientIP], Value)
catch _:_ ->
Buckets = application:get_env(eradius, histogram_buckets, [10, 30, 50, 75, 100, 1000, 2000]),
prometheus_histogram:new([{name, Name}, {labels, [server_ip, server_port, server_name, client_name, client_ip]},
{buckets, Buckets}, {help, Help}]),
prometheus_histogram:declare([{name, Name}, {labels, [server_ip, server_port, server_name, client_name, client_ip]},
{buckets, Buckets}, {help, Help}]),
observe(Name, MetricsInfo, Value, Help)
end;
_ ->
Expand All @@ -109,8 +109,8 @@ observe(Name, #nas_prop{server_ip = ServerIP, server_port = ServerPort, nas_ip =
prometheus_histogram:observe(Name, [inet:ntoa(ServerIP), ServerPort, ServerName, inet:ntoa(NasIP), NasId], Value)
catch _:_ ->
Buckets = application:get_env(eradius, histogram_buckets, [10, 30, 50, 75, 100, 1000, 2000]),
prometheus_histogram:new([{name, Name}, {labels, [server_ip, server_port, server_name, nas_ip, nas_id]},
{buckets, Buckets}, {help, Help}]),
prometheus_histogram:declare([{name, Name}, {labels, [server_ip, server_port, server_name, nas_ip, nas_id]},
{buckets, Buckets}, {help, Help}]),
observe(Name, Nas, Value, ServerName, Help)
end;
_ ->
Expand Down

0 comments on commit 5926af5

Please sign in to comment.