-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-metrics-server.ts
More file actions
executable file
·230 lines (191 loc) · 6.86 KB
/
test-metrics-server.ts
File metadata and controls
executable file
·230 lines (191 loc) · 6.86 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env bun
/**
* Metrics Server Integration Test
* Tests Prometheus metrics endpoint
*/
import { createDefaultMetricsServer } from './src/monitoring/metrics-server';
import { metrics, recordCommandExecuted, recordScriptExecuted } from './src/monitoring/metrics';
console.log('🧪 Metrics Server Integration Test');
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\\n');
// Test 1: Start Server
console.log('📝 Test 1: Start Metrics Server');
const server = createDefaultMetricsServer();
await server.start();
console.log('✅ Server started successfully\n');
// Wait a moment for server to be ready
await new Promise(resolve => setTimeout(resolve, 100));
// Test 2: Health Check Endpoint
console.log('📝 Test 2: Health Check Endpoint');
try {
const response = await fetch('http://127.0.0.1:9090/health');
if (!response.ok) {
throw new Error(`Health check failed: ${response.status}`);
}
const data = await response.json();
if (data.status !== 'healthy') {
throw new Error('Health status not healthy');
}
console.log('✅ Health check endpoint works');
console.log(` Status: ${data.status}`);
} catch (error) {
console.error('❌ Health check failed:', error);
await server.stop();
process.exit(1);
}
console.log();
// Test 3: Metrics Endpoint - Empty
console.log('📝 Test 3: Metrics Endpoint - Empty State');
try {
metrics.reset();
const response = await fetch('http://127.0.0.1:9090/metrics');
if (!response.ok) {
throw new Error(`Metrics fetch failed: ${response.status}`);
}
const contentType = response.headers.get('Content-Type');
if (!contentType?.includes('text/plain')) {
throw new Error(`Wrong content type: ${contentType}`);
}
const body = await response.text();
// Should have uptime metric
if (!body.includes('bot_uptime_seconds')) {
throw new Error('Missing uptime metric');
}
console.log('✅ Metrics endpoint returns Prometheus format');
console.log(` Content-Type: ${contentType}`);
} catch (error) {
console.error('❌ Metrics endpoint test failed:', error);
await server.stop();
process.exit(1);
}
console.log();
// Test 4: Metrics Endpoint - With Data
console.log('📝 Test 4: Metrics Endpoint - With Data');
try {
metrics.reset();
// Record some metrics
recordCommandExecuted('deploy', true);
recordCommandExecuted('deploy', false);
recordCommandExecuted('status', true);
recordScriptExecuted('backup', true, 2500);
recordScriptExecuted('backup', true, 2800);
recordScriptExecuted('backup', false, 5000);
const response = await fetch('http://127.0.0.1:9090/metrics');
const body = await response.text();
// Check for command metrics
if (!body.includes('bot_commands_total')) {
throw new Error('Missing commands counter');
}
if (!body.includes('command="deploy"')) {
throw new Error('Missing command labels');
}
if (!body.includes('success="true"')) {
throw new Error('Missing success label');
}
// Check for script metrics
if (!body.includes('bot_script_executions_total')) {
throw new Error('Missing script executions counter');
}
if (!body.includes('bot_script_duration_ms')) {
throw new Error('Missing script duration histogram');
}
if (!body.includes('script="backup"')) {
throw new Error('Missing script label');
}
// Check histogram format
if (!body.includes('bot_script_duration_ms_count')) {
throw new Error('Missing histogram count');
}
if (!body.includes('bot_script_duration_ms_sum')) {
throw new Error('Missing histogram sum');
}
if (!body.includes('quantile="0.5"')) {
throw new Error('Missing histogram quantiles');
}
console.log('✅ Metrics endpoint exposes recorded data');
console.log(' ✓ Command counters');
console.log(' ✓ Script execution counters');
console.log(' ✓ Script duration histogram');
console.log(' ✓ Labels and quantiles');
} catch (error) {
console.error('❌ Metrics with data test failed:', error);
await server.stop();
process.exit(1);
}
console.log();
// Test 5: Prometheus Format Validation
console.log('📝 Test 5: Prometheus Format Validation');
try {
const response = await fetch('http://127.0.0.1:9090/metrics');
const body = await response.text();
// Check format: # HELP, # TYPE, metric lines
const lines = body.split('\n').filter(l => l.trim());
let hasHelp = false;
let hasType = false;
let hasMetric = false;
for (const line of lines) {
if (line.startsWith('# HELP')) hasHelp = true;
if (line.startsWith('# TYPE')) hasType = true;
if (/^[a-z_]+(\{[^}]+\})?\s+\d+/.test(line)) hasMetric = true;
}
if (!hasHelp) throw new Error('Missing # HELP lines');
if (!hasType) throw new Error('Missing # TYPE lines');
if (!hasMetric) throw new Error('Missing metric value lines');
console.log('✅ Prometheus format valid');
console.log(` Total lines: ${lines.length}`);
console.log(` Metric groups: ${lines.filter(l => l.startsWith('# HELP')).length}`);
} catch (error) {
console.error('❌ Format validation failed:', error);
await server.stop();
process.exit(1);
}
console.log();
// Test 6: 404 for Unknown Paths
console.log('📝 Test 6: 404 for Unknown Paths');
try {
const response = await fetch('http://127.0.0.1:9090/unknown');
if (response.status !== 404) {
throw new Error(`Expected 404, got ${response.status}`);
}
console.log('✅ Unknown paths return 404');
} catch (error) {
console.error('❌ 404 test failed:', error);
await server.stop();
process.exit(1);
}
console.log();
// Test 7: Sample Prometheus Scrape
console.log('📝 Test 7: Sample Prometheus Scrape Output');
try {
const response = await fetch('http://127.0.0.1:9090/metrics');
const body = await response.text();
console.log('✅ Sample output (first 20 lines):');
const lines = body.split('\n').slice(0, 20);
for (const line of lines) {
if (line.trim()) {
console.log(` ${line}`);
}
}
} catch (error) {
console.error('❌ Sample output failed:', error);
}
console.log();
// Cleanup
console.log('📝 Cleanup: Stopping Server');
await server.stop();
console.log('✅ Server stopped\n');
// Summary
console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━');
console.log('✅ All metrics server tests passed!\\n');
console.log('📋 Summary:');
console.log(' - Metrics server starts and stops cleanly');
console.log(' - Health check endpoint works');
console.log(' - Metrics endpoint exposes Prometheus format');
console.log(' - Recorded metrics appear correctly');
console.log(' - Format validation passes');
console.log('');
console.log('💡 To run standalone metrics server:');
console.log(' bun src/monitoring/metrics-server.ts');
console.log('');
console.log('💡 To scrape metrics:');
console.log(' curl http://127.0.0.1:9090/metrics');
console.log('');