@@ -24,6 +24,7 @@ test_suite_test_() ->
24
24
fun format_one /0 ,
25
25
fun format_with_many_labels /0 ,
26
26
fun format_ratio /0 ,
27
+ fun format_time_metrics /0 ,
27
28
fun format_selected_metrics /0 ,
28
29
fun text_format_selected_metrics /0 ,
29
30
fun invalid_fields /0 ]}.
@@ -92,7 +93,7 @@ counters_with_persistent_term_field_spec() ->
92
93
ok .
93
94
94
95
format_group () ->
95
- Group = widgets ,
96
+ Group = ? FUNCTION_NAME ,
96
97
Counters = [{reads , 1 , counter , " Total reads" }],
97
98
seshat :new_group (Group ),
98
99
seshat :new (Group , widget1 , Counters , #{component => widget1 }),
@@ -107,7 +108,7 @@ format_group() ->
107
108
ok .
108
109
109
110
format_one () ->
110
- Group = widgets ,
111
+ Group = ? FUNCTION_NAME ,
111
112
Counters = [{reads , 1 , counter , " Total reads" }],
112
113
seshat :new_group (Group ),
113
114
seshat :new (Group , widget1 , Counters , #{component => widget1 }),
@@ -120,7 +121,7 @@ format_one() ->
120
121
ok .
121
122
122
123
format_with_many_labels () ->
123
- Group = widgets ,
124
+ Group = ? FUNCTION_NAME ,
124
125
Counters = [{reads , 1 , counter , " Total reads" }],
125
126
seshat :new_group (Group ),
126
127
seshat :new (Group , widget1 , Counters , #{component => " widget1" , status => up }),
@@ -136,7 +137,7 @@ format_with_many_labels() ->
136
137
ok .
137
138
138
139
format_selected_metrics () ->
139
- Group = widgets ,
140
+ Group = ? FUNCTION_NAME ,
140
141
Counters = [
141
142
{reads , 1 , counter , " Total reads" },
142
143
{writes , 2 , counter , " Total writes" },
@@ -168,7 +169,7 @@ invalid_fields() ->
168
169
ok .
169
170
170
171
format_ratio () ->
171
- Group = widgets ,
172
+ Group = ? FUNCTION_NAME ,
172
173
Counters = [{pings , 1 , ratio , " Some ratio that happens to be 0%" },
173
174
{pongs , 2 , ratio , " Some ratio that happens to be 17%" },
174
175
{pangs , 3 , ratio , " Some ratio that happens to be 33%" },
@@ -197,27 +198,97 @@ format_ratio() ->
197
198
? assertEqual (ExpectedPrometheusFormat , PrometheusFormat ),
198
199
ok .
199
200
201
+ format_time_metrics () ->
202
+ Group = ? FUNCTION_NAME ,
203
+ Counters = [
204
+ {job_duration , 2 , time_s , " Job duration" },
205
+ {short_latency , 3 , time_ms , " Short latency" },
206
+ {long_latency , 1 , time_ms , " Request latency" }
207
+ ],
208
+ seshat :new_group (Group ),
209
+ Labels = #{component => test },
210
+ seshat :new (Group , test_component , Counters , Labels ),
211
+
212
+ % Set values (1500 ms, 30 s, 5 ms)
213
+ set_value (Group , test_component , job_duration , 30 ),
214
+ set_value (Group , test_component , short_latency , 5 ),
215
+ set_value (Group , test_component , long_latency , 1500 ),
216
+
217
+ MapFormat = seshat :format (Group ),
218
+ ExpectedMapFormat = #{
219
+ job_duration => #{type => gauge ,
220
+ help => " Job duration" ,
221
+ values => #{Labels => 30.0 }},
222
+ short_latency => #{type => gauge ,
223
+ help => " Short latency" ,
224
+ values => #{Labels => 0.005 }},
225
+ long_latency => #{type => gauge ,
226
+ help => " Request latency" ,
227
+ values => #{Labels => 1.5 }}
228
+ },
229
+ ? assertEqual (ExpectedMapFormat , MapFormat ),
230
+
231
+ Prefix = " myapp" ,
232
+ MetricNames = [job_duration , short_latency , long_latency ], % Added new metric name
233
+ ResultAsList = binary_to_list (seshat :text_format (Group , Prefix , MetricNames )),
234
+
235
+ % Expected format needs sorting because order isn't guaranteed
236
+ ExpectedLines = [
237
+ " # HELP myapp_job_duration_seconds Job duration" ,
238
+ " # TYPE myapp_job_duration_seconds gauge" ,
239
+ " myapp_job_duration_seconds{component=\" test\" } 30.0" ,
240
+ " # HELP myapp_short_latency_seconds Short latency" ,
241
+ " # TYPE myapp_short_latency_seconds gauge" ,
242
+ " myapp_short_latency_seconds{component=\" test\" } 0.005" ,
243
+ " # HELP myapp_long_latency_seconds Request latency" ,
244
+ " # TYPE myapp_long_latency_seconds gauge" ,
245
+ " myapp_long_latency_seconds{component=\" test\" } 1.5"
246
+ ],
247
+ ExpectedSortedText = lists :sort (ExpectedLines ),
248
+
249
+ % Split and sort the actual result for comparison
250
+ ResultLines = string :split (ResultAsList , " \n " , all ),
251
+ FilteredResultLines = [Line || Line <- ResultLines , Line /= " " ],
252
+ SortedResultText = lists :sort (FilteredResultLines ),
253
+
254
+ ? assertEqual (ExpectedSortedText , SortedResultText ),
255
+
256
+ ok .
257
+
200
258
text_format_selected_metrics () ->
201
259
Group = widgets ,
202
260
Counters = [
203
261
{reads , 1 , counter , " Total reads" },
204
262
{writes , 2 , counter , " Total writes" },
205
- {cached , 3 , ratio , " Ratio of things served from cache" }
206
- ],
263
+ {cached , 3 , ratio , " Ratio of things served from cache" },
264
+ {latency , 4 , time_ms , " Latency" },
265
+ {duration , 5 , time_s , " Duration" },
266
+ {npc , 6 , gauge , " A metric we don't request in a call to text_format/3" }
267
+ ],
207
268
seshat :new_group (Group ),
208
269
seshat :new (Group , thing1 , Counters , #{component => " thing1" , version => " 1.2.3" }),
209
270
seshat :new (Group , thing2 , Counters , #{component => " thing2" , some_atom => atom_value }),
210
271
seshat :new (Group , thing3 , Counters , #{component => " thing3" , some_binary => <<" binary_value" >>}),
211
272
set_value (Group , thing1 , reads , 1 ),
212
273
set_value (Group , thing1 , writes , 2 ),
213
274
set_value (Group , thing1 , cached , 10 ),
275
+ set_value (Group , thing1 , latency , 5 ),
276
+ set_value (Group , thing1 , duration , 123 ),
277
+ set_value (Group , thing1 , npc , 1 ), % to be ignored
214
278
set_value (Group , thing2 , reads , 3 ),
215
279
set_value (Group , thing2 , writes , 4 ),
216
280
set_value (Group , thing2 , cached , 100 ),
281
+ set_value (Group , thing2 , latency , 6 ),
282
+ set_value (Group , thing2 , duration , 234 ),
283
+ set_value (Group , thing2 , npc , 1 ), % to be ignored
217
284
set_value (Group , thing3 , reads , 1234 ),
218
285
set_value (Group , thing3 , writes , 4321 ),
219
286
set_value (Group , thing3 , cached , 17 ),
220
- PrometheusFormat = binary_to_list (seshat :text_format (Group , " acme" , [reads , writes , cached ])),
287
+ set_value (Group , thing3 , latency , 7 ),
288
+ set_value (Group , thing3 , duration , 345 ),
289
+ set_value (Group , thing3 , npc , 1 ), % to be ignored
290
+
291
+ ResultAsList = binary_to_list (seshat :text_format (Group , " acme" , [reads , writes , cached , latency , duration ])),
221
292
ExpectedPrometheusFormat = " # HELP acme_reads Total reads\n "
222
293
" # TYPE acme_reads counter\n "
223
294
" acme_reads{version=\" 1.2.3\" ,component=\" thing1\" } 1\n "
@@ -232,9 +303,19 @@ text_format_selected_metrics() ->
232
303
" # TYPE acme_cached_ratio gauge\n "
233
304
" acme_cached_ratio{version=\" 1.2.3\" ,component=\" thing1\" } 0.1\n "
234
305
" acme_cached_ratio{component=\" thing2\" ,some_atom=\" atom_value\" } 1.0\n "
235
- " acme_cached_ratio{component=\" thing3\" ,some_binary=\" binary_value\" } 0.17\n " ,
306
+ " acme_cached_ratio{component=\" thing3\" ,some_binary=\" binary_value\" } 0.17\n "
307
+ " # HELP acme_latency_seconds Latency\n "
308
+ " # TYPE acme_latency_seconds gauge\n "
309
+ " acme_latency_seconds{version=\" 1.2.3\" ,component=\" thing1\" } 0.005\n "
310
+ " acme_latency_seconds{component=\" thing2\" ,some_atom=\" atom_value\" } 0.006\n "
311
+ " acme_latency_seconds{component=\" thing3\" ,some_binary=\" binary_value\" } 0.007\n "
312
+ " # HELP acme_duration_seconds Duration\n "
313
+ " # TYPE acme_duration_seconds gauge\n "
314
+ " acme_duration_seconds{version=\" 1.2.3\" ,component=\" thing1\" } 123.0\n "
315
+ " acme_duration_seconds{component=\" thing2\" ,some_atom=\" atom_value\" } 234.0\n "
316
+ " acme_duration_seconds{component=\" thing3\" ,some_binary=\" binary_value\" } 345.0\n " ,
236
317
237
- ? assertEqual (ExpectedPrometheusFormat , PrometheusFormat ),
318
+ ? assertEqual (ExpectedPrometheusFormat , ResultAsList ),
238
319
ok .
239
320
240
321
% % test helpers
0 commit comments