Implementing Metrics and Analytics

1. Tracking Request Count Metrics

MetricTypeLabels
gateway_requests_totalCounterroute, method, status
gateway_active_requestsGaugeroute
gateway_request_bytes_totalCounterroute

Example: Prometheus query

sum(rate(gateway_requests_total{status=~"5.."}[5m])) by (route)
/
sum(rate(gateway_requests_total[5m])) by (route)
# Error rate per route

2. Measuring Response Time

MetricType
request_duration_secondsHistogram
upstream_duration_secondsHistogram
tls_handshake_durationHistogram
Bucket boundaries5ms,10,25,50,100,250,500,1s,2.5,5,10

3. Monitoring Error Rates

SLOTarget
Availability99.95% (~22 min/mo)
5xx rate< 0.1%
429 rate< 1%
4xx (excl 429)Track but no SLO

4. Tracking Throughput

MetricUse
RPS per routeCapacity planning
Bytes in/outBandwidth billing
Concurrent connsConnection pool sizing
Goodput (2xx only)Effective service rate

5. Measuring Cache Hit Rates

MetricFormula
Hit ratiohits / (hits + misses)
Byte hit ratiocached_bytes / total_bytes
Origin offload1 - (origin_reqs / total)

6. Monitoring Backend Health Status

MetricDescription
upstream_healthy_targetsGauge per upstream
upstream_health_check_failuresCounter
circuit_breaker_open0/1 gauge
upstream_connect_errorsCounter

7. Tracking Rate Limit Usage

MetricUse
rate_limit_hitsPer consumer/route
rate_limit_rejections429s
quota_used_percentPer plan
Top-N consumersIdentify heavy users

8. Measuring Authentication Failures

CounterAlert If
auth_invalid_token> baseline 3x
auth_expired_tokenSpikes (clock skew?)
auth_brute_forcePer-IP failures
jwks_fetch_errorsIdP outage

9. Monitoring Resource Utilization

ResourceThreshold
CPU< 70% sustained
Memory< 75%
File descriptors< 80% of ulimit
Connections< 80% of pool
Network bandwidth< 70% link

10. Setting Up Custom Metrics

Example: Custom counter (Lua)

local prometheus = require "prometheus"
local metrics = prometheus.init("metrics")
local payment_counter = metrics:counter(
  "payment_attempts_total",
  "Payment attempts",
  {"currency", "method", "status"}
)
-- in handler
payment_counter:inc(1, {currency, method, status})