Extending Instrumentation
To add new spans or metrics to Megatron code, use the instrumentation primitives from nemo.lens. The primitives themselves are documented in
lens: instrumentation.
This page covers Megatron conventions.
Adding a custom span
Simple block — span_cm
span_cm always creates a span when telemetry is active — good for cold paths.
Group-gated block — managed_span
For hot paths where you want minimal cost when the group is disabled:
managed_span yields None when the group is disabled; the body still runs. Check if span is not None before setting attributes.
Fallback pattern
Every import of lens in Megatron code must use the try/except fallback idiom so the code runs when lens isn’t installed:
megatron/core/telemetry/fallbacks.py re-exports from nemo.lens.fallbacks when lens is installed, otherwise provides inline no-ops.
See lens: optional dependency.
Naming conventions
Use the constants in nemo.lens.semconv when a name is shared across consumers (DL_RANK, NEMO_RUN_ID, etc.). For Megatron-specific names, hard-coded strings are fine — they’re short and grep-able.
Choosing a span group
When adding a new span, decide which group it belongs to:
- Always want it in production? →
job(very rare outside setup spans) - Once per iteration? →
step - Inside the forward/backward? →
forward_backwardormicrobatch - Inside the optimizer? →
optimizer - Related to checkpointing? →
checkpoint - Related to evaluation? →
evaluate - Cross-rank communication? →
communication - Inference request path? →
inference
Don’t invent a new group unless no existing group fits — new groups add to MegatronSpanGroup and require preset updates.
Adding a new span group
If you do need a new group:
-
Edit
megatron/core/telemetry/span_groups.py: -
Document it in Span Groups with the spans it controls and typical frequency.
-
Update dashboard queries if the group introduces new metric labels.
Adding a metric
For domain-specific metrics, add a module under megatron/core/telemetry/ following the pattern in megatron/core/telemetry/training_metrics.py:
Call record_training_metrics(meter=handle.meter, my_new_value_ms=42.0) only on the export rank (check handle.is_exporting).
See lens: metrics for the pattern rationale.
Testing new instrumentation
Megatron’s telemetry tests live at tests/unit_tests/telemetry/ and use the fixture pattern from lens’s conftest.py (global OTel state reset before/after each test).
When adding a span:
- Add a test in
tests/unit_tests/telemetry/that asserts the span is emitted when its group is enabled and absent when disabled. - Use
InMemorySpanExporter(from lens’sconftest.py, shared viasys.pathor a test utility) to capture spans. - Assert on span name, attributes, and parent relationships.
See lens: testing for fixture patterns.
When not to add instrumentation
- Inside a tight inner loop (per-token, per-parameter). Even
managed_span’s frozenset lookup adds up over trillions of invocations. - On code that runs on all ranks with unbounded cardinality. If the span attribute includes something like a tensor shape with high variance, you get cardinality explosion at the backend.
- As a replacement for logging. Structured logs belong in logs (and can be correlated via the log bridge). Spans describe bounded operations, not every interesting event.
When in doubt, start with a coarse span at the boundary of the subsystem, not a fine-grained one at every internal call.