diff --git a/litellm/proxy/common_utils/debug_utils.py b/litellm/proxy/common_utils/debug_utils.py index 6c96967aa..8306ad0fb 100644 --- a/litellm/proxy/common_utils/debug_utils.py +++ b/litellm/proxy/common_utils/debug_utils.py @@ -38,6 +38,25 @@ async def get_otel_spans(): print("Spans: ", recorded_spans) # noqa + most_recent_parent = None + most_recent_start_time = 1000000 + spans_grouped_by_parent = {} + for span in recorded_spans: + if span.parent is not None: + parent_trace_id = span.parent.trace_id + if parent_trace_id not in spans_grouped_by_parent: + spans_grouped_by_parent[parent_trace_id] = [] + spans_grouped_by_parent[parent_trace_id].append(span.name) + + # check time of span + if span.start_time > most_recent_start_time: + most_recent_parent = parent_trace_id + most_recent_start_time = span.start_time + # these are otel spans - get the span name span_names = [span.name for span in recorded_spans] - return {"otel_spans": span_names} + return { + "otel_spans": span_names, + "spans_grouped_by_parent": spans_grouped_by_parent, + "most_recent_parent": most_recent_parent, + } diff --git a/tests/otel_tests/test_otel.py b/tests/otel_tests/test_otel.py index a07e93e3f..e372ddb95 100644 --- a/tests/otel_tests/test_otel.py +++ b/tests/otel_tests/test_otel.py @@ -100,12 +100,17 @@ async def test_chat_completion_check_otel_spans(): print("otel_spans: ", otel_spans) all_otel_spans = otel_spans["otel_spans"] + most_recent_parent = str(otel_spans["most_recent_parent"]) + print("Most recent OTEL parent: ", most_recent_parent) + print("\n spans grouped by parent: ", otel_spans["spans_grouped_by_parent"]) + parent_trace_spans = otel_spans["spans_grouped_by_parent"][most_recent_parent] - assert len(all_otel_spans) == 5 + print("Parent trace spans: ", parent_trace_spans) + + assert len(parent_trace_spans) == 4 # 'postgres', 'redis', 'raw_gen_ai_request', 'litellm_request', 'Received Proxy Server Request' in the span - assert "postgres" in all_otel_spans - assert "redis" in all_otel_spans - assert "raw_gen_ai_request" in all_otel_spans - assert "litellm_request" in all_otel_spans - assert "Received Proxy Server Request" in all_otel_spans + assert "postgres" in parent_trace_spans + assert "redis" in parent_trace_spans + assert "raw_gen_ai_request" in parent_trace_spans + assert "litellm_request" in parent_trace_spans