From 5ded5e272911bdb99ae3087b466b5b9a3c504764 Mon Sep 17 00:00:00 2001 From: zhanghaotong <16302010009@fudan.edu.cn> Date: Wed, 12 Nov 2025 13:16:43 +0800 Subject: [PATCH] [Feature] Trace: Support http/protobuf span exporter protocol (#12396) Signed-off-by: zhanghaotong --- docs/references/production_request_trace.md | 6 +++++ python/sglang/srt/tracing/trace.py | 28 +++++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/references/production_request_trace.md b/docs/references/production_request_trace.md index 2450ccfda..93950503e 100644 --- a/docs/references/production_request_trace.md +++ b/docs/references/production_request_trace.md @@ -33,6 +33,12 @@ This section explains how to configure the request tracing and export the trace Replace `0.0.0.0:4317` with 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`. + ```bash + export OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=http/protobuf + ``` + + 4. raise some requests 5. Observe whether trace data is being exported * Access port 16686 of Jaeger using a web browser to visualize the request traces. diff --git a/python/sglang/srt/tracing/trace.py b/python/sglang/srt/tracing/trace.py index ffc3b1326..e3a12dca7 100644 --- a/python/sglang/srt/tracing/trace.py +++ b/python/sglang/srt/tracing/trace.py @@ -37,7 +37,15 @@ tracing_enabled = False try: from opentelemetry import context, propagate, trace - from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter + from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( + OTLPSpanExporter as GRPCSpanExporter, + ) + from opentelemetry.exporter.otlp.proto.http.trace_exporter import ( + OTLPSpanExporter as HTTPSpanExporter, + ) + from opentelemetry.sdk.environment_variables import ( + OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, + ) from opentelemetry.sdk.resources import SERVICE_NAME, Resource from opentelemetry.sdk.trace import TracerProvider, id_generator from opentelemetry.sdk.trace.export import BatchSpanProcessor @@ -207,7 +215,7 @@ def process_tracing_init(otlp_endpoint, server_name): ) processor = BatchSpanProcessor( - OTLPSpanExporter(endpoint=otlp_endpoint, insecure=True), + span_exporter=get_otlp_span_exporter(otlp_endpoint), schedule_delay_millis=schedule_delay_millis, max_export_batch_size=max_export_batch_size, ) @@ -225,6 +233,22 @@ def process_tracing_init(otlp_endpoint, server_name): tracing_enabled = True +def get_otlp_span_exporter(endpoint): + protocol = os.environ.get(OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, "grpc") + supported_protocols = {"grpc", "http/protobuf"} + + if protocol not in supported_protocols: + raise ValueError( + f"Unsupported OTLP protocol '{protocol}' configured. " + f"Supported protocols are: {', '.join(sorted(supported_protocols))}" + ) + + if protocol == "grpc": + return GRPCSpanExporter(endpoint=endpoint, insecure=True) + elif protocol == "http/protobuf": + return HTTPSpanExporter(endpoint=endpoint) + + # Should be called by each tracked thread. def trace_set_thread_info( thread_label: str, tp_rank: Optional[int] = None, dp_rank: Optional[int] = None