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 |
1462 |
maps:foreach(fun(MetricName, MetricType) -> |
11 |
2309 |
set_up_metric(EventName, Labels, MetricName, MetricType) |
12 |
|
end, Metrics), |
13 |
1462 |
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 |
15061 |
maps:foreach(fun(MetricName, MetricType) -> |
21 |
32827 |
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 |
2309 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
27 |
2309 |
catch exometer:new(Name, MetricType). |
28 |
|
|
29 |
|
handle_metric_event(EventName, Labels, MetricName, MetricType, Measurements) -> |
30 |
32827 |
case Measurements of |
31 |
|
#{MetricName := MetricValue} -> |
32 |
31850 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
33 |
31850 |
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 |
15210 |
exometer:update(Name, Value); |
40 |
|
update_metric(Name, histogram, Value) when is_integer(Value) -> |
41 |
16640 |
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 |
34159 |
[mongoose_metrics:get_host_type_prefix(HostType), EventName, MetricName]. |