> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/gym/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/gym/_mcp/server.

# nemo_gym.telemetry.contrib

Gym's boundary onto nemo-lens's propagation and framework-instrumentation helpers.

`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`.

`_fallbacks` covers the primitives used at instrumentation sites. This module covers the
two helpers that are not primitives: W3C context injection at Gym's HTTP egress, and
FastAPI auto-instrumentation at each server's startup. Both are called only on paths that
already established telemetry is active, so neither needs a no-op twin — but both are
defensive anyway, because telemetry must never be the reason a server fails to start or a
request fails to send.

## Module Contents

### Functions

| Name                                                                           | Description                                                |
| ------------------------------------------------------------------------------ | ---------------------------------------------------------- |
| [`inject_trace_context`](#nemo_gym-telemetry-contrib-inject_trace_context)     | Write the current span's W3C `traceparent` into *headers*. |
| [`instrument_fastapi_app`](#nemo_gym-telemetry-contrib-instrument_fastapi_app) | Apply OpenTelemetry FastAPI auto-instrumentation to *app*. |

### Data

[`logger`](#nemo_gym-telemetry-contrib-logger)

### API

```python
nemo_gym.telemetry.contrib.inject_trace_context(
    headers: dict
) -> None
```

Write the current span's W3C `traceparent` into *headers*.

The egress half of cross-process propagation. A no-op when there is no recording span,
because the propagator has no context to write.

Call only behind a span-group gate: the import below is function-local so a disabled
site never reaches it.

```python
nemo_gym.telemetry.contrib.instrument_fastapi_app(
    app: typing.Any
) -> bool
```

Apply OpenTelemetry FastAPI auto-instrumentation to *app*.

The ingress half of cross-process propagation: it extracts an inbound `traceparent`
and parents this server's SERVER span to the caller's span.

`service_name` is deliberately not passed. `nemo.lens.contrib.fastapi`'s helper
accepts it and never uses it at the pinned commit; the name comes from
`OTEL_SERVICE_NAME`, which `init_telemetry` resolves.

**Returns:** `bool`

True when instrumentation was applied. False when the optional instrumentation

```python
nemo_gym.telemetry.contrib.logger = logging.getLogger(__name__)
```