diff --git a/fastdeploy/entrypoints/openai/api_server.py b/fastdeploy/entrypoints/openai/api_server.py index ca63e586cc..492f308268 100644 --- a/fastdeploy/entrypoints/openai/api_server.py +++ b/fastdeploy/entrypoints/openai/api_server.py @@ -84,7 +84,7 @@ from fastdeploy.utils import ( retrive_model_from_server, ) -tracing.process_tracing_init() +_tracing_inited = False parser = make_arg_parser(FlexibleArgumentParser()) args = parser.parse_args() @@ -171,8 +171,15 @@ async def lifespan(app: FastAPI): async context manager for FastAPI lifespan """ global engine_args + global _tracing_inited import logging + # Initialize tracing in worker lifecycle instead of module import time. + # This avoids creating grpc/cygrpc state before gunicorn forks workers. + if not _tracing_inited: + tracing.process_tracing_init() + _tracing_inited = True + uvicorn_access = logging.getLogger("uvicorn.access") uvicorn_access.handlers.clear() diff --git a/fastdeploy/metrics/trace.py b/fastdeploy/metrics/trace.py index a7727ff380..612866b837 100644 --- a/fastdeploy/metrics/trace.py +++ b/fastdeploy/metrics/trace.py @@ -289,7 +289,7 @@ def get_otlp_span_exporter(endpoint, headers): OTLPSpanExporter as HTTPSpanExporter, ) - protocol = os.environ.get(OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, "grpc") + protocol = os.environ.get(OTEL_EXPORTER_OTLP_TRACES_PROTOCOL, "http/protobuf") supported_protocols = {"grpc", "http/protobuf"} if protocol not in supported_protocols: diff --git a/tests/metrics/test_trace.py b/tests/metrics/test_trace.py index eaa06ecf44..0601682fc6 100644 --- a/tests/metrics/test_trace.py +++ b/tests/metrics/test_trace.py @@ -908,6 +908,8 @@ class TestAdditionalCoverage: def test_get_otlp_span_exporter_grpc(self): """Test get_otlp_span_exporter with grpc protocol""" + # Set environment variable for grpc protocol + os.environ["OTEL_EXPORTER_OTLP_TRACES_PROTOCOL"] = "grpc" exporter = trace.get_otlp_span_exporter("http://localhost:4317", None) from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import ( OTLPSpanExporter as GRPCSpanExporter,