Repository navigation
fix(telemetry): adopt nemo-lens SpanRegistry and reuse an existing Lens setup - #3890
Merged
ananthsub merged 2 commits intoOct 1, 2026
Merged
Conversation
…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]>
Contributor
Contributor
|
LGTM, but please verify all spans appear with an eval run if not already covered in CI checks, thanks |
Signed-off-by: Ananth Subramaniam <[email protected]>
oyilmaz-nvidia
approved these changes
Oct 1, 2026
kajalj22
approved these changes
Oct 1, 2026
Contributor
|
🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/36928115462 |
This was referenced Oct 12, 2026
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:SpanGroupbase class withSpanRegistryand droppedspan_group_cls. Gym'sGymSpanGroupsubclassednemo.lens.groups.SpanGroup.setup_telemetryper process, and NeMo-RL reaches it before Gym does.This PR:
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. Inuv.lock, nemo-lens is the only package that changes.SpanRegistrywhennemo_gym.telemetry.span_groupsis imported. This follows the same pattern as NeMo-RL'sRLSpanGroup.GymSpanGroupis 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_callandsandbox.init_telemetrywraps the existing providers in a handle instead of callingsetup_telemetryagain.shutdown_telemetrydoes not shut down providers that Gym does not own. The check for an existing setup reads Lens's privatehandle._INITIALIZEDflag, which is the only available signal.export_strategyandexport_rankfromTelemetryConfig, because Lens removed rank-based export filtering and therank/world_sizearguments. Each Gym server now reportsnv.dl.rank=0andnv.dl.world_size=1.Compatibility
export_strategyorexport_rankkeep loading, becauseTelemetryConfigallows extra keys. The fields have no effect.defaultselects both NeMo-RL's and Gym's default groups.jobandrolloutare shared group names, so enabling either name enables both libraries' spans.telemetry:block is ignored in that process.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.pytest tests/unit_tests -n 8: 6309 passed, 13 failed. The same 13 tests (test_opensandbox_compose.py,test_sandbox_stop_retry.py, onetest_slurm_script.pycase) also fail on unmodifiedmainin this environment, which does not have the sandbox extras installed.pre-commit run --all-files: clean.export_strategy/export_rankloads and exports.Rollout evidence
I ran
gym eval runonexample_single_tool_callwith the simple agent andgpt-oss-20b(3 tasks), once from this branch and once frommain. Both runs had every span group on (span_groups=all) and used console export.main(nemo-lensb85578fc)b0f977d4)gym.rollout,gym.agent.responses,gym.model.responsesandgym.verify. TheHTTP POSTclient spans and the FastAPI server spans are present on all four servers.main. That adds one/get_weathercall, one model call and two agentHTTP POSTspans.nv.dl.rank=0andnv.dl.world_size=1as resource attributes. All other resource attributes are unchanged.sandboxspans, because this environment has no sandbox.gym.job, because thegym eval runprocess exports no spans onmaineither.NEMO_LENS_ENABLED=1.