Skip to content

Commit 5d3bf30

Browse files
sg263shige
andauthored
merge develop trace FD_START (#3253)
Co-authored-by: shige <[email protected]>
1 parent f672a34 commit 5d3bf30

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

fastdeploy/entrypoints/openai/api_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
get_filtered_metrics,
4646
main_process_metrics,
4747
)
48-
from fastdeploy.metrics.trace_util import inject_to_metadata, instrument
48+
from fastdeploy.metrics.trace_util import fd_start_span, inject_to_metadata, instrument
4949
from fastdeploy.utils import (
5050
FlexibleArgumentParser,
5151
api_server_logger,
@@ -270,6 +270,7 @@ def launch_api_server() -> None:
270270

271271
api_server_logger.info(f"launch Fastdeploy api server... port: {args.port}")
272272
api_server_logger.info(f"args: {args.__dict__}")
273+
fd_start_span("FD_START")
273274

274275
try:
275276
uvicorn.run(

fastdeploy/envs.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@
8080
"EXPORTER_OTLP_HEADERS": lambda: os.getenv("EXPORTER_OTLP_HEADERS"),
8181
# enable kv cache block scheduler v1 (no need for kv_cache_ratio)
8282
"ENABLE_V1_KVCACHE_SCHEDULER": lambda: int(os.getenv("ENABLE_V1_KVCACHE_SCHEDULER", "0")),
83+
# set trace attribute job_id.
84+
"FD_JOB_ID": lambda: os.getenv("FD_JOB_ID"),
8385
}
8486

8587

fastdeploy/metrics/trace_util.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23

34
from fastapi import FastAPI
45
from opentelemetry import trace
@@ -176,7 +177,22 @@ def start_span(span_name, request, kind=trace.SpanKind.CLIENT):
176177
return
177178
# extract Trace context from request.metadata.trace_carrier
178179
ctx = extract_from_metadata(request)
179-
with tracer.start_as_current_span(span_name, context=ctx, kind=kind):
180+
with tracer.start_as_current_span(span_name, context=ctx, kind=kind) as span:
181+
span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null"))
182+
pass
183+
except:
184+
pass
185+
186+
187+
def fd_start_span(span_name, kind=trace.SpanKind.CLIENT):
188+
"""
189+
when fd start, start a new span show start success
190+
"""
191+
try:
192+
if not traces_enable:
193+
return
194+
with tracer.start_as_current_span(span_name, kind=kind) as span:
195+
span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null"))
180196
pass
181197
except:
182198
pass
@@ -191,7 +207,8 @@ def start_span_request(span_name, request, kind=trace.SpanKind.CLIENT):
191207
return
192208
# extract Trace context from request.metadata.trace_carrier
193209
ctx = extract_from_request(request)
194-
with tracer.start_as_current_span(span_name, context=ctx, kind=kind):
210+
with tracer.start_as_current_span(span_name, context=ctx, kind=kind) as span:
211+
span.set_attribute("job_id", os.getenv("FD_JOB_ID", default="null"))
195212
pass
196213
except:
197214
pass

0 commit comments

Comments
 (0)