feat(trace): support SGLANG_TRACE_LEVEL env var for startup trace level (#24716)

Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Tianhe
2026-05-11 10:31:03 -07:00
committed by GitHub
co-authored by Claude Sonnet 4.6
parent 2e69266f84
commit 95985f983d
3 changed files with 17 additions and 3 deletions
+7 -2
View File
@@ -52,9 +52,14 @@ This section explains how to configure the request tracing and export the trace
0: disable tracing
1: Trace important slices
2: Trace all slices except nested ones
3: Trace all slices
3: Trace all slices (default)
```
The trace level can be dynamically set via HTTP API, for example:
**At startup** — set `SGLANG_TRACE_LEVEL` before launching the server:
```bash
SGLANG_TRACE_LEVEL=2 python -m sglang.launch_server --enable-trace --otlp-traces-endpoint 0.0.0.0:4317 <other options>
```
**At runtime** — dynamically adjust via HTTP API without restarting:
```bash
curl http://0.0.0.0:30000/set_trace_level?level=2
```
+1 -1
View File
@@ -32,7 +32,7 @@ opentelemetry_initialized = False
_trace_context_propagator = None
tracer: Optional[trace.Tracer] = None
global_trace_level = 3
global_trace_level = get_int_env_var("SGLANG_TRACE_LEVEL", 3)
TRACE_HEADERS = ["traceparent", "tracestate"]
@@ -56,6 +56,15 @@ class TestTraceFunctions(unittest.TestCase):
self.assertEqual(mod.global_trace_level, 5)
mod.global_trace_level = orig
def test_global_trace_level_env_var(self):
import importlib
with patch.dict(os.environ, {"SGLANG_TRACE_LEVEL": "2"}):
importlib.reload(mod)
self.assertEqual(mod.global_trace_level, 2)
importlib.reload(mod) # restore default (SGLANG_TRACE_LEVEL unset → 3)
self.assertEqual(mod.global_trace_level, 3)
def test_get_global_tracing_enabled(self):
self.assertEqual(get_global_tracing_enabled(), mod.opentelemetry_initialized)