Skip to content

perf(telemetry): install the FastAPI and aiohttp instrumentors only when traces are exported #4478

Closed

Description

@ananthsub

Problem

With telemetry metrics on and traces off, every Gym server still installs the OpenTelemetry FastAPI instrumentor, and every process still patches aiohttp through instrument_aiohttp.
Both add per-request cost that buys nothing when no span is exported.

Measured on one workstation with tracing off:

Instrumentation Cost
FastAPI instrumentor, per inbound request about 63 µs (15 µs bare, 78 µs instrumented)
aiohttp client instrumentation, per outbound request about 58 µs

A server-to-server hop pays both, about 120 µs.
In the capacity simulator (#3156, fast_inference_burst profile, near-free inference, three interleaved runs per setting on a quiet workstation), turning metrics on with traces off raised the servers' combined CPU per rollout from 22.2 to 22.7 ms, about 2 percent.
An earlier comparison on a heavily loaded machine showed about 17 percent, so the cost depends on contention, and the per-request figures above are the more reliable measure.
The event-loop and process metrics from #4474 add nothing measurable on top.
The gain from this change is therefore modest but grows with request rate, since it is a fixed cost per inbound and outbound request.

Proposal

Install the FastAPI instrumentor and the aiohttp client instrumentation only when traces are exported.

The FastAPI instrumentor is also the source of the http.server.* metrics, which Gym uses instead of the undimensioned nemo-lens request histogram.
With the pinned instrumentation package, a run exports the older semantic-convention names http.server.duration, http.server.request.size, and http.server.response.size, while metrics.mdx documents http.server.request.duration.
Dropping it with traces off would remove those metrics, so a metrics-only run needs a replacement:

A histogram record costs about 1.7 µs, so the replacement keeps the documented metrics at a small fraction of the instrumentor's cost.

Outbound trace context does not depend on the aiohttp instrumentation: server_utils.request injects traceparent itself when instrument_aiohttp is false.

Validation

  • Unit tests: with traces off and metrics on, no instrumentor is installed and the request-duration histogram is still exported with its attributes; with traces on, the instrumentors are installed as today.
  • Simulator: compare fast_inference_burst CPU per rollout with metrics off, metrics on before the change, and metrics on after it, and check the exported instruments with the telemetry_e2e profile.

Related: #3155, #2890.

Activity

  1. added
    area:core
    on Oct 12, 2026
  2. added 7 commits that reference this issue on Oct 12, 2026
    f65553d
    6312e47
    172ce67
    8df1e5c
    b2e7e59
    f81276f
    53eae81
  3. added a commit that references this issue on Oct 14, 2026
    98de01c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:core

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions