40 lines
1.2 KiB
Markdown
40 lines
1.2 KiB
Markdown
|
|
# Prometheus sample code
|
||
|
|
|
||
|
|
This is a sample main that will use the custom doczy prometheus library to expose some typical
|
||
|
|
metrics.
|
||
|
|
|
||
|
|
`task compose:up:test` (to start prometheus)
|
||
|
|
Then run
|
||
|
|
`go run ./cmd/metricsExample_test/main.go`
|
||
|
|
to start the metrics example server.
|
||
|
|
|
||
|
|
Prometheus console is at http://localhost:9090/
|
||
|
|
|
||
|
|
## Example queries for the
|
||
|
|
Use these queries in the prometheus console to see the metrics from the sample main.go
|
||
|
|
|
||
|
|
### Total requests per endpoint (rate over 5m)
|
||
|
|
rate(myapp_api_api_requests_total[5m])
|
||
|
|
|
||
|
|
### Request rate by status code
|
||
|
|
sum by (status) (rate(myapp_api_api_requests_total[5m]))
|
||
|
|
|
||
|
|
### Request rate by method
|
||
|
|
sum by (method) (rate(myapp_api_api_requests_total[5m]))
|
||
|
|
|
||
|
|
### Current active users by type
|
||
|
|
myapp_api_active_users
|
||
|
|
|
||
|
|
### Total active users across all types
|
||
|
|
sum(myapp_api_active_users)
|
||
|
|
|
||
|
|
## Histograms
|
||
|
|
### 95th percentile request duration by endpoint
|
||
|
|
histogram_quantile(0.95, sum(rate(myapp_api_request_duration_seconds_bucket[5m])) by (endpoint, le))
|
||
|
|
|
||
|
|
### Average request duration
|
||
|
|
rate(myapp_api_request_duration_seconds_sum[5m]) / rate(myapp_api_request_duration_seconds_count[5m])
|
||
|
|
|
||
|
|
### Request duration heatmap (if you have Grafana)
|
||
|
|
myapp_api_request_duration_seconds_bucket
|