pixano_inference.observability
Observability: request-id propagation, structured logging, and Prometheus metrics.
Three concerns, all optional-at-runtime and cheap:
- Request id: :class:
RequestContextMiddleware(a pure-ASGI middleware, so the id propagates via aContextVarinto endpoints and log records) tags every request with anX-Request-ID— echoed from the client's header or freshly generated — exposes it onrequest.state.request_idand the response header, and makes it available to logs. - Logging: :func:
configure_logginginstalls adictConfigwhose formatter includes the current request id (plain or JSON). - Metrics: :class:
PrometheusMiddlewarerecords request count / latency / in-flight gauges; :func:render_metricsrenders the exposition for a/metricsscrape endpoint. Ray Serve's own replica/queue metrics are exported separately by Serve.
JsonLogFormatter
Bases: Formatter
Minimal structured JSON log formatter carrying the request id.
format(record)
Serialize the record to a single-line JSON object.
Source code in pixano_inference/observability.py
PrometheusMiddleware(app)
Record request count, latency, and in-flight gauge per method and route template.
The path label is the matched route template (e.g. /v1/inference/detection), not
the raw URL, so cardinality stays bounded; unmatched requests are labelled unmatched.
Source code in pixano_inference/observability.py
__call__(scope, receive, send)
async
Time the request and update the metrics, tolerating a missing prometheus_client.
Source code in pixano_inference/observability.py
RequestContextMiddleware(app)
Assign each HTTP request an id and expose it on state, logs, and the response header.
Source code in pixano_inference/observability.py
__call__(scope, receive, send)
async
Set the request-id ContextVar and inject the X-Request-ID response header.
Source code in pixano_inference/observability.py
RequestIdFilter
Bases: Filter
Inject the current request id onto every log record as request_id.
configure_logging(level='INFO', json_logs=False)
Configure root logging with a request-id-aware formatter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Root log level name (e.g. |
'INFO'
|
json_logs
|
bool
|
Emit structured JSON logs instead of the plain text format. |
False
|
Source code in pixano_inference/observability.py
get_request_id()
install_observability_middleware(app)
Add the metrics and request-id middleware to app (request-id outermost).
add_middleware stacks last-added outermost, so adding metrics first then the request-id
middleware makes the request id available to the metrics layer, the error handlers, and the
response headers for every request.
Source code in pixano_inference/observability.py
render_metrics()
Return the Prometheus exposition body and its content type for a /metrics route.