Skip to navigation

nemo_gym.server_runtime.request_handling.trace_context

View as Markdown

Middleware that reads trace context carried in a request’s URL path.

Module Contents

Classes

NameDescription
TraceContextPathMiddlewareStrip /ng-trace/<traceparent> from the path and supply it as the inbound traceparent.

Functions

NameDescription
_restore_trace_segment_on_redirectPut /ng-trace/<traceparent> back into root-relative and same-origin redirect targets.
install_trace_context_pathInstall TraceContextPathMiddleware outside the instrumentor, which reads headers and path when it

Data

_ROLLOUT_PREFIX_PATTERN

_ROLLOUT_PREFIX_RE

_TRACE_CONTEXT_PATH_RE

API

class nemo_gym.server_runtime.request_handling.trace_context.TraceContextPathMiddleware(
app: starlette.types.ASGIApp
)

Strip /ng-trace/<traceparent> from the path and supply it as the inbound traceparent.

The segment comes from apply_rollout_prefix: a model URL an agent hands to a client that sends no traceparent header, such as a harness inside a sandbox. A header the client did send wins. The segment is stripped whether or not traces are on, so such a URL always routes.

nemo_gym.server_runtime.request_handling.trace_context.TraceContextPathMiddleware.__call__(
scope: starlette.types.Scope,
receive: starlette.types.Receive,
send: starlette.types.Send
) -> None
async
nemo_gym.server_runtime.request_handling.trace_context._restore_trace_segment_on_redirect(
message: starlette.types.Message,
segment: str,
request_headers: list[tuple[bytes, bytes]]
) -> starlette.types.Message

Put /ng-trace/<traceparent> back into root-relative and same-origin redirect targets.

Inserted after a rollout or token-capture prefix (which the capture middleware has already restored) or at the root; cross-origin targets and targets that already carry a segment are left alone.

nemo_gym.server_runtime.request_handling.trace_context.install_trace_context_path(
app: fastapi.FastAPI
) -> None

Install TraceContextPathMiddleware outside the instrumentor, which reads headers and path when it opens the server span. Same mechanism as install_request_context_isolation; call it after instrument_app_for_telemetry and before install_request_context_isolation, which stays outermost.

nemo_gym.server_runtime.request_handling.trace_context._ROLLOUT_PREFIX_PATTERN = f'/{re.escape(ROLLOUT_PATH_PREFIX)}/[^/]+(?:/{re.escape(TOKEN_CAPTURE_PATH_SEGME...
nemo_gym.server_runtime.request_handling.trace_context._ROLLOUT_PREFIX_RE = re.compile(f'^{_ROLLOUT_PREFIX_PATTERN}(?=/|$)')
nemo_gym.server_runtime.request_handling.trace_context._TRACE_CONTEXT_PATH_RE = re.compile(f'^(?P<head>(?:{_ROLLOUT_PREFIX_PATTERN})?)/{re.escape(TRACE_CONTEXT_...