Instrumentation Primitives
NeMo Lens provides three span primitives. These primitives cover the full spectrum from a “cheap when off, gated by a frozenset lookup” approach to an “always-on ergonomic context manager.” Select a primitive based on the call site.
managed_span as a Group-Gated Context Manager
How It Behaves
- When the
stepgroup is disabled, the function yieldsNone, the body runs unchanged, and no span object is created. The gating check is a fastfrozensetlookup. - When the
stepgroup is enabled, the function starts a span, sets attributes, attaches its context, and yields the span. On exit, the function detaches the context and ends the span. If the body raises an exception, that exception is recorded on the span withStatusCode.ERRORbefore re-raising.
When to Use It
- Hot paths with configurable granularity. Use this for training steps, microbatches, or communication operations where configurable granularity is required.
- Blocks where telemetry is frequently disabled. Use this for any block of code where performance overhead must be minimized when telemetry is disabled.
Tracer Resolution
If you do not pass a tracer argument, managed_span obtains a named tracer from the global TracerProvider using the nemo.lens.helpers instrumentation scope. This defaults to the same scope used by span_cm, whereas trace_fn defaults to the nemo.lens scope instead. Passing tracer=handle.tracer (which uses the nemo.lens scope) skips this get_tracer call, allowing spans to share the handle’s instrumentation scope.
trace_fn as a Group-Gated Decorator
How It Behaves
This decorator has identical gating semantics to managed_span, but is applied as a function decorator, requiring no re-indentation of the function body. The span group is checked at call time rather than at decoration time, which enables dynamic toggling of groups.
When to Use It
- Instrument existing functions. Use this to instrument existing functions without restructuring their bodies.
- Span name matches function name. Use this when the desired span name matches the function name, which is typical.
Limitations
trace_fn cannot set span attributes from function arguments without a custom wrapper. For attribute-rich spans, use managed_span inside the function body instead.
span_cm as a Simple Ungated Context Manager
How It Behaves
This context manager always creates a span with no group gating. Attributes are set through safe_set_span_attributes, which processes only scalars and scalar sequences; non-scalar values are silently dropped.
When to Use It
- Required code paths. Use this in code paths where you always want a span when telemetry is active, such as evaluation, setup, or shutdown procedures.
- Utility modules. Use this in utility modules that do not contain information about span groups.
- Cold paths. Use this for spans outside hot paths where the per-call cost of always creating a span is negligible.
No-Op Interaction
When the global TracerProvider is a no-op instance (such as on a non-exporting rank or when telemetry is disabled), start_as_current_span is still called but behaves as a cheap no-op operation within the OTel API, resulting in no exporter round-trip and no attribute processing cost. This call does not bypass span object creation entirely the way managed_span does when its group is disabled, but the performance cost is negligible.
safe_set_span_attributes
Use this utility to bulk-set span attributes with sensible filtering and redaction:
Behavior and Evaluation Rules
- Verify recording status. If
span.is_recording()isFalse, the call is a no-op. - Skip null values.
Nonevalues are silently skipped. - Drop non-scalars. Non-scalar values (such as dictionaries or complex objects) are silently skipped, as OTel attributes must be scalars or sequences of scalars.
- Format sequences. Sequences of scalars are converted to a
list. - Redact sensitive strings. String values whose keys match a redact key are replaced with the string
'[REDACTED]'.
Default Redact Keys
Pass a custom redact_keys set to override.
redact_value
redact_value(key, value, redact_keys=DEFAULT_REDACT_KEYS) is the single-value primitive that safe_set_span_attributes uses internally:
The function returns '[REDACTED]' if and only if the key is present in redact_keys; otherwise, it returns value unchanged. Redaction is decided by the attribute key name rather than by inspecting the value itself.
get_tracer and get_meter
Both are top-level exports that return the globally registered tracer/meter from the active provider:
Each function accepts an optional name argument to set the instrumentation scope, which defaults to 'nemo.lens'. Use these functions when you require a tracer or meter outside the span primitives; for example, you can use them to create custom metric instruments.
Choose the Correct Primitive
Check Group Status Before Expensive Preparation
If building the attributes dict is itself expensive, gate it:
This saves the build_expensive_attributes() cost when step is disabled.