feat: support custom OTLP trace service name (#35802)

This commit is contained in:
ymren
2026-09-16 22:37:42 +08:00
committed by GitHub
parent 00a9a81b67
commit cc171fbad0
10 changed files with 74 additions and 8 deletions
@@ -175,6 +175,48 @@ class TestProcessTracingInit(unittest.TestCase):
finally:
mod.opentelemetry_imported = orig
@unittest.skipIf(not _has_otel, "OpenTelemetry not installed")
def test_service_name_priority_explicit(self):
"""Service name priority: explicit parameter > env var > default."""
with patch.dict(os.environ, {"OTEL_SERVICE_NAME": "from-env"}):
with patch.object(mod, "TracerProvider"):
with patch.object(mod, "Resource") as mock_resource:
process_tracing_init("localhost:4317", "explicit-name")
# Verify Resource.create was called with the explicit name
mock_resource.create.assert_called_once()
call_kwargs = mock_resource.create.call_args[1]
self.assertEqual(
call_kwargs["attributes"][mod.SERVICE_NAME], "explicit-name"
)
@unittest.skipIf(not _has_otel, "OpenTelemetry not installed")
def test_service_name_priority_env_var(self):
"""Service name falls back to OTEL_SERVICE_NAME when explicit is None."""
with patch.dict(os.environ, {"OTEL_SERVICE_NAME": "from-env"}):
with patch.object(mod, "TracerProvider"):
with patch.object(mod, "Resource") as mock_resource:
process_tracing_init("localhost:4317", None)
# Verify Resource.create was called with env var value
mock_resource.create.assert_called_once()
call_kwargs = mock_resource.create.call_args[1]
self.assertEqual(
call_kwargs["attributes"][mod.SERVICE_NAME], "from-env"
)
@unittest.skipIf(not _has_otel, "OpenTelemetry not installed")
def test_service_name_priority_default(self):
"""Service name falls back to 'sglang' when both explicit and env are unset."""
with patch.dict(os.environ, {}, clear=True):
with patch.object(mod, "TracerProvider"):
with patch.object(mod, "Resource") as mock_resource:
process_tracing_init("localhost:4317", None)
# Verify Resource.create was called with default value
mock_resource.create.assert_called_once()
call_kwargs = mock_resource.create.call_args[1]
self.assertEqual(
call_kwargs["attributes"][mod.SERVICE_NAME], "sglang"
)
class TestTraceReqContextDisabled(unittest.TestCase):
def setUp(self):