duck.etc.blueprints.dashboard.middleware

Metrics collection middleware and in-memory store for the dashboard blueprint.

Drop MetricsMiddleware into your MIDDLEWARES list and the dashboard will immediately have real data — no configuration required.

The store is a module-level singleton protected by a threading.Lock so it is safe across Duck’s multi-worker environment. All data lives in memory and resets on server restart; swap the store backend here if persistence is needed.

Module Contents

Classes

MetricsMiddleware

Request/response middleware that records metrics into MetricsStore.

MetricsStore

Thread-safe singleton that accumulates all request metrics.

Data

MAX_LOG_ENTRIES

MAX_ROUTE_ENTRIES

WS_UPGRADE_VALUE

store

API

duck.etc.blueprints.dashboard.middleware.MAX_LOG_ENTRIES

200

duck.etc.blueprints.dashboard.middleware.MAX_ROUTE_ENTRIES

100

class duck.etc.blueprints.dashboard.middleware.MetricsMiddleware[source]

Bases: duck.http.middlewares.BaseMiddleware

Request/response middleware that records metrics into MetricsStore.

Captures request start time in process_request, then measures elapsed time and records the full request into the store in process_response. WebSocket upgrades are detected via the Upgrade header and reported as method “WS”.

Register in settings.py: MIDDLEWARES = [ “web.dashboard.middleware.MetricsMiddleware”, ]

LOG_LEVEL_MAP

None

START_TIME_KEY

‘dashboard.metrics.start_time’

access_times

0

debug_message

‘Metrics Middleware: Request metrics collection error’

classmethod process_request(request: duck.http.request.HttpRequest) int[source]

Stamps the request start time into META for latency measurement.

Parameters:

request – The incoming HTTP request.

Returns:

Always request_ok — this middleware never blocks.

classmethod process_response(response: duck.http.response.HttpResponse, request: duck.http.request.HttpRequest) None[source]

Records the completed request into the metrics store.

Computes elapsed time from the META start timestamp, detects WebSocket upgrades via the Upgrade header, and calls store.record_request with all gathered data.

Parameters:
  • response – The outgoing HTTP response.

  • request – The originating HTTP request.

classmethod record_log(level: int, message: str) None[source]

Writes a log record into the metrics store ring buffer.

class duck.etc.blueprints.dashboard.middleware.MetricsStore[source]

Thread-safe singleton that accumulates all request metrics.

All public methods acquire the internal lock and are safe to call from any thread. The dashboard service layer reads from this store directly via the module-level store instance.

Initialization

Initialises all counters, buffers, and the lock.

append_log(level: str, message: str) None[source]

Appends a log entry to the ring buffer.

Parameters:
  • level – Log level string — INFO, WARNING, ERROR, or DEBUG.

  • message – The log message text.

get_latency_percentiles() dict[source]

Returns latency percentile values from the history buffer.

Returns:

Dict with keys p50, p90, p95, p99, avg, history. All values are integers in milliseconds. Returns zeros when no data is available.

get_requests_per_minute() int[source]

Returns the number of requests received in the last 60 seconds.

Returns:

Integer count of recent requests.

get_uptime_str() str[source]

Returns a human-readable uptime string from server start time.

Returns:

String in the format “03h 12m 45s”.

record_request(method: str, path: str, status_code: int, duration_ms: float, is_ws: bool) None[source]

Records a completed request into all relevant counters and buffers.

Parameters:
  • method – HTTP method string, e.g. “GET”.

  • path – Request path, e.g. “/api/products”.

  • status_code – HTTP response status code.

  • duration_ms – Response time in milliseconds.

  • is_ws – True when the request is a WebSocket upgrade.

snapshot() dict[source]

Returns a consistent read of all metrics under a single lock.

Returns:

Dict with copies of method_counts, status_counts, route_stats, total, success, error request counts, requests_per_minute, start_time, and log_entries.

duck.etc.blueprints.dashboard.middleware.WS_UPGRADE_VALUE

‘websocket’

duck.etc.blueprints.dashboard.middleware.store

‘MetricsStore(…)’