core.otlp

Elegant OTLP integration for jobmon that prevents global log pollution.

Submodules

Attributes

OTLP_AVAILABLE

OpenTelemetryLogFormatter

Classes

JobmonOTLPFormatter

Formatter that adds OpenTelemetry span details to jobmon logs.

JobmonOTLPLoggingHandler

Universal OTLP logging handler with lazy initialization and attribute extraction.

JobmonOTLPStructlogHandler

OTLP logging handler for structlog.

Functions

add_span_details_processor(→ Dict[str, Any])

Structlog processor to add OpenTelemetry span details to log entries.

get_current_span_details(→ Tuple[Optional[str], ...)

Get details of the current OpenTelemetry span.

Package Contents

core.otlp.OTLP_AVAILABLE = True[source]
class core.otlp.JobmonOTLPFormatter(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]

Bases: logging.Formatter

Formatter that adds OpenTelemetry span details to jobmon logs.

Note: For handlers using _JobmonOTLPLoggingHandler, this formatter is not used for OTLP output (handler creates OTLPLogRecord directly). It only affects console/file output.

format(record: logging.LogRecord) str[source]

Format log record with OpenTelemetry span details.

class core.otlp.JobmonOTLPLoggingHandler(level: int = logging.NOTSET, exporter: Any | Dict | None = None, logger_provider: Any | None = None)[source]

Bases: logging.Handler

Universal OTLP logging handler with lazy initialization and attribute extraction.

This handler extracts attributes from structlog’s thread-local event_dict and supports flexible configuration patterns:

  1. Inline dict configuration (server pattern):
    handlers:
    otlp_logs:

    class: jobmon.core.otlp.JobmonOTLPLoggingHandler level: INFO exporter:

    module: opentelemetry.exporter.otlp.proto.grpc._log_exporter class: OTLPLogExporter endpoint: otelcol.dev.aks.scicomp.ihme.washington.edu:443 options: [[“grpc.max_send_message_length”, 16777216]] max_export_batch_size: 8

  2. Pre-configured exporter instance:

    handler = JobmonOTLPLoggingHandler(exporter=my_exporter)

  3. Direct use with logger_provider (for testing):

    handler = JobmonOTLPLoggingHandler(logger_provider=my_provider)

_SEVERITY_MAP = None
classmethod _get_severity_map() Dict[str, Any][source]

Get severity map with lazy import.

_exporter_config = None
_logger_provider = None
_logger: Any | None = None
_initialized = False
_capture_registered = False
_ensure_initialized() bool[source]

Ensure logger provider is initialized. Returns True if ready.

close() None[source]

Tidy up any resources used by the handler.

This version removes the handler from an internal map of handlers, _handlers, which is used for handler lookup by name. Subclasses should ensure that this gets called from overridden close() methods.

_extract_attributes(event_dict: Dict[str, Any]) Dict[str, Any][source]

Extract OTLP attributes from event_dict.

Strips the ‘telemetry_’ prefix from attribute names for cleaner OTLP exports while maintaining internal namespacing.

_parse_trace_context(event_dict: Dict[str, Any] | None) tuple[int, int][source]

Parse trace and span IDs from event_dict or current span.

emit(record: logging.LogRecord) None[source]

Emit log record to OTLP with extracted attributes.

class core.otlp.JobmonOTLPStructlogHandler(level: int = logging.NOTSET, exporter: Any | Dict | None = None, logger_provider: Any | None = None)[source]

Bases: JobmonOTLPLoggingHandler

OTLP logging handler for structlog.

Identical to JobmonOTLPLoggingHandler - uses the same custom handler that extracts attributes from thread-local event_dict. This class exists for clarity in configuration (to indicate structlog support) but functionally is the same as the parent class.

core.otlp.add_span_details_processor(logger: Any, method_name: str, event_dict: Dict[str, Any]) Dict[str, Any][source]

Structlog processor to add OpenTelemetry span details to log entries.

Parameters:
  • logger – The logger instance (not used, but required by Structlog processor signature).

  • method_name – The logging method name (e.g., “info”, “debug”).

  • event_dict – The event dictionary representing the log entry.

Returns:

The modified event dictionary with OpenTelemetry span details added.

core.otlp.get_current_span_details() Tuple[str | None, str | None, str | None][source]

Get details of the current OpenTelemetry span.

Returns:

Tuple of (span_id, trace_id, parent_span_id) as hex strings, or (None, None, None)

core.otlp.OpenTelemetryLogFormatter[source]