openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

feat(instrumentation-ollama): add duration telemetry span attributes

Open Saptami191 opened this issue 6 months ago β€’ 3 comments

Summary

Adds Ollama duration-related response metadata as tracing span attributes.

Added attributes:

  • llm.ollama.total_duration
  • llm.ollama.load_duration
  • llm.ollama.prompt_eval_duration
  • llm.ollama.eval_duration

Changes

  • Extended set_model_response_attributes() to capture duration telemetry fields from Ollama responses
  • Added tracing assertions for generation and streaming generation tests

Verification

Verified locally through exported span output.

Example observed Ollama response fields:

{
  "total_duration": 115588345200,
  "load_duration": 53118691900,
  "prompt_eval_duration": 48132531100,
  "eval_duration": 13506373700
}

These values are now exported as tracing span attributes.

Checklist

  • [x] I have added tests that cover my changes.
  • [ ] If adding a new instrumentation or changing an existing one, I've added screenshots from some observability platform showing the change.
  • [x] PR name follows conventional commits format: feat(instrumentation): ... or fix(instrumentation): ....
  • [ ] (If applicable) I have updated the documentation accordingly.

Summary by CodeRabbit

  • New Features

    • Ollama instrumentation now captures four additional timing metrics in distributed traces: total duration, load duration, prompt evaluation duration, and evaluation duration. This enhancement provides deeper insight into model execution performance and resource utilization during inference operations.
  • Tests

    • Added comprehensive test coverage to verify these new duration metrics are captured accurately and contain valid timing values.

Review Change Stack

Saptami191 avatar May 26 '26 22:05 Saptami191

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

CLAassistant avatar May 26 '26 22:05 CLAassistant

No actionable comments were generated in the recent review. πŸŽ‰

ℹ️ Recent review info
βš™οΈ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 7cd09404-3b9b-4ad6-be8e-39048bde129f

πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between b39151bc4d9be6d4a7bb2189fef3b20f8a577156 and cb1f00b1bfa614c7895c22a83085b32d6ca711c4.

πŸ“’ Files selected for processing (2)
  • packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py
  • packages/opentelemetry-instrumentation-ollama/tests/test_generation.py

πŸ“ Walkthrough

Walkthrough

This PR extends Ollama instrumentation by recording four timing metrics as span attributes. The implementation extracts duration values (total, load, prompt evaluation, and inference evaluation) from the Ollama API response and sets them as OpenTelemetry span attributes. Tests verify these attributes are captured and contain non-zero values.

Changes

Ollama Duration Span Attributes

Layer / File(s) Summary
Ollama duration span attributes and test coverage
packages/opentelemetry-instrumentation-ollama/opentelemetry/instrumentation/ollama/span_utils.py, packages/opentelemetry-instrumentation-ollama/tests/test_generation.py
set_model_response_attributes sets four new span attributes (llm.ollama.total_duration, llm.ollama.load_duration, llm.ollama.prompt_eval_duration, llm.ollama.eval_duration) from the response object. Tests verify these attributes are present and non-zero in both legacy generation and legacy streaming generation scenarios.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related issues

  • traceloop/openllmetry#4192: Implements the exact Ollama duration span attributes requested, directly addressing the feature in the related issue.

Poem

🐰 Four timers ticked and found their place, Duration metrics now embrace, The Ollama span so bright and true, From load to eval, metrics shrew! ✨

πŸš₯ Pre-merge checks | βœ… 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
βœ… Passed checks (4 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check βœ… Passed The title clearly and specifically describes the main change: adding duration telemetry span attributes to Ollama instrumentation, which matches the changeset exactly.
Linked Issues check βœ… Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check βœ… Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
πŸ§ͺ Generate unit tests (beta)
  • [ ] Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar May 26 '26 22:05 coderabbitai[bot]

Before

{
  "llm.provider": "ollama",
  "llm.model": "llama3"
}

After

{
  "llm.provider": "ollama",
  "llm.model": "llama3",
  "llm.ollama.total_duration": 115588345200,
  "llm.ollama.load_duration": 53118691900,
  "llm.ollama.prompt_eval_duration": 48132531100,
  "llm.ollama.eval_duration": 13506373700
}

Saptami191 avatar May 26 '26 22:05 Saptami191