forward otel traceparent in request headers

This commit is contained in:
Ishaan Jaff 2024-06-26 12:31:28 -07:00
parent ae431eb85d
commit b16b846711
2 changed files with 20 additions and 0 deletions

View file

@ -144,10 +144,13 @@ async def add_litellm_data_to_request(
) # do not store the original `sk-..` api key in the db
data[_metadata_variable_name]["headers"] = _headers
data[_metadata_variable_name]["endpoint"] = str(request.url)
# OTEL Controls / Tracing
# Add the OTEL Parent Trace before sending it LiteLLM
data[_metadata_variable_name][
"litellm_parent_otel_span"
] = user_api_key_dict.parent_otel_span
_add_otel_traceparent_to_data(data, request=request)
### END-USER SPECIFIC PARAMS ###
if user_api_key_dict.allowed_model_region is not None:
@ -169,3 +172,18 @@ async def add_litellm_data_to_request(
} # add the team-specific configs to the completion call
return data
def _add_otel_traceparent_to_data(data: dict, request: Request):
if data is None:
return
if request.headers:
if "traceparent" in request.headers:
# we want to forward this to the LLM Provider
# Relevant issue: https://github.com/BerriAI/litellm/issues/4419
# pass this in extra_headers
if "extra_headers" not in data:
data["extra_headers"] = {}
_exra_headers = data["extra_headers"]
if "traceparent" not in _exra_headers:
_exra_headers["traceparent"] = request.headers["traceparent"]