@@ -34,7 +34,8 @@ shutdown() ->
3434
3535get_metrics () ->
3636 case ets :info (? TABLE ) of
37- undefined -> <<>>;
37+ undefined ->
38+ <<>>;
3839 _ ->
3940 case ets :lookup (? TABLE , metrics_output ) of
4041 [{metrics_output , Output }] -> Output ;
@@ -62,16 +63,20 @@ accumulate_data(PromName, #sum{datapoints = Datapoints, is_monotonic = false}) -
6263accumulate_data (PromName , # gauge {datapoints = Datapoints }) ->
6364 lists :foreach (fun (DP ) -> store_gauge (PromName , DP ) end , Datapoints ).
6465
65- accumulate_histogram (PromName , # histogram_datapoint {attributes = Attrs ,
66- count = Count ,
67- sum = Sum ,
68- bucket_counts = BucketCounts ,
69- explicit_bounds = Bounds }) ->
66+ accumulate_histogram (PromName , # histogram_datapoint {
67+ attributes = Attrs ,
68+ count = Count ,
69+ sum = Sum ,
70+ bucket_counts = BucketCounts ,
71+ explicit_bounds = Bounds
72+ }) ->
7073 Key = {dp , PromName , normalize_attrs (Attrs )},
7174 case ets :lookup (? TABLE , Key ) of
7275 [{Key , {hist , PrevCount , PrevSum , PrevBuckets , B }}] ->
7376 NewBuckets = add_lists (PrevBuckets , BucketCounts ),
74- ets :insert (? TABLE , {Key , {hist , PrevCount + Count , add_num (PrevSum , Sum ), NewBuckets , B }});
77+ ets :insert (
78+ ? TABLE , {Key , {hist , PrevCount + Count , add_num (PrevSum , Sum ), NewBuckets , B }}
79+ );
7580 [] ->
7681 ets :insert (? TABLE , {Key , {hist , Count , Sum , BucketCounts , Bounds }})
7782 end .
@@ -108,21 +113,35 @@ metric_meta(PromName) -> {meta, PromName}.
108113
109114render_all () ->
110115 Metas = ets :match (? TABLE , {{meta , '$1' }, '$2' , '$3' , '_' , '_' }),
111- Lines = lists :flatmap (fun ([PromName , Desc , Type ]) ->
112- render_metric (PromName , Desc , Type )
113- end , lists :sort (Metas )),
116+ Lines = lists :flatmap (
117+ fun ([PromName , Desc , Type ]) ->
118+ render_metric (PromName , Desc , Type )
119+ end ,
120+ lists :sort (Metas )
121+ ),
114122 iolist_to_binary (Lines ).
115123
116124render_metric (PromName , Desc , Type ) ->
117125 Header = [
118- <<" # HELP " >>, PromName , <<" " >>, to_bin (Desc ), <<" \n " >>,
119- <<" # TYPE " >>, PromName , <<" " >>, Type , <<" \n " >>
126+ <<" # HELP " >>,
127+ PromName ,
128+ <<" " >>,
129+ to_bin (Desc ),
130+ <<" \n " >>,
131+ <<" # TYPE " >>,
132+ PromName ,
133+ <<" " >>,
134+ Type ,
135+ <<" \n " >>
120136 ],
121137 DPs = ets :match (? TABLE , {{dp , PromName , '$1' }, '$2' }),
122- Body = lists :flatmap (fun ([AttrList , Data ]) ->
123- Attrs = maps :from_list (AttrList ),
124- render_datapoint (PromName , Attrs , Data )
125- end , lists :sort (DPs )),
138+ Body = lists :flatmap (
139+ fun ([AttrList , Data ]) ->
140+ Attrs = maps :from_list (AttrList ),
141+ render_datapoint (PromName , Attrs , Data )
142+ end ,
143+ lists :sort (DPs )
144+ ),
126145 case Body of
127146 [] -> Header ;
128147 _ -> Header ++ Body
@@ -147,11 +166,20 @@ format_buckets(Name, Labels, BucketCounts, Bounds) ->
147166format_buckets (Name , Labels , [Count | RestCounts ], [Bound | RestBounds ], Cumulative , Acc ) ->
148167 CumCount = Cumulative + Count ,
149168 Le = format_number (Bound ),
150- Line = [Name , <<" _bucket" >>, insert_le_label (Labels , Le ), <<" " >>, format_number (CumCount ), <<" \n " >>],
169+ Line = [
170+ Name , <<" _bucket" >>, insert_le_label (Labels , Le ), <<" " >>, format_number (CumCount ), <<" \n " >>
171+ ],
151172 format_buckets (Name , Labels , RestCounts , RestBounds , CumCount , Acc ++ [Line ]);
152173format_buckets (Name , Labels , [Count | _ ], [], Cumulative , Acc ) ->
153174 CumCount = Cumulative + Count ,
154- Line = [Name , <<" _bucket" >>, insert_le_label (Labels , <<" +Inf" >>), <<" " >>, format_number (CumCount ), <<" \n " >>],
175+ Line = [
176+ Name ,
177+ <<" _bucket" >>,
178+ insert_le_label (Labels , <<" +Inf" >>),
179+ <<" " >>,
180+ format_number (CumCount ),
181+ <<" \n " >>
182+ ],
155183 Acc ++ [Line ];
156184format_buckets (_Name , _Labels , [], _ , _Cumulative , Acc ) ->
157185 Acc .
@@ -163,12 +191,16 @@ format_buckets(_Name, _Labels, [], _, _Cumulative, Acc) ->
163191format_labels (Attrs ) when map_size (Attrs ) =:= 0 ->
164192 <<>>;
165193format_labels (Attrs ) ->
166- Pairs = lists :sort (maps :fold (fun (K , V , Acc ) ->
167- [{prom_label_name (K ), prom_label_value (V )} | Acc ]
168- end , [], Attrs )),
169- Inner = lists :join (<<" ," >>, [
170- [LK , <<" =\" " >>, LV , <<" \" " >>] || {LK , LV } <- Pairs
171- ]),
194+ Pairs = lists :sort (
195+ maps :fold (
196+ fun (K , V , Acc ) ->
197+ [{prom_label_name (K ), prom_label_value (V )} | Acc ]
198+ end ,
199+ [],
200+ Attrs
201+ )
202+ ),
203+ Inner = lists :join (<<" ," >>, [[LK , <<" =\" " >>, LV , <<" \" " >>] || {LK , LV } <- Pairs ]),
172204 [<<" {" >>, Inner , <<" }" >>].
173205
174206insert_le_label (<<>>, Le ) ->
@@ -187,8 +219,10 @@ prom_metric_name(Name, Unit, Data) ->
187219 WithUnit = maybe_append_unit (Base , Unit ),
188220 maybe_append_total (WithUnit , Data ).
189221
190- maybe_append_unit (Base , undefined ) -> Base ;
191- maybe_append_unit (Base , '' ) -> Base ;
222+ maybe_append_unit (Base , undefined ) ->
223+ Base ;
224+ maybe_append_unit (Base , '' ) ->
225+ Base ;
192226maybe_append_unit (Base , s ) ->
193227 append_if_missing (Base , <<" _seconds" >>);
194228maybe_append_unit (Base , 'By' ) ->
@@ -242,8 +276,14 @@ escape_label_value(Bin) ->
242276 binary :replace (
243277 binary :replace (
244278 binary :replace (Bin , <<" \\ " >>, <<" \\\\ " >>, [global ]),
245- <<" \" " >>, <<" \\\" " >>, [global ]),
246- <<" \n " >>, <<" \\ n" >>, [global ]).
279+ <<" \" " >>,
280+ <<" \\\" " >>,
281+ [global ]
282+ ),
283+ <<" \n " >>,
284+ <<" \\ n" >>,
285+ [global ]
286+ ).
247287
248288dots_to_underscores (Bin ) ->
249289 binary :replace (Bin , <<" ." >>, <<" _" >>, [global ]).
0 commit comments