Skip to content

fix(telemetry): adopt nemo-lens SpanRegistry and reuse an existing Lens setup - #3890

Merged
ananthsub merged 2 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ansubramania/nemo-lens-span-registry
Oct 1, 2026
Merged

ananthsub merged 2 commits into
NVIDIA-NeMo:mainfrom
ananthsub:ansubramania/nemo-lens-span-registry

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

What changed and why

nemo-lens made several breaking API changes after the commit Gym pinned (b85578fc), and NeMo-RL main already pins the newer Lens (b0f977d4). That breaks Gym telemetry when Gym runs inside a NeMo-RL job:

  1. Gym fails to import with the newer Lens. Lens replaced the SpanGroup base class with SpanRegistry and dropped span_group_cls. Gym's GymSpanGroup subclassed nemo.lens.groups.SpanGroup.
  2. Gym's telemetry setup raises when NeMo-RL initialised Lens first. Lens allows one setup_telemetry per process, and NeMo-RL reaches it before Gym does.

This PR:

  • Pins nemo-lens to b0f977d4, the commit NeMo-RL pins. An RL job installs Gym into its environment and resolves one nemo-lens, so the two pins need to agree. In uv.lock, nemo-lens is the only package that changes.
  • Registers Gym's span groups with SpanRegistry when nemo_gym.telemetry.span_groups is imported. This follows the same pattern as NeMo-RL's RLSpanGroup. GymSpanGroup is now a plain class of string constants, so call sites are unchanged. Gym registers only the groups it emits: job, server, http_client, rollout, verify, agent, model_call and sandbox.
  • Reuses a Lens setup that another library already created. init_telemetry wraps the existing providers in a handle instead of calling setup_telemetry again. shutdown_telemetry does not shut down providers that Gym does not own. The check for an existing setup reads Lens's private handle._INITIALIZED flag, which is the only available signal.
  • Removes export_strategy and export_rank from TelemetryConfig, because Lens removed rank-based export filtering and the rank/world_size arguments. Each Gym server now reports nv.dl.rank=0 and nv.dl.world_size=1.

Compatibility

  • Old configs: configs that still set export_strategy or export_rank keep loading, because TelemetryConfig allows extra keys. The fields have no effect.
  • Unknown span-group names: an unknown name now logs one warning per process and is ignored, instead of raising. This is Lens's new behavior. The warning names the bad entry and lists the registered groups. It is evaluated only at setup and when a library registers groups, and repeats are suppressed, so it never fires on the span hot path.
  • Running inside NeMo-RL:
    • Presets combine across libraries, so default selects both NeMo-RL's and Gym's default groups.
    • job and rollout are shared group names, so enabling either name enables both libraries' spans.
    • When Gym reuses NeMo-RL's setup, NeMo-RL's span-group setting, service name and exporter apply, and Gym's telemetry: block is ignored in that process.
  • Removed Gym-only groups: the training-oriented groups Gym inherited from Lens (checkpoint, step, optimizer, and so on) are no longer registered by Gym. No Gym code emitted under them.

No issue was filed. NeMo-RL users reported the problem directly.

Validation

  • pytest tests/unit_tests/telemetry: 195 passed.
    • New tests cover reusing an existing Lens setup, keeping Gym's groups enabled under the owner's setting, not shutting down borrowed providers, and the rank resource attributes.
    • With the reuse branch disabled, the reuse and shutdown tests fail.
  • pytest tests/unit_tests -n 8: 6309 passed, 13 failed. The same 13 tests (test_opensandbox_compose.py, test_sandbox_stop_retry.py, one test_slurm_script.py case) also fail on unmodified main in this environment, which does not have the sandbox extras installed.
  • pre-commit run --all-files: clean.
  • Manual check:
    • A config with legacy export_strategy/export_rank loads and exports.
    • A span-group setting containing a typo logs exactly one warning per process, even after repeated setup and 50 re-registrations.

Rollout evidence

I ran gym eval run on example_single_tool_call with the simple agent and gpt-oss-20b (3 tasks), once from this branch and once from main. Both runs had every span group on (span_groups=all) and used console export.

main (nemo-lens b85578fc) This PR (nemo-lens b0f977d4)
Span types (name, kind, process) 49 49, identical set
Traces 8 8
Traces that cross processes 4 4
Most processes in one trace 4 4
  • All of Gym's spans are present: gym.rollout, gym.agent.responses, gym.model.responses and gym.verify. The HTTP POST client spans and the FastAPI server spans are present on all four servers.
  • Span counts differ only because the model took one extra tool-call turn on main. That adds one /get_weather call, one model call and two agent HTTP POST spans.
  • Each server now also reports nv.dl.rank=0 and nv.dl.world_size=1 as resource attributes. All other resource attributes are unchanged.
  • Not covered by this run:
    • sandbox spans, because this environment has no sandbox.
    • gym.job, because the gym eval run process exports no spans on main either.
    • Running inside a NeMo-RL process, which still needs an RL job with NEMO_LENS_ENABLED=1.

…ns setup

nemo-lens replaced the SpanGroup base class with SpanRegistry and removed the
span_group_cls, rank, world_size, and export-strategy APIs. NeMo-RL already
pins the new Lens, so Gym's import of nemo.lens.groups.SpanGroup failed in RL
environments. Lens also allows one setup_telemetry per process, so Gym raised
when NeMo-RL had already initialised it.

- Pin nemo-lens to b0f977d4, the commit NeMo-RL pins.
- Register Gym's span groups and presets with SpanRegistry at import.
- Reuse providers another library already set up, and leave their shutdown
  to that library.
- Drop export_strategy / export_rank; each server reports rank 0 of 1.

Signed-off-by: Ananth Subramaniam <[email protected]>
@ananthsub
ananthsub requested a review from a team as a code owner October 1, 2026 17:30
@ananthsub ananthsub added area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working needs-review PR is ready for code review and waiting on a reviewer labels Oct 1, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 1, 2026 17:36
@github-actions

github-actions Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Comment thread nemo_gym/telemetry/setup.py Resolved
@rrs45

rrs45 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

LGTM, but please verify all spans appear with an eval run if not already covered in CI checks, thanks

oyilmaz-nvidia
oyilmaz-nvidia previously approved these changes Oct 1, 2026

@oyilmaz-nvidia oyilmaz-nvidia left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@yaoyu-33 yaoyu-33 added the complexity:medium Single-domain change with interacting parts or a moderate review surface label Oct 1, 2026
@ananthsub
ananthsub added this pull request to the merge queue Oct 1, 2026
@github-actions

github-actions Bot commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/36928115462

Merged via the queue into NVIDIA-NeMo:main with commit 062a9db Oct 1, 2026
43 checks passed
@ananthsub
ananthsub deleted the ansubramania/nemo-lens-span-registry branch October 1, 2026 21:35
ananthsub added a commit that referenced this pull request Oct 14, 2026
> **Stack:** telemetry lands before the optimizations, so the
optimizations can be measured with it.
> 1. #4474: event-loop, process, request, driver, and
resources-operation metrics (merged)
> 2. #4481: drop instrumentation that exports nothing (base `main`)
> 3. #2890: replace the HTTP middleware wrappers with ASGI (base #4481)

## What changed

With `traces_enabled: false`, nemo-lens still enables the configured
span groups, so instrumentation whose only output is spans kept running
on every request against a tracer that records nothing.
This PR skips that work while traces are off:

- `init_telemetry` records whether the process exports traces, and
`nemo_gym.telemetry.setup.is_trace_exporter_active()` reports it. When
Gym reuses providers another library set up, it checks for a real SDK
tracer provider, as it already does for metrics.
- `SimpleServer.instrument_app_for_telemetry` installs the FastAPI
instrumentor only while traces export.
- `server_utils.request` opens the outbound CLIENT span and injects
`traceparent` only while traces export.
- The `telemetry.instrument_aiohttp` setting and its
`NEMO_GYM_OTEL_INSTRUMENT_AIOHTTP` variable are removed. Nothing in Gym
or nemo-lens calls
`nemo.lens.contrib.aiohttp.instrument_aiohttp_client`, so the setting
never did anything; its docstring described a CLIENT span that Gym in
fact creates itself in `server_utils.request`.
- `opentelemetry-instrumentation-aiohttp-client`, which only backs that
unused helper, is dropped from the `telemetry` extra and from the
packages installed into each server venv.

Span groups that also drive metrics, such as `verify`, `rollout`, and
`sandbox`, are unchanged, so `gym.verify.*`, `gym.rollout.*`, and the
sandbox metrics still report in a metrics-only run.

The FastAPI instrumentor is also the source of the `http.server.*`
metrics.
A metrics-only run no longer reports them, and the docs now say so.
The docs also named metrics the pinned instrumentation does not export:
a run exports `http.server.duration` (ms), `http.server.request.size`,
`http.server.response.size`, and `http.server.active_requests`, keyed by
`http.target`, `http.method`, and `http.status_code`, while
`metrics.mdx`, `extending.mdx`, and the telemetry README listed
`http.server.request.duration`.

## Why

Measured on one workstation with tracing off, the FastAPI instrumentor
adds about 63 µs to each inbound request (15 µs bare, 78 µs
instrumented).
The outbound path adds a no-op span and a header injection to every call
between servers.
Neither produces anything a metrics-only run can use.

## Issue

Closes #4478.

The issue also listed aiohttp client instrumentation at about 58 µs per
outbound request.
Nothing in Gym or nemo-lens installs that instrumentor; the outbound
cost Gym pays comes from its own client span, which this PR gates, and
the setting that appeared to control the instrumentor is removed.
This PR absorbs #4485.

## Compatibility

- Runs with `traces_enabled: false` and metrics on lose the
`http.server.*` metrics. Turn traces on to keep them.
- Runs with traces on are unchanged.
- Configs that still set `instrument_aiohttp` keep loading, because
`TelemetryConfig` allows extra keys; the key has no effect, as before.
The setting is removed rather than deprecated, as `export_strategy` and
`export_rank` were in #3890.
- The `telemetry` extra no longer installs
`opentelemetry-instrumentation-aiohttp-client`. `daytona` still depends
on it, so it stays in `uv.lock` for the `sandbox` extra. Code that calls
`instrument_aiohttp_client` itself can install `nemo-lens[aiohttp]`.
- Builds on #4474, now merged. In `server_utils.request`, the trace gate
chooses between the traced and untraced send, and #4474's outbound
in-flight count wraps either one.

## Validation

- `pytest tests/unit_tests/telemetry
tests/unit_tests/test_server_utils.py tests/unit_tests/test_cli.py -n
12` with the `telemetry` extra installed.
- `pytest tests/unit_tests -n 16`: the only failures are the same
environment-dependent ones as on the base commit
(`test_opensandbox_compose.py`, `test_reward_profile.py`,
`test_dataset_orchestrator.py`, `test_sandbox_stop_retry.py`), and
`test_exporters.py` needs `mlflow`.
- `test_setup.py` covers the trace flag following `traces_enabled`, the
flag for reused providers with and without an SDK tracer provider, and
the FastAPI instrumentor installed only while traces export.
- `test_config.py` checks that a config setting `instrument_aiohttp`
still loads and that `configure_telemetry_env` no longer exports the
variable; it fails on the base commit.
- In a venv synced from this branch with the `telemetry` extra,
`opentelemetry.instrumentation.aiohttp_client` is not importable, and
the full unit suite runs with the same environment-dependent failures as
the base commit, including passing propagation tests that check
`traceparent` on outbound requests.
- `uv lock --check` passes.
- `test_propagation.py` serves two real FastAPI apps and checks that,
with traces off, no CLIENT span is produced and no `traceparent` reaches
the downstream server.
- Removing either gate makes its test fail.
- `pre-commit run` on the changed files.

- An interleaved comparison through the CPU capacity simulator from
#3156 (`fast_inference_burst`, near-instant inference so that Gym's own
cost dominates, 2,048 rollouts per run, three runs per setting, engine,
servers, and driver pinned to separate cores, traces off). Server CPU
per rollout, median with range:

| Setting | Server CPU per rollout | Rollouts per second |
| --- | ---: | ---: |
| Base commit, metrics on | 22.6 ms (22.6–22.7) | 100 (97–107) |
| This PR, metrics on | 21.0 ms (21.0–21.1) | 106 (102–109) |
| This PR, telemetry off | 19.7 ms (19.4–19.7) | 114 (110–114) |

The cost of turning metrics on drops from about 2.9 ms to 1.3 ms per
rollout.
Both metrics-on runs exported the same `gym.*` metrics, including
`gym.rollout.duration_ms` and the connection-pool metrics; only the
`http.server.*` metrics are gone. No run had a failed rollout.

## Rollout evidence

`example_single_tool_call` with the simple agent and `gpt-oss-20b` (3
tasks) ran from the dependency-removal commits and from the base commit
`75bb53e6d`, both with `span_groups=all`, traces on, and console export,
through `gym env start` and then `gym eval run --no-serve`. On the base
commit every server venv had
`opentelemetry-instrumentation-aiohttp-client`; with the change, none
did, and each still had `opentelemetry-instrumentation-fastapi`.

| | Base commit | Without the aiohttp package |
| --- | --- | --- |
| Spans | 238 | 238 |
| Span types (name, kind, process) | 49 | 49, identical set |
| `HTTP POST` client spans | 26 | 26 |
| Traces | 12 | 12 |
| Traces that cross processes | 4 | 4 |
| Server spans whose parent is a client span in another process | 18 |
18 |
| Rewards | 0, 1, 1 | 0, 1, 1 |

`traceparent` still joins each outbound request to the server span that
handles it. Sandbox spans are not covered, because this environment has
no sandbox.

The trace gate does not change agent, model, or verifier decisions; the
capacity-simulator runs above exercise it through real rollouts with a
mock inference engine.

---------

Signed-off-by: Ananth Subramaniam <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 1ae85671 Deployed Oct 1, 2026 by copy-pr-bot[bot] via release / finalize / notify #4127
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:core Shared APIs, servers, telemetry, health, and registries bug Something isn't working complexity:medium Single-domain change with interacting parts or a moderate review surface needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants