lens.providers
Internal: build TracerProvider + MeterProvider + LoggerProvider.
Only imported on exporting ranks when telemetry is enabled. All heavy SDK
imports live here; opentelemetry-api (no-op) is the only dependency for
code paths that never reach this module.
Module Contents
Classes
Functions
Data
API
OTel-compatible IdGenerator whose IDs survive a global random.seed().
OTel’s default RandomIdGenerator draws from the process-global random
module, which training frameworks (Megatron) seed IDENTICALLY across
data-parallel ranks -> every rank would emit the SAME span/trace IDs, so a
backend sees many spans sharing one span ID and parent links resolve to the
wrong span. A private random.Random instance is seeded from the OS at
construction and is unaffected by random.seed() on the global module.
Duck-typed rather than subclassing opentelemetry.sdk.trace.id_generator. IdGenerator (same as :class:~nemo.lens.sampling.RankAwareSampler) so this
module stays importable without the SDK installed.
Declares the W3C random-trace-id trace flag (Trace Context Level 2).
Span processor that ends still-open spans on provider shutdown.
BatchSpanProcessor only emits a span on on_end, so a span still open
when the process exits is never exported — which silently drops exactly the
long-lived spans that wrap a whole run. This tracks in-flight spans and ends
the leftovers on shutdown.
Must be registered BEFORE the BatchSpanProcessor: TracerProvider’s
default SynchronousMultiSpanProcessor shuts its children down in
registration order, so ending a straggler here still reaches a live batch
processor via on_end, and its own shutdown then flushes the queue. This
ordering is the whole design, and it does NOT hold for
ConcurrentMultiSpanProcessor, whose shutdown runs in parallel — do
not swap the provider’s active_span_processor for that one.
A force-closed span carries nemo.span.truncated and a matching event: its
end time is the shutdown time, not the true end, and a consumer must be able
to tell the difference.
force_flush is deliberately a no-op — it runs mid-run to push already
ended spans and must not terminate live ones.
Duck-typed rather than subclassing opentelemetry.sdk.trace.SpanProcessor
(same as :class:SeedIndependentIdGenerator) so this module stays importable
without the SDK installed — every SDK import here is function-local.
Format one OTel record (span, metrics batch, or log record) as a JSON line.
The SDK’s console exporters default to to_json(indent=4), which emits a
multi-line block per record instead of one JSON object per line. Passing
indent=None keeps each record on a single line, so a redirected console
export is real JSONL that downstream tooling (e.g. perfetto conversion) can
read directly.
Resolve the OTLP wire protocol for the given signal (‘traces’ / ‘metrics’ / ‘logs’).
Honours the OTel SDK convention: OTEL_EXPORTER_OTLP_<SIGNAL>_PROTOCOL
takes precedence over OTEL_EXPORTER_OTLP_PROTOCOL. Defaults to "grpc"
when neither is set, matching the OTel SDK’s default.
Recognised values: grpc, http/protobuf, http/json.
Set W3C TraceContext + Baggage as the global text map propagator.
Set up the OTel LoggerProvider for log bridging.
Register no-op providers for non-exporting ranks or disabled telemetry.
Initialise TracerProvider, MeterProvider, and optionally LoggerProvider.
Imports the OTel SDK. Raises ImportError if not installed.
Parameters:
Telemetry configuration.
Current process rank.
Total number of ranks.
Extra resource attributes to merge.
Optional custom span exporter (bypasses config-based exporter).
Optional custom metric reader (bypasses config-based reader).