| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | from collections.abc import Mapping |
| 5 | |
| 6 | from vllm.logger import init_logger |
| 7 | from vllm.utils.func_utils import run_once |
| 8 | |
| 9 | logger = init_logger(__name__) |
| 10 | |
| 11 | # Standard W3C headers used for context propagation |
| 12 | TRACE_HEADERS = ["traceparent", "tracestate"] |
| 13 | |
| 14 | |
| 15 | class SpanAttributes: |
| 16 | """Standard attributes for spans. |
| 17 | |
| 18 | These are largely based on OpenTelemetry Semantic Conventions but are defined |
| 19 | here as constants so they can be used by any backend or logger. |
| 20 | """ |
| 21 | |
| 22 | # Attribute names copied from OTel semantic conventions to avoid version conflicts |
| 23 | GEN_AI_USAGE_COMPLETION_TOKENS = "gen_ai.usage.completion_tokens" |
| 24 | GEN_AI_USAGE_PROMPT_TOKENS = "gen_ai.usage.prompt_tokens" |
| 25 | GEN_AI_REQUEST_MAX_TOKENS = "gen_ai.request.max_tokens" |
| 26 | GEN_AI_REQUEST_TOP_P = "gen_ai.request.top_p" |
| 27 | GEN_AI_REQUEST_TEMPERATURE = "gen_ai.request.temperature" |
| 28 | GEN_AI_RESPONSE_MODEL = "gen_ai.response.model" |
| 29 | |
| 30 | # Custom attributes added until they are standardized |
| 31 | GEN_AI_REQUEST_ID = "gen_ai.request.id" |
| 32 | GEN_AI_REQUEST_N = "gen_ai.request.n" |
| 33 | GEN_AI_USAGE_NUM_SEQUENCES = "gen_ai.usage.num_sequences" |
| 34 | GEN_AI_LATENCY_TIME_IN_QUEUE = "gen_ai.latency.time_in_queue" |
| 35 | GEN_AI_LATENCY_TIME_TO_FIRST_TOKEN = "gen_ai.latency.time_to_first_token" |
| 36 | GEN_AI_LATENCY_E2E = "gen_ai.latency.e2e" |
| 37 | GEN_AI_LATENCY_TIME_IN_SCHEDULER = "gen_ai.latency.time_in_scheduler" |
| 38 | |
| 39 | # Latency breakdowns |
| 40 | GEN_AI_LATENCY_TIME_IN_MODEL_FORWARD = "gen_ai.latency.time_in_model_forward" |
| 41 | GEN_AI_LATENCY_TIME_IN_MODEL_EXECUTE = "gen_ai.latency.time_in_model_execute" |
| 42 | GEN_AI_LATENCY_TIME_IN_MODEL_PREFILL = "gen_ai.latency.time_in_model_prefill" |
| 43 | GEN_AI_LATENCY_TIME_IN_MODEL_DECODE = "gen_ai.latency.time_in_model_decode" |
| 44 | GEN_AI_LATENCY_TIME_IN_MODEL_INFERENCE = "gen_ai.latency.time_in_model_inference" |
| 45 | |
| 46 | |
| 47 | class LoadingSpanAttributes: |
| 48 | """Custom attributes for code-level tracing (file, line number).""" |
| 49 | |
| 50 | CODE_NAMESPACE = "code.namespace" |
| 51 | CODE_FUNCTION = "code.function" |
| 52 | CODE_FILEPATH = "code.filepath" |
| 53 | CODE_LINENO = "code.lineno" |
| 54 | |
| 55 | |
| 56 | def contains_trace_headers(headers: Mapping[str, str]) -> bool: |
| 57 | """Check if the provided headers dictionary contains trace context.""" |
| 58 | return any(h in headers for h in TRACE_HEADERS) |
| 59 | |
| 60 | |
| 61 | def extract_trace_headers(headers: Mapping[str, str]) -> Mapping[str, str]: |
| 62 | """Extract only trace-related headers from a larger header dictionary. |
| 63 | Useful for logging or passing context to a non-OTel client. |
| 64 | """ |
| 65 | return {h: headers[h] for h in TRACE_HEADERS if h in headers} |
| 66 | |
| 67 | |
| 68 | @run_once |
| 69 | def log_tracing_disabled_warning() -> None: |
| 70 | logger.warning("Received a request with trace context but tracing is disabled") |