_site/cover/pgec_telemetry_metrics.COVER.html

1 %% Copyright (c) 2023 Peter Morgan <peter.james.morgan@gmail.com>
2 %%
3 %% Licensed under the Apache License, Version 2.0 (the "License");
4 %% you may not use this file except in compliance with the License.
5 %% You may obtain a copy of the License at
6 %%
7 %% http://www.apache.org/licenses/LICENSE-2.0
8 %%
9 %% Unless required by applicable law or agreed to in writing, software
10 %% distributed under the License is distributed on an "AS IS" BASIS,
11 %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 %% See the License for the specific language governing permissions and
13 %% limitations under the License.
14
15
16 -module(pgec_telemetry_metrics).
17
18
19 -export([handle/4]).
20 -include_lib("kernel/include/logger.hrl").
21
22 handle([cowboy, request, start], _Measurements, _Metadata, _Config) ->
23
:-(
ok;
24
25 handle([cowboy, request, stop] = EventName,
26 Measurements,
27 #{req := Req},
28 _Config) ->
29
:-(
Prefix = lists:sublist(EventName, 2),
30
:-(
Label = maps:with([host, port], Req),
31
:-(
metrics:counter(
32 maps:fold(
33 fun
34 (duration = Metric, Delta, A) ->
35
:-(
[#{name => pgec_util:snake_case(Prefix ++ [Metric, ms]),
36 label => Label,
37 delta => erlang:convert_time_unit(
38 Delta,
39 native,
40 millisecond)} | A];
41
42 (Metric, Delta, A) ->
43
:-(
[#{name => pgec_util:snake_case(Prefix ++ [Metric]),
44 label => Label,
45 delta => Delta} | A]
46 end,
47 [#{name => pgec_util:snake_case(Prefix ++ [count]),
48 label => Label,
49 delta => 1}],
50 Measurements));
51
52 handle([cowboy, request, exception] = EventName,
53 Measurements,
54 #{req := Req},
55 _Config) ->
56
:-(
Prefix = lists:sublist(EventName, 2),
57
:-(
Label = maps:with([host, port], Req),
58
:-(
metrics:counter(
59 maps:fold(
60 fun
61 (duration = Metric, Delta, A) ->
62
:-(
[#{name => pgec_util:snake_case(Prefix ++ [error, Metric, ms]),
63 label => Label,
64 delta => erlang:convert_time_unit(
65 Delta,
66 native,
67 millisecond)} | A];
68
69 (Metric, Delta, A) ->
70
:-(
[#{name => pgec_util:snake_case(Prefix ++ [error, Metric]),
71 label => Label,
72 delta => Delta} | A]
73 end,
74 [#{name => pgec_util:snake_case(Prefix ++ [error, count]),
75 label => Label,
76 delta => 1}],
77 Measurements));
78
79 handle([pgec, System, _] = EventName,
80 #{count := N} = Measurements,
81 Metadata,
82 Config) when System == storage;
83 System == replica ->
84
:-(
?LOG_DEBUG(#{event_name => EventName,
85 measurements => Measurements,
86 metadata => Metadata,
87
:-(
config => Config}),
88
89
:-(
metrics:counter(
90 [#{name => pgec_util:snake_case(EventName ++ [count]),
91 label => Metadata,
92 delta => N}]);
93
94 handle([pgec, System, _] = EventName,
95 #{value := N} = Measurements,
96 Metadata,
97 Config) when System == storage;
98 System == replica ->
99
:-(
?LOG_DEBUG(#{event_name => EventName,
100 measurements => Measurements,
101 metadata => Metadata,
102
:-(
config => Config}),
103
104
:-(
metrics:gauge(
105 [#{name => pgec_util:snake_case(EventName),
106 label => Metadata,
107 value => N}]);
108
109 handle(EventName, Measurements, Metadata, Config) ->
110
:-(
?LOG_INFO(#{event_name => EventName,
111 measurements => Measurements,
112 metadata => Metadata,
113
:-(
config => Config}).
Line Hits Source