dataenginex.middleware¶
Logging, metrics, tracing, and request handling middleware.
dataenginex.middleware
¶
Middleware — logging, metrics, tracing, and request handling.
Public API::
from dataenginex.middleware import (
configure_logging, get_logger, APP_VERSION,
get_metrics, PrometheusMetricsMiddleware,
RequestLoggingMiddleware,
configure_tracing, instrument_fastapi, get_tracer,
)
PrometheusMetricsMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware to collect Prometheus metrics for HTTP requests.
Source code in packages/dataenginex/src/dataenginex/middleware/metrics_middleware.py
dispatch(request, call_next)
async
¶
Process request and collect metrics.
Source code in packages/dataenginex/src/dataenginex/middleware/metrics_middleware.py
RequestLoggingMiddleware
¶
Bases: BaseHTTPMiddleware
Middleware to log all HTTP requests with request ID tracking.
Source code in packages/dataenginex/src/dataenginex/middleware/request_logging.py
__init__(app)
¶
dispatch(request, call_next)
async
¶
Process request and add logging context.
Source code in packages/dataenginex/src/dataenginex/middleware/request_logging.py
configure_logging(log_level='INFO', json_logs=True)
¶
Configure loguru + structlog for the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
str
|
Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL) |
'INFO'
|
json_logs
|
bool
|
If True, output JSON logs; otherwise use coloured console |
True
|
Source code in packages/dataenginex/src/dataenginex/middleware/logging_config.py
get_logger(name)
¶
Get a configured structlog logger instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name (typically |
required |
Returns:
| Type | Description |
|---|---|
BoundLogger
|
Configured structlog logger |
Source code in packages/dataenginex/src/dataenginex/middleware/logging_config.py
get_metrics()
¶
Generate Prometheus metrics in text format.
Returns:
| Type | Description |
|---|---|
tuple[bytes, str]
|
Tuple of (metrics_data, content_type) |
configure_tracing(service_name=APP_NAME, service_version=APP_VERSION, otlp_endpoint=None, enable_console_export=False)
¶
Configure OpenTelemetry tracing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
service_name
|
str
|
Name of the service |
APP_NAME
|
service_version
|
str
|
Version of the service |
APP_VERSION
|
otlp_endpoint
|
str | None
|
OTLP collector endpoint (e.g., "http://localhost:4317") |
None
|
enable_console_export
|
bool
|
If True, print spans to console (for debugging) |
False
|
Returns:
| Type | Description |
|---|---|
TracerProvider
|
Configured TracerProvider |
Source code in packages/dataenginex/src/dataenginex/middleware/tracing.py
get_tracer(name)
¶
Get a tracer instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tracer name (typically name of the calling module) |
required |
Returns:
| Type | Description |
|---|---|
Tracer
|
Tracer instance |
Source code in packages/dataenginex/src/dataenginex/middleware/tracing.py
instrument_fastapi(app)
¶
Instrument FastAPI application with OpenTelemetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application instance |
required |