@@ -40,6 +40,7 @@ Key Features
4040- Database logging and/or real-time signal notifications
4141- Built-in admin dashboard with charts and performance metrics
4242- Per-request API profiling with auto-diagnosis of bottlenecks
43+ - Request correlation through request attributes, logging context, and signals
4344
4445
4546Supported Versions
@@ -176,6 +177,8 @@ Signal data structure:
176177 ' tracing_id' : ' uuid4-string' , # if tracing enabled
177178 ' profiling_data' : { ... }, # if profiling enabled
178179 ' sql_query_count' : 5 , # if profiling enabled
180+ ' correlation' : { ... }, # if correlation enabled
181+ ' low_cardinality' : { ... }, # if correlation enabled
179182 }
180183
181184
@@ -347,6 +350,26 @@ Configuration Reference
347350 - str
348351 - ``None ``
349352 - Header name to read tracing ID from
353+ * - ``DRF_API_LOGGER_ENABLE_CORRELATION ``
354+ - bool
355+ - ``False ``
356+ - Enable request correlation metadata without adding database columns
357+ * - ``DRF_API_LOGGER_CORRELATION_REQUEST_ID_HEADERS ``
358+ - list
359+ - ``['X-Request-ID', 'X-Correlation-ID'] ``
360+ - Headers checked for inbound request IDs
361+ * - ``DRF_API_LOGGER_CORRELATION_TRACE_ID_HEADERS ``
362+ - list
363+ - ``['traceparent', 'X-Trace-ID'] ``
364+ - Headers checked for W3C traceparent or trace IDs
365+ * - ``DRF_API_LOGGER_CORRELATION_CONTEXT_FUNC ``
366+ - str
367+ - ``None ``
368+ - Dotted-path callback returning allowlisted opaque context IDs
369+ * - ``DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT ``
370+ - bool
371+ - ``False ``
372+ - Expose correlation metadata through a ContextVar during the view call
350373 * - ``DRF_API_LOGGER_CUSTOM_HANDLER ``
351374 - str
352375 - ``None ``
@@ -440,6 +463,79 @@ Read tracing ID from request header:
440463 DRF_API_LOGGER_TRACING_ID_HEADER_NAME = ' X-Trace-ID'
441464
442465
466+ Request Correlation
467+ ===================
468+
469+ Enable request correlation to connect API logger events with application logs,
470+ upstream gateways, distributed traces, or metrics labels:
471+
472+ .. code-block :: python
473+
474+ DRF_API_LOGGER_ENABLE_CORRELATION = True
475+ DRF_API_LOGGER_CORRELATION_REQUEST_ID_HEADERS = [" X-Request-ID" , " X-Correlation-ID" ]
476+ DRF_API_LOGGER_CORRELATION_TRACE_ID_HEADERS = [" traceparent" , " X-Trace-ID" ]
477+ DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT = True
478+
479+ Correlation metadata is intentionally not stored in ``APILogsModel ``. Enabling
480+ it does not add model fields, migrations, admin columns, database indexes, or
481+ synthetic fields in queued database log rows.
482+
483+ When enabled, request handlers can read:
484+
485+ - ``request.api_logger_correlation ``
486+ - ``request.api_logger_low_cardinality ``
487+ - ``request.api_logger_request_id ``
488+ - ``request.api_logger_trace_id ``
489+ - ``drf_api_logger.logging_context.get_correlation_context() ``
490+
491+ Signal listeners receive ``correlation `` and ``low_cardinality `` keys. Use the
492+ low-cardinality dictionary for metrics tags such as route, URL name, and status
493+ class. Keep high-cardinality request and trace IDs in logs or trace systems, not
494+ metrics labels.
495+
496+ .. code-block :: python
497+
498+ from drf_api_logger import API_LOGGER_SIGNAL
499+
500+ def forward_api_event (** kwargs ):
501+ labels = kwargs.get(" low_cardinality" , {})
502+ correlation = kwargs.get(" correlation" , {})
503+ metrics.count(
504+ " drf_api_logger.request" ,
505+ tags = {
506+ " route" : labels.get(" route" ),
507+ " status_class" : labels.get(" status_class" ),
508+ },
509+ )
510+ app_logger.info(
511+ " api request observed" ,
512+ extra = {
513+ " request_id" : correlation.get(" request_id" ),
514+ " trace_id" : correlation.get(" trace_id" ),
515+ },
516+ )
517+
518+ API_LOGGER_SIGNAL .listen += forward_api_event
519+
520+ Add opaque, non-sensitive context through an allowlisted callback:
521+
522+ .. code-block :: python
523+
524+ DRF_API_LOGGER_CORRELATION_CONTEXT_FUNC = " myapp.logging.api_logger_context"
525+
526+ def api_logger_context (request ):
527+ return {
528+ " actor_id" : getattr (request.user, " pk" , None ),
529+ " tenant_id" : getattr (request, " tenant_id" , None ),
530+ " api_consumer_id" : getattr (request, " api_consumer_id" , None ),
531+ " client_id" : getattr (request, " client_id" , None ),
532+ }
533+
534+ Only ``actor_id ``, ``tenant_id ``, ``api_consumer_id ``, and ``client_id `` are
535+ accepted from the callback. Do not return names, emails, tokens, or other
536+ identifying values.
537+
538+
443539Path Configuration
444540==================
445541
0 commit comments