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 |
1486 |
maps:foreach(fun(MetricName, MetricType) -> |
11 |
2350 |
set_up_metric(EventName, Labels, MetricName, MetricType) |
12 |
|
end, Metrics), |
13 |
1486 |
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 |
14567 |
maps:foreach(fun(MetricName, MetricType) -> |
21 |
31362 |
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 |
2350 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
27 |
2350 |
catch exometer:new(Name, MetricType). |
28 |
|
|
29 |
|
handle_metric_event(EventName, Labels, MetricName, MetricType, Measurements) -> |
30 |
31362 |
case Measurements of |
31 |
|
#{MetricName := MetricValue} -> |
32 |
30377 |
Name = exometer_metric_name(EventName, Labels, MetricName), |
33 |
30377 |
update_metric(Name, MetricType, MetricValue); |
34 |
|
#{} -> |
35 |
985 |
ok |
36 |
|
end. |
37 |
|
|
38 |
|
update_metric(Name, spiral, Value) when is_integer(Value), Value >= 0 -> |
39 |
14716 |
exometer:update(Name, Value); |
40 |
|
update_metric(Name, histogram, Value) when is_integer(Value) -> |
41 |
15661 |
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 |
32727 |
[mongoose_metrics:get_host_type_prefix(HostType), EventName, MetricName]. |