|
| 1 | +# Prometheus Metrics |
| 2 | + |
| 3 | +This application exposes Prometheus-compatible metrics on a separate port from the main API server. |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +The metrics server runs on a separate port configured via the `METRICS_PORT` environment variable: |
| 8 | + |
| 9 | +```bash |
| 10 | +# Default: 9090 |
| 11 | +METRICS_PORT=9090 |
| 12 | +``` |
| 13 | + |
| 14 | +Add this to your `.env` file. See `.env.sample` for reference. |
| 15 | + |
| 16 | +## Metrics Endpoint |
| 17 | + |
| 18 | +The metrics are served at: |
| 19 | + |
| 20 | +``` |
| 21 | +http://localhost:9090/metrics |
| 22 | +``` |
| 23 | + |
| 24 | +(Replace `9090` with your configured `METRICS_PORT` if different) |
| 25 | + |
| 26 | +## Available Metrics |
| 27 | + |
| 28 | +### Default Node.js Metrics |
| 29 | + |
| 30 | +The following default Node.js metrics are automatically collected: |
| 31 | + |
| 32 | +- **nodejs_version_info** - Node.js version information |
| 33 | +- **process_cpu_user_seconds_total** - Total user CPU time spent in seconds |
| 34 | +- **process_cpu_system_seconds_total** - Total system CPU time spent in seconds |
| 35 | +- **nodejs_heap_size_total_bytes** - Total heap size in bytes |
| 36 | +- **nodejs_heap_size_used_bytes** - Used heap size in bytes |
| 37 | +- **nodejs_external_memory_bytes** - External memory in bytes |
| 38 | +- **nodejs_heap_space_size_total_bytes** - Total heap space size in bytes |
| 39 | +- **nodejs_heap_space_size_used_bytes** - Used heap space size in bytes |
| 40 | +- **nodejs_eventloop_lag_seconds** - Event loop lag in seconds |
| 41 | +- **nodejs_eventloop_lag_min_seconds** - Minimum event loop lag |
| 42 | +- **nodejs_eventloop_lag_max_seconds** - Maximum event loop lag |
| 43 | +- **nodejs_eventloop_lag_mean_seconds** - Mean event loop lag |
| 44 | +- **nodejs_eventloop_lag_stddev_seconds** - Standard deviation of event loop lag |
| 45 | +- **nodejs_eventloop_lag_p50_seconds** - 50th percentile event loop lag |
| 46 | +- **nodejs_eventloop_lag_p90_seconds** - 90th percentile event loop lag |
| 47 | +- **nodejs_eventloop_lag_p99_seconds** - 99th percentile event loop lag |
| 48 | + |
| 49 | +### Custom HTTP Metrics |
| 50 | + |
| 51 | +#### http_request_duration_seconds (Histogram) |
| 52 | + |
| 53 | +Duration of HTTP requests in seconds, labeled by: |
| 54 | +- `method` - HTTP method (GET, POST, etc.) |
| 55 | +- `route` - Request route/path |
| 56 | +- `status_code` - HTTP status code |
| 57 | + |
| 58 | +Buckets: 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5, 10 seconds |
| 59 | + |
| 60 | +#### http_requests_total (Counter) |
| 61 | + |
| 62 | +Total number of HTTP requests, labeled by: |
| 63 | +- `method` - HTTP method (GET, POST, etc.) |
| 64 | +- `route` - Request route/path |
| 65 | +- `status_code` - HTTP status code |
| 66 | + |
| 67 | +## Testing |
| 68 | + |
| 69 | +### Manual Testing |
| 70 | + |
| 71 | +You can test the metrics endpoint using curl: |
| 72 | + |
| 73 | +```bash |
| 74 | +curl http://localhost:9090/metrics |
| 75 | +``` |
| 76 | + |
| 77 | +Or run the provided test script: |
| 78 | + |
| 79 | +```bash |
| 80 | +./test-metrics.sh |
| 81 | +``` |
| 82 | + |
| 83 | +### Integration Tests |
| 84 | + |
| 85 | +Integration tests for metrics are located in `test/integration/cases/metrics.test.ts`. |
| 86 | + |
| 87 | +Run them with: |
| 88 | + |
| 89 | +```bash |
| 90 | +npm run test:integration |
| 91 | +``` |
| 92 | + |
| 93 | +## Implementation Details |
| 94 | + |
| 95 | +The metrics implementation uses the `prom-client` library and consists of: |
| 96 | + |
| 97 | +1. **Metrics Module** (`src/metrics/index.ts`): |
| 98 | + - Initializes a Prometheus registry |
| 99 | + - Configures default Node.js metrics collection |
| 100 | + - Defines custom HTTP metrics (duration histogram and request counter) |
| 101 | + - Provides middleware for tracking HTTP requests |
| 102 | + - Creates a separate Express app for serving metrics |
| 103 | + |
| 104 | +2. **Integration** (`src/index.ts`): |
| 105 | + - Adds metrics middleware to the main Express app |
| 106 | + - Starts metrics server on a separate port |
| 107 | + - Keeps metrics server isolated from main API traffic |
| 108 | + |
| 109 | +## Prometheus Configuration |
| 110 | + |
| 111 | +To scrape these metrics with Prometheus, add the following to your `prometheus.yml`: |
| 112 | + |
| 113 | +```yaml |
| 114 | +scrape_configs: |
| 115 | + - job_name: 'hawk-api' |
| 116 | + static_configs: |
| 117 | + - targets: ['localhost:9090'] |
| 118 | +``` |
| 119 | +
|
| 120 | +Adjust the target host and port according to your deployment. |
0 commit comments