1 |
|
-module(mongoose_instrument_exometer). |
2 |
|
|
3 |
|
-behaviour(mongoose_instrument). |
4 |
|
|
5 |
|
-export([set_up/3, handle_event/4]). |
6 |
|
|
7 |
|
-spec set_up(mongoose_instrument:event_name(), mongoose_instrument:labels(), |
8 |
|
mongoose_instrument:config()) -> boolean(). |
9 |
|
set_up(EventName, Labels, #{metrics := Metrics}) -> |
10 |
1518 |
maps:foreach(fun(MetricName, MetricType) -> |
11 |
2404 |
set_up_metric(EventName, Labels, MetricName, MetricType) |
12 |
|
end, Metrics), |
13 |
1518 |
true; |
14 |
|
set_up(_EventName, _Labels, #{}) -> |
15 |
:-( |
false. |
16 |
|
|
17 |
|
-spec handle_event(mongoose_instrument:event_name(), mongoose_instrument:labels(), |
18 |
|
mongoose_instrument:config(), mongoose_instrument:measurements()) -> ok. |
19 |
|
handle_event(EventName, Labels, #{metrics := Metrics}, Measurements) -> |
20 |
14925 |
maps:foreach(fun(MetricName, MetricType) -> |
21 |
32387 |
handle_metric_event(EventName, Labels, MetricName, MetricType, Measurements) |
22 |
|
end, Metrics). |
23 |
|
|
24 |
|
set_up_metric(EventName, Labels, MetricName, MetricType) -> |
25 |
|
%% TODO improve handling of already existing metrics |
26 |
2404 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
27 |
2404 |
catch exometer:new(Name, MetricType). |
28 |
|
|
29 |
|
handle_metric_event(EventName, Labels, MetricName, MetricType, Measurements) -> |
30 |
32387 |
case Measurements of |
31 |
|
#{MetricName := MetricValue} -> |
32 |
31410 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
33 |
31410 |
update_metric(Name, MetricType, MetricValue); |
34 |
|
#{} -> |
35 |
977 |
ok |
36 |
|
end. |
37 |
|
|
38 |
|
update_metric(Name, spiral, Value) when is_integer(Value), Value >= 0 -> |
39 |
15074 |
exometer:update(Name, Value); |
40 |
|
update_metric(Name, histogram, Value) when is_integer(Value) -> |
41 |
16336 |
exometer:update(Name, Value). |
42 |
|
|
43 |
|
%% This logic will need extending if we add more labels |
44 |
|
exometer_metric_name(EventName, #{host_type := HostType}, MetricName) -> |
45 |
33814 |
[mongoose_metrics:get_host_type_prefix(HostType), EventName, MetricName]. |