core.config.structlog_config
Shared structlog configuration for all jobmon components.
This module provides structlog configuration that enables:
Context variable merging (required for
@bind_contextdecorator)Basic stdlib metadata decoration (logger name, level) while deferring rendering/formatting to the host application
Optional Jobmon telemetry isolation and OTLP capture
Optional component identification in logs
Functions
|
Enable thread-local capture for OTLP handlers. |
|
Disable OTLP capture (used for tests or when handlers are removed). |
|
Context manager to enable OTLP capture temporarily. |
|
Configure structlog for jobmon components. |
|
Create a processor that isolates telemetry metadata to specific logger prefixes. |
|
Configure structlog with OTLP trace integration if enabled. |
Prepend Jobmon processors to an existing structlog configuration. |
|
|
Check if structlog has been configured. |
Module Contents
- core.config.structlog_config.enable_structlog_otlp_capture() None
Enable thread-local capture for OTLP handlers.
- core.config.structlog_config.disable_structlog_otlp_capture() None
Disable OTLP capture (used for tests or when handlers are removed).
- core.config.structlog_config.structlog_otlp_capture_enabled() Iterator[None]
Context manager to enable OTLP capture temporarily.
Ensures the reference count is decremented even if an exception is raised. Particularly useful in tests that need to toggle capture around assertions.
- core.config.structlog_config.configure_structlog(component_name: str | None = None, *, extra_processors: Iterable[Callable] | None = None) None
Configure structlog for jobmon components.
This function sets up structlog with processors that:
Merge context variables (from bind_contextvars and
@bind_contextdecorator)Add logger metadata (logger name, log level)
Optionally add a component field to the event_dict
Isolate Jobmon telemetry metadata to jobmon.* loggers
Capture the raw event_dict for OTLP handlers
Optionally include extra processors supplied by the caller
IMPORTANT: This must be called before using the
@bind_contextdecorator or anystructlog.contextvars.bind_contextvars()calls.PROCESSOR CHAIN ORDER (after configuration):
merge_contextvars (Jobmon context)
component processor (optional, when component_name is provided)
filter_by_level (stdlib)
add_logger_name (stdlib)
add_log_level (stdlib)
telemetry isolation processor
_store_event_dict_for_otlp(Jobmon OTLP capture)Extra processors supplied via
extra_processors(if any)ProcessorFormatter.wrap_for_formatter (stdlib - keeps stdlib handlers working)
- Parameters:
component_name – Component name to add to all logs (e.g., “distributor”)
extra_processors – Additional structlog processors to append after Jobmon’s defaults (e.g., custom formatting or telemetry processors)
Example:
# Basic usage configure_structlog(component_name="distributor")
- core.config.structlog_config.create_telemetry_isolation_processor(telemetry_prefixes: List[str]) Callable[[Any, str, Dict[str, Any]], Dict[str, Any]]
Create a processor that isolates telemetry metadata to specific logger prefixes.
- Parameters:
telemetry_prefixes – List of logger name prefixes that should receive telemetry metadata (e.g., [“jobmon.”, “myapp.telemetry”]).
- Returns:
A structlog processor function that isolates telemetry metadata.
- core.config.structlog_config.configure_structlog_with_otlp(component_name: str) None
Configure structlog with OTLP trace integration if enabled.
Checks JobmonConfig for telemetry.tracing settings and configures structlog with OpenTelemetry trace processors if enabled.
This is the recommended function to call from component CLIs as it automatically handles OTLP configuration based on settings.
- Parameters:
component_name – Component name (e.g., “distributor”, “worker”)
Example
>>> # In distributor CLI >>> configure_structlog_with_otlp(component_name="distributor")
>>> # In worker CLI >>> configure_structlog_with_otlp(component_name="worker")
- core.config.structlog_config.prepend_jobmon_processors_to_existing_config() None
Prepend Jobmon processors to an existing structlog configuration.
This is called when Jobmon is used as a library and the host application has already configured structlog. It intelligently prepends Jobmon’s processors to the existing processor chain to enable telemetry context management and isolation while preserving the host application’s final rendering.
Adapts to the host app’s logging architecture:
Stdlib integration adds
merge_contextvars,filter_by_level,add_logger_name, and telemetry isolationDirect rendering (like FHS) adds
merge_contextvarsand telemetry isolationHost processors remain untouched so final rendering is preserved