--enable-trace and configure the OpenTelemetry Collector endpoint using --otlp-traces-endpoint when launching the server.
You can find example screenshots of the visualization in https://github.com/sgl-project/sglang/issues/8965.
Setup Guide
This section explains how to configure the request tracing and export the trace data.-
Install the required packages and tools
- install Docker and Docker Compose
- install the dependencies
Command -
Launch OpenTelemetry collector and Jaeger
Command
-
Start your SGLang server with tracing enabled
ReplaceCommand
0.0.0.0:4317with the actual endpoint of the OpenTelemetry collector. If you launched the openTelemetry collector with tracing_compose.yaml, the default receiving port is 4317. To use the HTTP/protobuf span exporter, set the following environment variable and point to an HTTP endpoint, for example,http://0.0.0.0:4318/v1/traces.Command - Raise some requests
-
Observe whether trace data is being exported
- Access port 16686 of Jaeger using a web browser to visualize the request traces.
- The OpenTelemetry Collector also exports trace data in JSON format to /tmp/otel_trace.json. In a follow-up patch, we will provide a tool to convert this data into a Perfetto-compatible format, enabling visualization of requests in the Perfetto UI.
-
Dynamically adjust trace level
The trace level accepts configurable values from
0to3. The meanings of different trace level values are as follows:The trace level can be dynamically set via HTTP API, for example:ReplaceCommand0.0.0.0:30000with your actual server address, and replacelevel=2with the level you want to set. Note: You must set the parameter--enable-trace; otherwise, the trace capability will not be enabled regardless of any dynamic adjustments to the trace level.
How to add Tracing for slices you’re interested in?(API introduction)
We have already inserted instrumentation points in the tokenizer and scheduler main threads. If you wish to trace additional request execution segments or perform finer-grained tracing, please use the APIs from the tracing package as described below. All of the following implementations are done in python/sglang/srt/observability/req_time_stats.py. If you want to add another slice, please do it here.-
Initialization
Every process involved in tracing during the initialization phase should execute:
The otlp_traces_endpoint is obtained from the arguments, and you can set server_name freely, but it should remain consistent across all processes. Every thread involved in tracing during the initialization phase should execute:ExampleThe “thread label” can be regarded as the name of the thread, used to distinguish different threads in the visualization view.Example
-
Create a trace context for a request
Each request needs to call
TraceReqContext()to initialize a request context, which is used to generate slice spans and record request stage info. You can either store it within the request object or maintain it as a global variable. -
Mark the beginning and end of a request
trace_req_start() and trace_req_finish() must be called within the same process, for example, in the tokenizer.
-
Add tracing for a slice
-
Add slice tracing normally:
Example
- The end of the last slice in a thread must be marked with thread_finish_flag=True, or explicitly call trace_ctx.abort(); otherwise, the thread’s span will not be properly generated.
Example
-
Add slice tracing normally:
-
When the request execution flow transfers to another thread, the thread context needs to be explicitly rebuilt.
- receiver: Execute the following code after receiving the request via ZMQ
Example
- receiver: Execute the following code after receiving the request via ZMQ
How to Extend the Tracing Framework to Support Complex Tracing Scenarios
The currently provided tracing package still has potential for further development. If you wish to build more advanced features upon it, you must first understand its existing design principles. The core of the tracing framework’s implementation lies in the design of the span structure and the trace context. To aggregate scattered slices and enable concurrent tracking of multiple requests, we have designed a three-level trace context structure or span structure:TraceReqContext, TraceThreadContext and TraceSliceContext. Their relationship is as follows:
TraceReqContext and creates a corresponding request span. For every thread that processes the request, a TraceThreadContext is recorded and a thread span is created. The TraceThreadContext is nested within the TraceReqContext, and each currently traced code slice—potentially nested—is stored in its associated TraceThreadContext.
In addition to the above hierarchy, each slice also records its previous slice via Span.add_link(), which can be used to trace the execution flow.