#!/usr/bin/env node
/**
* Simple test script to verify metrics functionality
*/
import { spawn } from 'child_process';
import { setTimeout } from 'timers/promises';
async function testMetrics() {
console.log('π Starting Analytical MCP Server with metrics...');
// Start the server
const server = spawn('node', ['build/index.js'], {
env: {
...process.env,
METRICS_ENABLED: 'true',
METRICS_PORT: '9090',
LOG_LEVEL: 'INFO'
},
stdio: ['pipe', 'pipe', 'pipe']
});
let serverReady = false;
// Listen for server startup messages
server.stdout.on('data', (data) => {
const output = data.toString();
console.log('π Server output:', output.trim());
if (output.includes('Metrics server started on port 9090')) {
serverReady = true;
}
});
server.stderr.on('data', (data) => {
console.error('β Server error:', data.toString().trim());
});
// Wait for server to start
console.log('β±οΈ Waiting for server to start...');
let attempts = 0;
while (!serverReady && attempts < 30) {
await setTimeout(1000);
attempts++;
}
if (!serverReady) {
console.error('β Server failed to start within 30 seconds');
server.kill();
process.exit(1);
}
console.log('β
Server started successfully!');
// Test metrics endpoints
try {
console.log('π Testing metrics endpoints...');
// Test Prometheus metrics
const prometheusResponse = await fetch('http://localhost:9090/metrics');
if (prometheusResponse.ok) {
const metrics = await prometheusResponse.text();
console.log('β
Prometheus metrics endpoint working');
console.log('π Sample metrics:');
console.log(metrics.split('\n').slice(0, 10).join('\n'));
} else {
console.error('β Prometheus metrics endpoint failed');
}
// Test JSON metrics
const jsonResponse = await fetch('http://localhost:9090/metrics?format=json');
if (jsonResponse.ok) {
const metrics = await jsonResponse.json();
console.log('β
JSON metrics endpoint working');
console.log('π Uptime:', Math.floor(metrics.uptime / 1000), 'seconds');
console.log('π§ Circuit breakers:', Object.keys(metrics.circuitBreakers).length);
console.log('πΎ Cache namespaces:', Object.keys(metrics.cache.general).length + Object.keys(metrics.cache.research).length);
} else {
console.error('β JSON metrics endpoint failed');
}
// Test health endpoint
const healthResponse = await fetch('http://localhost:9090/health');
if (healthResponse.ok) {
const health = await healthResponse.json();
console.log('β
Health endpoint working');
console.log('π Status:', health.status);
} else {
console.error('β Health endpoint failed');
}
console.log('π All metrics endpoints are working correctly!');
} catch (error) {
console.error('β Error testing metrics endpoints:', error.message);
}
// Clean up
console.log('π Stopping server...');
server.kill();
// Wait a bit for clean shutdown
await setTimeout(2000);
console.log('β
Test completed!');
}
// Run the test
testMetrics().catch(console.error);