From cb1f00b1bfa614c7895c22a83085b32d6ca711c4 Mon Sep 17 00:00:00 2001 From: Saptami191 Date: Wed, 27 May 2026 03:46:48 +0530 Subject: [PATCH] Add Ollama duration telemetry span attributes --- .../instrumentation/ollama/span_utils.py | 24 +++++++++++++++++++ .../tests/test_generation.py | 16 +++++++++++++ 2 files changed, 40 insertions(+) diff --git a/packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py b/packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py index e108f04c21..1977ef6d28 100644 --- a/packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py +++ b/packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py @@ -111,6 +111,30 @@ def set_model_response_attributes(span, token_histogram, llm_request_type, respo ) _set_span_attribute(span, GenAIAttributes.GEN_AI_SYSTEM, "Ollama") + _set_span_attribute( + span, + "llm.ollama.total_duration", + response.get("total_duration"), + ) + + _set_span_attribute( + span, + "llm.ollama.load_duration", + response.get("load_duration"), + ) + + _set_span_attribute( + span, + "llm.ollama.prompt_eval_duration", + response.get("prompt_eval_duration"), + ) + + _set_span_attribute( + span, + "llm.ollama.eval_duration", + response.get("eval_duration"), + ) + if ( token_histogram is not None and isinstance(input_tokens, int) diff --git a/packages/opentelemetry-instrumentation-ollama/tests/test_generation.py b/packages/opentelemetry-instrumentation-ollama/tests/test_generation.py index 153e61ce2d..ac8aab2029 100644 --- a/packages/opentelemetry-instrumentation-ollama/tests/test_generation.py +++ b/packages/opentelemetry-instrumentation-ollama/tests/test_generation.py @@ -38,6 +38,14 @@ def test_ollama_generation_legacy( GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS ) + ollama_span.attributes.get(GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS) + assert ollama_span.attributes.get("llm.ollama.total_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.load_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.prompt_eval_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.eval_duration") > 0 + logs = log_exporter.get_finished_logs() assert ( len(logs) == 0 @@ -162,6 +170,14 @@ def test_ollama_streaming_generation_legacy( GenAIAttributes.GEN_AI_USAGE_OUTPUT_TOKENS ) + ollama_span.attributes.get(GenAIAttributes.GEN_AI_USAGE_INPUT_TOKENS) + assert ollama_span.attributes.get("llm.ollama.total_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.load_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.prompt_eval_duration") > 0 + + assert ollama_span.attributes.get("llm.ollama.eval_duration") > 0 + logs = log_exporter.get_finished_logs() assert ( len(logs) == 0