Repository navigation
feat(telemetry): add optional OpenTelemetry instrumentation via nemo-lens - #2646
Conversation
…lens Adds `nemo_gym/telemetry/`, the base layer for optional OpenTelemetry instrumentation built on nemo-lens. No call sites yet; those follow in a second PR so this one is reviewable on its own. Telemetry activates only when the `telemetry` extra is installed and it is switched on via a `telemetry:` config block or `NEMO_GYM_OTEL_*` / `NEMO_LENS_*` environment variables. With either missing, every instrumentation site is a no-op and Gym behaves as it does today. The module: * `config.py` — `TelemetryConfig`, the `telemetry:` block. Imports pydantic only. * `setup.py` — lifecycle. `configure_telemetry_env` translates the config block into `NEMO_GYM_OTEL_*` variables in the orchestrator before any server is spawned; `init_telemetry` reads that environment back in each server process. Gym runs N independent FastAPI processes that share no memory, so the environment is the only channel between them. `export_strategy` defaults to `all_ranks` rather than `single_rank` because each Gym server is rank 0 of its own world of one. * `span_groups.py` — `GymSpanGroup` and the `default` / `per_rollout` / `all` presets. Groups live downstream in the consumer, as they do in NeMo-RL. * `metrics.py` — a wrapper over nemo-lens's `gym.*` instruments that takes an explicit position on each, including which are unsuitable and why. * `spans.py` — a CLIENT-kind span. `managed_span` cannot set `SpanKind`, and an INTERNAL span on a cross-service hop loses the client/server distinction a backend needs. * `_fallbacks.py` — the single import point for the instrumentation primitives. It resolves to the real `nemo.lens` implementations when the extra is installed and to no-op shims when it is not, so a call site needs one import rather than its own `try/except ImportError`. `telemetry` is registered as a reserved top-level config key so the config parser does not read the block as a server instance definition. The extra pins nemo-lens to the same commit Megatron-LM uses, via `[tool.uv.sources]`. That table governs dependencies resolved through the local project, which includes the per-server venvs that `gym env test` and `gym env start` build. Signed-off-by: Ahmad Kiswani <[email protected]>
| `nemo_gym.telemetry` is the only package that imports `nemo.lens`. Everything outside it | ||
| reaches telemetry through this package, so a reader never has to work out whether a given | ||
| `nemo.lens` import is reachable when the extra is not installed. The invariant is | ||
| enforced by `tests/unit_tests/telemetry/test_call_sites.py`. |
There was a problem hiding this comment.
NOTE — this docstring states the "only nemo_gym.telemetry imports nemo.lens" invariant "is enforced by tests/unit_tests/telemetry/test_call_sites.py." That test file is not in this PR (the added tests are config/endpoints/fallbacks/metrics/overhead/setup/span_groups). What breaks: the invariant is documented as test-enforced but is actually unenforced — when call sites land in the follow-up PR, a stray from nemo.lens import ... outside this package will import fine with the extra installed and only blow up in the lens-absent config nobody runs by accident. Fix: add the test, or soften the claim to "by convention" until it exists.
| pytest tests/unit_tests/telemetry/ -x | ||
| ``` | ||
|
|
||
| The load-bearing test is `test_propagation.py`: two Gym apps, an in-memory span exporter, |
There was a problem hiding this comment.
NOTE — README calls test_propagation.py "the load-bearing test" and instructs future editors to "make sure that test still fails when propagation breaks." That file is not in this PR. Since cross-process context propagation (one rollout → one trace) is the entire stated purpose of the module, the one test that would guard it is absent. Fine for a no-call-site base layer, but the doc promises a regression guard that doesn't exist yet — either land it with the call sites in PR2 or mark it as TODO here so nobody trusts a green suite to mean propagation works.
|
SHIP WITH CARE — clean opt-in base layer; the correctness-critical hazards this repo worries about don't apply here yet, but two claimed test guarantees are missing. I reviewed this as the engineer who'd get paged. Up front: this PR adds no call sites (author's explicit design), so none of the usual page-triggers are in play — no What to carry forward, none blocking for a no-op base layer:
Nothing here corrupts scores or hangs a run. Merge once the missing-test prose is reconciled with reality. |
| except ImportError: | ||
| # TODO(ahmadki): SpanGroups are moving from nemo-lens to downstream consumers, at | ||
| # which point this stub and the try/except disappear and GymSpanGroup stands alone. | ||
| class SpanGroup: # type: ignore[no-redef] |
There was a problem hiding this comment.
If this will be on the downstream consumers, let's remove the from the
nemo.lens.groups import SpanGroup
And add the SpanGroup class here.
| Reads the optional top-level ``telemetry:`` block. A run that never mentions | ||
| telemetry gets the all-defaults config, which is disabled. | ||
| """ | ||
| from nemo_gym.telemetry.config import TelemetryConfig |
There was a problem hiding this comment.
Can we move the imports to the top of the file if there is no specific reason to keep them here?
|
|
||
| # OmegaConf nodes are Mapping-like but not dicts; resolve them before validating. | ||
| try: | ||
| from omegaconf import DictConfig, OmegaConf |
| #: ``TelemetryConfig`` field -> env var. ``service_name`` is handled separately because it | ||
| #: maps to the standard unprefixed ``OTEL_SERVICE_NAME``, and the Gym-owned flags | ||
| #: (``service_name_per_server``, ``instrument_aiohttp``) are not lens config fields. | ||
| _ENV_FIELD_MAP = { |
There was a problem hiding this comment.
Does user have to set these env vars? Or, is there a function in the setup or anywhere else that sets these env vars for us? If not, having a function that connects the config or inputs to the env vars will be helpful.
| return attrs | ||
|
|
||
|
|
||
| def init_telemetry( |
There was a problem hiding this comment.
Currently, it doesn't seem that setting telemetry.enabled: true is calling these? Will you do it in the next PR?
| disabled. Never raises: a telemetry failure must not take a server down. | ||
| """ | ||
| global _TELEMETRY_HANDLE, _INITIALISED | ||
| if _INITIALISED: |
There was a problem hiding this comment.
Not sure if there will be a concurrent init calls but there is a an unguarded singleton race and concurrent init calls can both pass the guard and register duplicate OTel providers.
| { url = "https://files.pythonhosted.org/packages/9d/1a/c8923d819b49872a612033b90d29299c0be73a7cbed1ddb3dc78dfe5e9f1/apache_tvm_ffi-0.1.9-cp314-cp314t-win_amd64.whl", hash = "sha256:a42d7ca27dce83efbdce7ec970fe3e773a69c31d928730ee5d9badb1229d106c", size = 2039007, upload-time = "2026-02-27T19:27:43.618Z" }, | ||
| ] | ||
|
|
||
| [[package]] |
There was a problem hiding this comment.
uv.lock forces an opentelemetry-api version bump for all installs, not just ones opting into the telemetry extra
| return f"{requirement_name}=={dist.version}" | ||
|
|
||
|
|
||
| def server_venv_requirements() -> list: |
There was a problem hiding this comment.
This is not called anywhere and will this be called in the second PR?
|
/ok to test 2c85ae2 |
Signed-off-by: Onur Yilmaz <[email protected]>
…try extra - Move the TelemetryConfig and omegaconf imports in setup.py to module scope; neither requires nemo-lens, so the function-local-import convention (reserved for lens imports) didn't apply to them. - Document that _ENV_FIELD_MAP is consumed by configure_telemetry_env, which is the function that connects a telemetry: config block to env vars, so a user doesn't set them by hand. - Note in server_venv_requirements and init_telemetry's docstrings that neither has a caller yet in this PR; wiring lands with the follow-up call-sites PR. - Guard init_telemetry's check-and-set singleton pattern with a lock: concurrent callers could previously both observe _INITIALISED as False and both call setup_telemetry, which nemo-lens raises on for a second call in the same process. Add a concurrency test proving the lock actually serializes concurrent init calls. - Document in pyproject.toml why adding the telemetry extra bumps the shared opentelemetry-api floor for every install, not just ones requesting the extra (uv resolves one unified lockfile). Signed-off-by: Onur Yilmaz <[email protected]>
|
/ok to test e1da2cc |
|
/ok to test 30ea2b9 |
@oyilmaz-nvidia, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
|
/ok to test 74f9892 |
|
/ok to test 1d1a1b0 |
|
/ok to test 1d1a1b0 |
@oyilmaz-nvidia, there was an error processing your request: See the following link for more information: https://docs.gha-runners.nvidia.com/cpr/e/2/ |
|
/ok to test 89dd7af |
|
/ok to test 9bd29fe |
What does this PR do?
Adds
nemo_gym/telemetry/, the base layer for optional OpenTelemetry instrumentationbuilt on nemo-lens. No call sites yet — those
follow in a second PR, so this one is reviewable on its own and changes no runtime
behaviour.
Telemetry activates only when the
telemetryextra is installed and it is switched onthrough a
telemetry:config block orNEMO_GYM_OTEL_*/NEMO_LENS_*environmentvariables. With either missing, every instrumentation site added later is a no-op and Gym
behaves exactly as it does today.
Why the design looks the way it does
Gym's process model drove most of it. Megatron-LM is one process tree and NeMo-RL is a Ray
driver plus actors, but a Gym run is N independent FastAPI processes spawned with
Popenthat share no memory and inherit no handle. So the orchestrator translates the config block
into
NEMO_GYM_OTEL_*variables before spawning anything, and each server reads thatenvironment back on startup — the environment is the only channel between them.
For the same reason
export_strategydefaults toall_ranksrather thansingle_rank:each Gym server is rank 0 of its own world of one, so a rank filter would either silence
all of them or none.
What's in it
config.pyTelemetryConfig, thetelemetry:block. Imports pydantic only.setup.pyconfigure_telemetry_env(orchestrator),init_telemetry(per server process),get_telemetry,shutdown_telemetry.span_groups.pyGymSpanGroupand thedefault/per_rollout/allpresets. Groups live downstream in the consumer, as they do in NeMo-RL.metrics.pygym.*instruments, taking an explicit position on each — including which are unsuitable and why.spans.pymanaged_spancannot setSpanKind, and an INTERNAL span on a cross-service hop loses the client/server distinction a backend needs for service maps.contrib.py_fallbacks.py_fallbacks.pyresolves to the realnemo.lensimplementations when the extra isinstalled and to no-op shims when it is not, so a call site needs one import rather than
its own
try/except ImportErrorat every site. Together withcontrib.pythis makesnemo_gym.telemetrythe only package that importsnemo.lens, which is enforced by atest rather than by convention.
telemetryis registered as a reserved top-level config key so the config parser does notread the block as a server instance definition.
Dependency pin
The extra pins nemo-lens to the same commit Megatron-LM uses, via
[tool.uv.sources].Two things that table does not do, worth knowing before review:
pip install nemo-gym[telemetry]from PyPI resolves nemo-lens from PyPI instead. The pingoverns source checkouts and the uv lock.
the per-server venvs needs more than this, and is handled in the follow-up PR.
Checklist
pre-commit run --all-files) (so CI lint/format/copyright pass).git commit -s) (so the DCO check passes).