-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrometheusMetricName.test.ts
87 lines (73 loc) · 2.38 KB
/
PrometheusMetricName.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { assertEquals, assertThrows } from '@std/assert';
import { PrometheusMetricName } from './PrometheusMetricName.ts';
Deno.test('PrometheusMetricName - validates standard Prometheus metric names', () =>
{
const validInputs = [
// Standard metrics
'http_requests_total',
'prometheus_http_requests_total',
'process_cpu_seconds_total',
// With colons (common in some exporters)
'node:http_requests_total:sum_rate5m',
'namespace:container_memory_usage_bytes:sum',
':leading_colon',
'trailing_colon:',
'multiple::colons',
// With numbers (but not leading)
'http_2xx_requests_total',
'tcp_port_8080_connections',
// With underscores
'_leading_underscore',
'trailing_underscore_',
'multiple__underscores',
// Single character
'a',
'_',
':',
];
for (const input of validInputs)
{
const result = PrometheusMetricName.try(input);
assertEquals(result, input);
}
});
Deno.test('PrometheusMetricName - rejects invalid metric names', () =>
{
const invalidInputs = [
// Invalid starts
'1_metric', // starts with number
'123abc', // starts with number
// Invalid characters
'http-requests-total', // hyphens not allowed
'http.requests.total', // dots not allowed
'http requests total', // spaces not allowed
'http$requests', // special characters
'metric/sec', // slashes
'metric@host', // at symbol
// Empty
'',
// Unicode/emoji
'http_requests_😀',
'métric_name',
];
for (const input of invalidInputs)
{
const result = PrometheusMetricName.try(input);
assertEquals(result, undefined);
}
});
Deno.test('PrometheusMetricName - assert throws with descriptive message', () =>
{
assertThrows(
() => PrometheusMetricName.assert('invalid-metric-name'),
Error,
'Invalid string did not pass validation: Supplied value "invalid-metric-name" is not valid for validator "PrometheusMetricName" (must start with a letter, underscore, or colon, followed by letters, digits, underscores, or colons).',
);
});
Deno.test('PrometheusMetricName - type safety', () =>
{
// @ts-expect-error Type 'string' is not assignable to type 'PrometheusMetricName'
const _invalid: PrometheusMetricName = 'invalid-metric-name';
const valid: PrometheusMetricName = PrometheusMetricName.assert('valid_metric_name');
assertEquals(valid, 'valid_metric_name');
});