diff --git a/fastdeploy/entrypoints/openai/api_server.py b/fastdeploy/entrypoints/openai/api_server.py index 8bcc74a31..0f9467c54 100644 --- a/fastdeploy/entrypoints/openai/api_server.py +++ b/fastdeploy/entrypoints/openai/api_server.py @@ -45,7 +45,7 @@ from fastdeploy.metrics.metrics import ( get_filtered_metrics, main_process_metrics, ) -from fastdeploy.metrics.trace_util import inject_to_metadata, instrument +from fastdeploy.metrics.trace_util import fd_start_span, inject_to_metadata, instrument from fastdeploy.plugins.model_register import load_model_register_plugins from fastdeploy.utils import ( FlexibleArgumentParser, @@ -272,6 +272,7 @@ def launch_api_server() -> None: api_server_logger.info(f"launch Fastdeploy api server... port: {args.port}") api_server_logger.info(f"args: {args.__dict__}") + fd_start_span("FD_START") try: uvicorn.run( diff --git a/fastdeploy/envs.py b/fastdeploy/envs.py index 3da7af75c..6d17ac005 100644 --- a/fastdeploy/envs.py +++ b/fastdeploy/envs.py @@ -82,6 +82,8 @@ environment_variables: dict[str, Callable[[], Any]] = { "ENABLE_V1_KVCACHE_SCHEDULER": lambda: int(os.getenv("ENABLE_V1_KVCACHE_SCHEDULER", "0")), # Whether to use PLUGINS. "FD_PLUGINS": lambda: None if "FD_PLUGINS" not in os.environ else os.environ["FD_PLUGINS"].split(","), + # set trace attribute job_id. + "FD_JOB_ID": lambda: os.getenv("FD_JOB_ID"), } diff --git a/fastdeploy/metrics/trace_util.py b/fastdeploy/metrics/trace_util.py index e51446e77..8b391dd66 100644 --- a/fastdeploy/metrics/trace_util.py +++ b/fastdeploy/metrics/trace_util.py @@ -1,4 +1,5 @@ import json +import os from fastapi import FastAPI from opentelemetry import trace @@ -176,7 +177,22 @@ def start_span(span_name, request, kind=trace.SpanKind.CLIENT): return # extract Trace context from request.metadata.trace_carrier ctx = extract_from_metadata(request) - with tracer.start_as_current_span(span_name, context=ctx, kind=kind): + with tracer.start_as_current_span(span_name, context=ctx, kind=kind) as span: + span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null")) + pass + except: + pass + + +def fd_start_span(span_name, kind=trace.SpanKind.CLIENT): + """ + when fd start, start a new span show start success + """ + try: + if not traces_enable: + return + with tracer.start_as_current_span(span_name, kind=kind) as span: + span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null")) pass except: pass @@ -191,7 +207,8 @@ def start_span_request(span_name, request, kind=trace.SpanKind.CLIENT): return # extract Trace context from request.metadata.trace_carrier ctx = extract_from_request(request) - with tracer.start_as_current_span(span_name, context=ctx, kind=kind): + with tracer.start_as_current_span(span_name, context=ctx, kind=kind) as span: + span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null")) pass except: pass