Performance Monitoring

Sentinel automatically tracks per-route latency and throughput using lightweight middleware. Every HTTP request is measured — no manual instrumentation required. Metrics are aggregated in memory and exposed via API endpoints and the built-in dashboard.

Always On

Performance monitoring is enabled by default when Sentinel is mounted. The middleware adds negligible overhead (nanosecond-precision timing around your handlers). Configure PerformanceConfig to tune slow-request thresholds and retention.

What Gets Tracked

The performance middleware captures the following data points for every request:

Data PointDescription
DurationTotal time from request received to response written, measured with time.Since.
StatusCodeHTTP status code returned to the client.
ResponseSizeSize of the response body in bytes.
MethodHTTP method (GET, POST, etc.).
PathRequest path, used for per-route aggregation.

Configuration

Customize performance monitoring through PerformanceConfig. The only tunable field is the slow-request threshold — everything else works out of the box.

main.gogo
1import (
2 "time"
3
4 sentinel "github.com/MUKE-coder/sentinel"
5 "github.com/gin-gonic/gin"
6)
7
8func main() {
9 r := gin.Default()
10
11 sentinel.Mount(r, nil, sentinel.Config{
12 Performance: sentinel.PerformanceConfig{
13 SlowRequestThreshold: 2 * time.Second,
14 },
15 })
16
17 r.GET("/api/data", func(c *gin.Context) {
18 c.JSON(200, gin.H{"status": "ok"})
19 })
20
21 r.Run(":8080")
22}

PerformanceConfig Reference

FieldTypeDefaultDescription
SlowRequestThresholdtime.Duration1 * time.SecondRequests exceeding this duration are flagged as slow in metrics and the dashboard.

Metrics

Sentinel computes the following aggregate metrics from collected request data. Metrics are maintained per route and system-wide.

MetricScopeDescription
p50Per-route / GlobalMedian latency — 50th percentile of request durations.
p95Per-route / Global95th percentile latency — captures tail performance.
p99Per-route / Global99th percentile latency — worst-case excluding extreme outliers.
errorRatePer-route / GlobalPercentage of requests returning 5xx status codes.
throughputPer-route / GlobalRequests per second over the most recent measurement window.
avgResponseSizePer-routeAverage response body size in bytes.
slowRequestsPer-route / GlobalCount of requests exceeding SlowRequestThreshold.

API Endpoints

Performance data is available through two REST endpoints served by the Sentinel dashboard router.

System Overview

Returns a high-level snapshot of system-wide performance metrics.

GET /sentinel/api/performance/overview
responsejson
1{
2 "totalRequests": 48210,
3 "avgResponseTime": "12ms",
4 "p50": "8ms",
5 "p95": "45ms",
6 "p99": "120ms",
7 "errorRate": 0.023,
8 "throughput": 82.5,
9 "slowRequests": 37
10}

Per-Route Breakdown

Returns latency, throughput, and error metrics broken down by route.

GET /sentinel/api/performance/routes
responsejson
1{
2 "routes": [
3 {
4 "method": "GET",
5 "path": "/api/users",
6 "totalRequests": 12400,
7 "avgDuration": "15ms",
8 "p50": "10ms",
9 "p95": "52ms",
10 "p99": "140ms",
11 "errorRate": 0.01,
12 "throughput": 21.3,
13 "avgResponseSize": 2048,
14 "slowRequests": 8
15 },
16 {
17 "method": "POST",
18 "path": "/api/orders",
19 "totalRequests": 3500,
20 "avgDuration": "85ms",
21 "p50": "60ms",
22 "p95": "210ms",
23 "p99": "450ms",
24 "errorRate": 0.04,
25 "throughput": 6.1,
26 "avgResponseSize": 512,
27 "slowRequests": 12
28 }
29 ]
30}

Dashboard

The Sentinel dashboard includes a dedicated Performance page that displays a sortable route metrics table. Each row shows a route's method, path, request count, latency percentiles (p50 / p95 / p99), error rate, throughput, and slow request count. Use the table to identify your slowest and most error-prone endpoints at a glance.

Real-Time Updates

The performance dashboard refreshes automatically. Metrics update as new requests are processed — no manual polling required.

Slow Request Detection

Any request whose duration exceeds SlowRequestThreshold is flagged as a slow request. Slow requests are counted separately in both the system overview and per-route metrics, making it easy to spot routes that need optimization.

// Flag requests taking longer than 500ms
Performance: sentinel.PerformanceConfig{
SlowRequestThreshold: 500 * time.Millisecond,
}
// Flag requests taking longer than 3 seconds
Performance: sentinel.PerformanceConfig{
SlowRequestThreshold: 3 * time.Second,
}

Threshold Tuning

Set the threshold based on your application's performance expectations. A value that is too low floods the slow request count with noise; too high and genuinely slow endpoints go unnoticed. Start with 1 * time.Second and adjust after observing real traffic.

How It Works

Performance monitoring is implemented as Gin middleware that wraps every request handler.

  1. Before handler — The middleware records time.Now() as the start time.
  2. Handler executes — The request is passed through to your route handler via c.Next().
  3. After handler — The middleware computes the duration (time.Since(start)), captures the status code and response size, and builds a PerformanceMetric struct.
  4. Pipeline emission — The metric is emitted to Sentinel's async event pipeline via a non-blocking send. This decouples metric aggregation from request handling, adding no meaningful latency.
  5. Aggregation — A pipeline handler receives the metric and updates in-memory aggregates: increments request counters, inserts the duration into a percentile sketch, updates error counts, and checks against SlowRequestThreshold.
# Request lifecycle with performance monitoring:
#
# Request ──> Middleware records start time
# │
# v
# c.Next() ──> Your handler runs
# │
# v
# Compute duration, status, size
# │
# v
# Emit PerformanceMetric to pipeline (non-blocking)
# │
# v
# Response sent to client
#
# Pipeline (async):
# PerformanceMetric ──> Update aggregates
# ├── Percentile sketch (p50/p95/p99)
# ├── Error rate counter
# ├── Throughput counter
# └── Slow request check

Zero Latency Impact

The metric emission is non-blocking. If the pipeline buffer is full, the metric is dropped rather than stalling the response. Under normal operation, no metrics are lost.

Next Steps


Built with by JB