docs(readme): add metrics and monitoring documentation
Document the new metrics collection system including available metrics methods, usage examples, and export formats for external monitoring systems.
This commit is contained in:
118
readme.md
118
readme.md
@ -919,6 +919,124 @@ Available helper functions:
|
||||
})
|
||||
```
|
||||
|
||||
## Metrics and Monitoring
|
||||
|
||||
SmartProxy includes a comprehensive metrics collection system that provides real-time insights into proxy performance, connection statistics, and throughput data.
|
||||
|
||||
### Getting Metrics
|
||||
|
||||
```typescript
|
||||
const proxy = new SmartProxy({ /* config */ });
|
||||
await proxy.start();
|
||||
|
||||
// Access metrics through the getStats() method
|
||||
const stats = proxy.getStats();
|
||||
|
||||
// Get current active connections
|
||||
console.log(`Active connections: ${stats.getActiveConnections()}`);
|
||||
|
||||
// Get total connections since start
|
||||
console.log(`Total connections: ${stats.getTotalConnections()}`);
|
||||
|
||||
// Get requests per second (RPS)
|
||||
console.log(`Current RPS: ${stats.getRequestsPerSecond()}`);
|
||||
|
||||
// Get throughput data
|
||||
const throughput = stats.getThroughput();
|
||||
console.log(`Bytes received: ${throughput.bytesIn}`);
|
||||
console.log(`Bytes sent: ${throughput.bytesOut}`);
|
||||
|
||||
// Get connections by route
|
||||
const routeConnections = stats.getConnectionsByRoute();
|
||||
for (const [route, count] of routeConnections) {
|
||||
console.log(`Route ${route}: ${count} connections`);
|
||||
}
|
||||
|
||||
// Get connections by IP address
|
||||
const ipConnections = stats.getConnectionsByIP();
|
||||
for (const [ip, count] of ipConnections) {
|
||||
console.log(`IP ${ip}: ${count} connections`);
|
||||
}
|
||||
```
|
||||
|
||||
### Available Metrics
|
||||
|
||||
The `IProxyStats` interface provides the following methods:
|
||||
|
||||
- `getActiveConnections()`: Current number of active connections
|
||||
- `getTotalConnections()`: Total connections handled since proxy start
|
||||
- `getRequestsPerSecond()`: Current requests per second (1-minute average)
|
||||
- `getThroughput()`: Total bytes transferred (in/out)
|
||||
- `getConnectionsByRoute()`: Connection count per route
|
||||
- `getConnectionsByIP()`: Connection count per client IP
|
||||
|
||||
### Monitoring Example
|
||||
|
||||
```typescript
|
||||
// Create a monitoring loop
|
||||
setInterval(() => {
|
||||
const stats = proxy.getStats();
|
||||
|
||||
// Log key metrics
|
||||
console.log({
|
||||
timestamp: new Date().toISOString(),
|
||||
activeConnections: stats.getActiveConnections(),
|
||||
rps: stats.getRequestsPerSecond(),
|
||||
throughput: stats.getThroughput()
|
||||
});
|
||||
|
||||
// Check for high connection counts from specific IPs
|
||||
const ipConnections = stats.getConnectionsByIP();
|
||||
for (const [ip, count] of ipConnections) {
|
||||
if (count > 100) {
|
||||
console.warn(`High connection count from ${ip}: ${count}`);
|
||||
}
|
||||
}
|
||||
}, 10000); // Every 10 seconds
|
||||
```
|
||||
|
||||
### Exporting Metrics
|
||||
|
||||
You can export metrics in various formats for external monitoring systems:
|
||||
|
||||
```typescript
|
||||
// Export as JSON
|
||||
app.get('/metrics.json', (req, res) => {
|
||||
const stats = proxy.getStats();
|
||||
res.json({
|
||||
activeConnections: stats.getActiveConnections(),
|
||||
totalConnections: stats.getTotalConnections(),
|
||||
requestsPerSecond: stats.getRequestsPerSecond(),
|
||||
throughput: stats.getThroughput(),
|
||||
connectionsByRoute: Object.fromEntries(stats.getConnectionsByRoute()),
|
||||
connectionsByIP: Object.fromEntries(stats.getConnectionsByIP())
|
||||
});
|
||||
});
|
||||
|
||||
// Export as Prometheus format
|
||||
app.get('/metrics', (req, res) => {
|
||||
const stats = proxy.getStats();
|
||||
res.set('Content-Type', 'text/plain');
|
||||
res.send(`
|
||||
# HELP smartproxy_active_connections Current active connections
|
||||
# TYPE smartproxy_active_connections gauge
|
||||
smartproxy_active_connections ${stats.getActiveConnections()}
|
||||
|
||||
# HELP smartproxy_requests_per_second Current requests per second
|
||||
# TYPE smartproxy_requests_per_second gauge
|
||||
smartproxy_requests_per_second ${stats.getRequestsPerSecond()}
|
||||
|
||||
# HELP smartproxy_bytes_in Total bytes received
|
||||
# TYPE smartproxy_bytes_in counter
|
||||
smartproxy_bytes_in ${stats.getThroughput().bytesIn}
|
||||
|
||||
# HELP smartproxy_bytes_out Total bytes sent
|
||||
# TYPE smartproxy_bytes_out counter
|
||||
smartproxy_bytes_out ${stats.getThroughput().bytesOut}
|
||||
`);
|
||||
});
|
||||
```
|
||||
|
||||
## Other Components
|
||||
|
||||
While SmartProxy provides a unified API for most needs, you can also use individual components:
|
||||
|
Reference in New Issue
Block a user