Middle
What metrics would you add for monitoring HTTP API?
sobes.tech AI
Answer from AI
For monitoring HTTP API, it is important to collect metrics that help understand performance, stability, and user experience. Here are the main metrics I would add:
- Request Count — the total number of processed requests, helps assess load.
- Latency/Response Time — measured in milliseconds, it is important to track the average and percentiles (p95, p99) to identify delays.
- HTTP Status Codes — distribution by categories (2xx, 3xx, 4xx, 5xx) to quickly identify errors.
- Error Rate — the percentage of requests that ended with an error (e.g., 5xx), for stability monitoring.
- Throughput — number of requests per second, reflects the load on the service.
- Active Connections — the current number of open connections, helps identify resource issues.
- Resource Usage — CPU, memory, disk, network at the service level.
Additionally, you can add metrics specific to business logic, such as the number of successful transactions or user events.
Example of using Prometheus metrics in Go:
httpRequestsTotal := prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests",
},
[]string{"method", "endpoint", "status"},
)
httpRequestDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "HTTP request processing time",
Buckets: prometheus.DefBuckets,
},
[]string{"method", "endpoint"},
)
By collecting and analyzing these metrics, you can quickly respond to issues and improve service quality.