Repository navigation
Description
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 pure ASGI middleware that records
http.server.request.duration, the namemetrics.mdxalready documents, withhttp.route,http.request.method, andhttp.response.status_code, one histogram record per request. gym.server.requests.in_flightfrom feat(telemetry): report event-loop, process, request, driver, and resources-operation metrics #4474 already covers active requests as a plain counter read by the exporter thread.
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_burstCPU per rollout with metrics off, metrics on before the change, and metrics on after it, and check the exported instruments with thetelemetry_e2eprofile.
Activity
perf(telemetry): skip span-only instrumentation while traces are off
perf(telemetry): skip span-only instrumentation while traces are off
perf(telemetry): skip span-only instrumentation while traces are off
perf(telemetry): skip span-only instrumentation while traces are off
perf(telemetry): skip span-only instrumentation while traces are off
perf(telemetry): skip span-only instrumentation while traces are off
feat(telemetry): report event-loop, process, request, driver, and res…
perf(telemetry): drop instrumentation that exports nothing (NVIDIA-Ne…