@@ -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,88 @@ format_ratio() ->
197
198
? assertEqual (ExpectedPrometheusFormat , PrometheusFormat ),
198
199
ok .
199
200
201
+ format_time_metrics () ->
202
+ Group = ? FUNCTION_NAME ,
203
+ Counters = [
204
+ {request_latency , 1 , time_ms , " Request latency" },
205
+ {job_duration , 2 , time_s , " Job duration" },
206
+ {short_latency , 3 , time_ms , " Short 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 , request_latency , 1500 ),
214
+ set_value (Group , test_component , job_duration , 30 ),
215
+ set_value (Group , test_component , short_latency , 5 ),
216
+
217
+ MapFormat = seshat :format (Group ),
218
+ ExpectedMapFormat = #{
219
+ request_latency => #{type => gauge ,
220
+ help => " Request latency" ,
221
+ values => #{Labels => 1.5 }}, % value in seconds
222
+ job_duration => #{type => gauge ,
223
+ help => " Job duration" ,
224
+ values => #{Labels => 30.0 }}, % value in seconds
225
+ short_latency => #{type => gauge ,
226
+ help => " Short latency" ,
227
+ values => #{Labels => 0.005 }} % value in seconds
228
+ },
229
+ ? assertEqual (ExpectedMapFormat , MapFormat ),
230
+
231
+ Prefix = " myapp" ,
232
+ MetricNames = [request_latency , job_duration , short_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_request_latency_seconds Request latency" ,
238
+ " # TYPE myapp_request_latency_seconds gauge" ,
239
+ " myapp_request_latency_seconds{component=\" test\" } 1.5" ,
240
+ " # HELP myapp_job_duration_seconds Job duration" ,
241
+ " # TYPE myapp_job_duration_seconds gauge" ,
242
+ " myapp_job_duration_seconds{component=\" test\" } 30.0" ,
243
+ " # HELP myapp_short_latency_seconds Short latency" ,
244
+ " # TYPE myapp_short_latency_seconds gauge" ,
245
+ " myapp_short_latency_seconds{component=\" test\" } 0.005"
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
+ ],
207
266
seshat :new_group (Group ),
208
267
seshat :new (Group , thing1 , Counters , #{component => " thing1" , version => " 1.2.3" }),
209
268
seshat :new (Group , thing2 , Counters , #{component => " thing2" , some_atom => atom_value }),
210
269
seshat :new (Group , thing3 , Counters , #{component => " thing3" , some_binary => <<" binary_value" >>}),
211
270
set_value (Group , thing1 , reads , 1 ),
212
271
set_value (Group , thing1 , writes , 2 ),
213
272
set_value (Group , thing1 , cached , 10 ),
273
+ set_value (Group , thing1 , latency , 5 ),
214
274
set_value (Group , thing2 , reads , 3 ),
215
275
set_value (Group , thing2 , writes , 4 ),
216
276
set_value (Group , thing2 , cached , 100 ),
277
+ set_value (Group , thing2 , latency , 6 ),
217
278
set_value (Group , thing3 , reads , 1234 ),
218
279
set_value (Group , thing3 , writes , 4321 ),
219
280
set_value (Group , thing3 , cached , 17 ),
220
- PrometheusFormat = binary_to_list (seshat :text_format (Group , " acme" , [reads , writes , cached ])),
281
+ set_value (Group , thing3 , latency , 7 ),
282
+ ResultAsList = binary_to_list (seshat :text_format (Group , " acme" , [reads , writes , cached ,latency ])),
221
283
ExpectedPrometheusFormat = " # HELP acme_reads Total reads\n "
222
284
" # TYPE acme_reads counter\n "
223
285
" acme_reads{version=\" 1.2.3\" ,component=\" thing1\" } 1\n "
@@ -232,9 +294,14 @@ text_format_selected_metrics() ->
232
294
" # TYPE acme_cached_ratio gauge\n "
233
295
" acme_cached_ratio{version=\" 1.2.3\" ,component=\" thing1\" } 0.1\n "
234
296
" 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 " ,
297
+ " acme_cached_ratio{component=\" thing3\" ,some_binary=\" binary_value\" } 0.17\n "
298
+ " # HELP acme_latency_seconds Latency\n "
299
+ " # TYPE acme_latency_seconds gauge\n "
300
+ " acme_latency_seconds{version=\" 1.2.3\" ,component=\" thing1\" } 0.005\n "
301
+ " acme_latency_seconds{component=\" thing2\" ,some_atom=\" atom_value\" } 0.006\n "
302
+ " acme_latency_seconds{component=\" thing3\" ,some_binary=\" binary_value\" } 0.007\n " ,
236
303
237
- ? assertEqual (ExpectedPrometheusFormat , PrometheusFormat ),
304
+ ? assertEqual (ExpectedPrometheusFormat , ResultAsList ),
238
305
ok .
239
306
240
307
% % test helpers
0 commit comments