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,
)
Requires the [api] extra::
pip install dataenginex[api]
PrometheusMetricsMiddleware
Bases: BaseHTTPMiddleware
Middleware to collect Prometheus metrics for HTTP requests.
Source code in src/dataenginex/middleware/metrics_middleware.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
dispatch(request, call_next)
async
Process request and collect metrics.
Source code in src/dataenginex/middleware/metrics_middleware.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
RequestLoggingMiddleware
Bases: BaseHTTPMiddleware
Middleware to log all HTTP requests with request ID tracking.
Source code in src/dataenginex/middleware/request_logging.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
__init__(app)
Initialize middleware.
Source code in src/dataenginex/middleware/request_logging.py
28 29 30 31 | |
dispatch(request, call_next)
async
Process request and add logging context.
Source code in src/dataenginex/middleware/request_logging.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
configure_logging(log_level='INFO', json_logs=True)
Configure 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 src/dataenginex/middleware/logging_config.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
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 src/dataenginex/middleware/logging_config.py
122 123 124 125 126 127 128 129 130 131 | |
get_metrics()
Generate Prometheus metrics in text format.
Returns:
| Type | Description |
|---|---|
tuple[bytes, str]
|
Tuple of (metrics_data, content_type) |
Source code in src/dataenginex/middleware/metrics.py
62 63 64 65 66 67 68 69 | |
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 src/dataenginex/middleware/tracing.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | |
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 src/dataenginex/middleware/tracing.py
94 95 96 97 98 99 100 101 102 103 104 | |
instrument_fastapi(app)
Instrument FastAPI application with OpenTelemetry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app
|
FastAPI
|
FastAPI application instance |
required |
Source code in src/dataenginex/middleware/tracing.py
84 85 86 87 88 89 90 91 | |