./ct_report/coverage/mongoose_instrument_exometer.COVER.html

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