1 |
|
%%============================================================================== |
2 |
|
%% Copyright 2014 Erlang Solutions Ltd. |
3 |
|
%% |
4 |
|
%% Licensed under the Apache License, Version 2.0 (the "License"); |
5 |
|
%% you may not use this file except in compliance with the License. |
6 |
|
%% You may obtain a copy of the License at |
7 |
|
%% |
8 |
|
%% http://www.apache.org/licenses/LICENSE-2.0 |
9 |
|
%% |
10 |
|
%% Unless required by applicable law or agreed to in writing, software |
11 |
|
%% distributed under the License is distributed on an "AS IS" BASIS, |
12 |
|
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 |
|
%% See the License for the specific language governing permissions and |
14 |
|
%% limitations under the License. |
15 |
|
%%============================================================================== |
16 |
|
-module(mongoose_api_metrics). |
17 |
|
|
18 |
|
-include("mongoose.hrl"). |
19 |
|
|
20 |
|
%% mongoose_api callbacks |
21 |
|
-export([prefix/0, |
22 |
|
routes/0, |
23 |
|
handle_options/2, |
24 |
|
handle_get/2]). |
25 |
|
|
26 |
|
%% internal exports |
27 |
|
-export([available_metrics/1, |
28 |
|
sum_metrics/1, |
29 |
|
sum_metric/1, |
30 |
|
host_type_metric/1, |
31 |
|
host_type_metrics/1, |
32 |
|
global_metric/1, |
33 |
|
global_metrics/1 |
34 |
|
]). |
35 |
|
|
36 |
|
-ignore_xref([available_metrics/1, global_metric/1, global_metrics/1, handle_get/2, |
37 |
|
handle_options/2, host_type_metric/1, host_type_metrics/1, prefix/0, |
38 |
|
routes/0, sum_metric/1, sum_metrics/1]). |
39 |
|
|
40 |
|
%%-------------------------------------------------------------------- |
41 |
|
%% mongoose_api callbacks |
42 |
|
%%-------------------------------------------------------------------- |
43 |
|
-spec prefix() -> mongoose_api:prefix(). |
44 |
|
prefix() -> |
45 |
1148 |
"/metrics". |
46 |
|
|
47 |
|
-spec routes() -> mongoose_api:routes(). |
48 |
|
routes() -> |
49 |
164 |
[{"/", [available_metrics]}, |
50 |
|
{"/all", [sum_metrics]}, |
51 |
|
{"/all/:metric", [sum_metric]}, |
52 |
|
{"/global", [global_metrics]}, |
53 |
|
{"/global/:metric", [global_metric]}, |
54 |
|
{"/host_type/:host_type/:metric", [host_type_metric]}, |
55 |
|
{"/host_type/:host_type", [host_type_metrics]}]. |
56 |
|
|
57 |
|
-spec handle_options(mongoose_api:bindings(), mongoose_api:options()) -> |
58 |
|
mongoose_api:methods(). |
59 |
|
handle_options(_Bindings, [_Command]) -> |
60 |
523 |
[get]. |
61 |
|
|
62 |
|
-spec handle_get(mongoose_api:bindings(), mongoose_api:options()) -> |
63 |
|
mongoose_api:response(). |
64 |
|
handle_get(Bindings, [Command]) -> |
65 |
521 |
?MODULE:Command(Bindings). |
66 |
|
|
67 |
|
%%-------------------------------------------------------------------- |
68 |
|
%% mongoose_api commands actual handlers |
69 |
|
%%-------------------------------------------------------------------- |
70 |
|
available_metrics(_Bindings) -> |
71 |
2 |
{HostTypes, Metrics} = get_available_host_type_metrics(), |
72 |
2 |
Global = get_available_global_metrics(), |
73 |
2 |
Reply = [{host_types, HostTypes}, {metrics, Metrics}, {global, Global}], |
74 |
2 |
{ok, Reply}. |
75 |
|
|
76 |
|
sum_metrics(_Bindings) -> |
77 |
83 |
Metrics = {metrics, get_sum_metrics()}, |
78 |
83 |
{ok, Metrics}. |
79 |
|
|
80 |
|
sum_metric(Bindings) -> |
81 |
84 |
{metric, Metric} = lists:keyfind(metric, 1, Bindings), |
82 |
84 |
try |
83 |
84 |
case get_sum_metric(binary_to_existing_atom(Metric, utf8)) of |
84 |
|
[] -> |
85 |
:-( |
{error, not_found}; |
86 |
|
Value -> |
87 |
83 |
{ok, {metric, Value}} |
88 |
|
end |
89 |
|
catch error:badarg -> |
90 |
1 |
{error, not_found} |
91 |
|
end. |
92 |
|
|
93 |
|
host_type_metric(Bindings) -> |
94 |
84 |
{host_type, HostType} = lists:keyfind(host_type, 1, Bindings), |
95 |
84 |
{metric, Metric} = lists:keyfind(metric, 1, Bindings), |
96 |
84 |
try |
97 |
84 |
MetricAtom = binary_to_existing_atom(Metric, utf8), |
98 |
83 |
{ok, Value} = mongoose_metrics:get_metric_value([HostType, MetricAtom]), |
99 |
83 |
{ok, {metric, Value}} |
100 |
|
catch error:badarg -> |
101 |
1 |
{error, not_found} |
102 |
|
end. |
103 |
|
|
104 |
|
host_type_metrics(Bindings) -> |
105 |
84 |
{host_type, HostType} = lists:keyfind(host_type, 1, Bindings), |
106 |
84 |
case get_host_type_metrics(HostType) of |
107 |
|
[] -> |
108 |
1 |
{error, not_found}; |
109 |
|
Metrics -> |
110 |
83 |
{ok, {metrics, Metrics}} |
111 |
|
end. |
112 |
|
|
113 |
|
global_metric(Bindings) -> |
114 |
92 |
{metric, Metric} = lists:keyfind(metric, 1, Bindings), |
115 |
92 |
MetricAtom = binary_to_existing_atom(Metric, utf8), |
116 |
92 |
case mongoose_metrics:get_metric_value(global, MetricAtom) of |
117 |
|
{ok, Value} -> |
118 |
92 |
{ok, {metric, Value}}; |
119 |
|
_Other -> |
120 |
:-( |
{error, not_found} |
121 |
|
end. |
122 |
|
|
123 |
|
global_metrics(_Bindings) -> |
124 |
92 |
case get_host_type_metrics(global) of |
125 |
|
[] -> |
126 |
:-( |
{error, not_found}; |
127 |
|
Metrics -> |
128 |
92 |
{ok, {metrics, Metrics}} |
129 |
|
end. |
130 |
|
|
131 |
|
|
132 |
|
%%-------------------------------------------------------------------- |
133 |
|
%% internal functions |
134 |
|
%%-------------------------------------------------------------------- |
135 |
|
-spec get_available_host_types() -> [mongooseim:host_type()]. |
136 |
|
get_available_host_types() -> |
137 |
85 |
?ALL_HOST_TYPES. |
138 |
|
|
139 |
|
-spec get_available_metrics(HostType :: mongooseim:host_type()) -> [any()]. |
140 |
|
get_available_metrics(HostType) -> |
141 |
85 |
mongoose_metrics:get_host_type_metric_names(HostType). |
142 |
|
|
143 |
|
-spec get_available_host_type_metrics() -> {[any(), ...], [any()]}. |
144 |
|
get_available_host_type_metrics() -> |
145 |
85 |
HostTypes = get_available_host_types(), |
146 |
85 |
Metrics = [Metric || [Metric] <- get_available_metrics(hd(HostTypes))], |
147 |
85 |
{HostTypes, Metrics}. |
148 |
|
|
149 |
|
get_available_global_metrics() -> |
150 |
2 |
[Metric || [Metric] <- mongoose_metrics:get_global_metric_names()]. |
151 |
|
|
152 |
|
-spec get_sum_metrics() -> [{_, _}]. |
153 |
|
get_sum_metrics() -> |
154 |
83 |
{_HostTypes, Metrics} = get_available_host_type_metrics(), |
155 |
83 |
[{Metric, get_sum_metric(Metric)} || Metric <- Metrics]. |
156 |
|
|
157 |
|
-spec get_sum_metric(atom()) -> [{_, _}]. |
158 |
|
get_sum_metric(Metric) -> |
159 |
5312 |
mongoose_metrics:get_aggregated_values(Metric). |
160 |
|
|
161 |
|
-spec get_host_type_metrics(undefined | global | mongooseim:host_type()) -> [{_, _}]. |
162 |
|
get_host_type_metrics(HostType) -> |
163 |
176 |
Metrics = mongoose_metrics:get_metric_values(HostType), |
164 |
176 |
[{prep_name(NameParts), Value} || {[_HostType | NameParts], Value} <- Metrics]. |
165 |
|
|
166 |
|
prep_name(NameParts) -> |
167 |
21414 |
ToStrings = [part_to_string(NamePart) || NamePart <- NameParts], |
168 |
21414 |
string:join(ToStrings, "."). |
169 |
|
|
170 |
34693 |
part_to_string(Part) when is_atom(Part) -> atom_to_list(Part); |
171 |
82 |
part_to_string(Part) when is_binary(Part) -> binary_to_list(Part); |
172 |
:-( |
part_to_string(Part) -> Part. |