openllmetry icon indicating copy to clipboard operation
openllmetry copied to clipboard

feat(google-genai): instrumentation for google-genai

Open dinmukhamedm opened this issue 10 months ago • 5 comments

Fixes #2675

Screenshot 2025-04-21 at 18 45 37
  • [X] I have added tests that cover my changes.
  • [X] 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): ....
  • [X] (If applicable) I have updated the documentation accordingly. --> I checked and it looks like the README already claims we instrument Google gemini

[!IMPORTANT] Introduces Google GenAI instrumentation with tracing capabilities, configuration, utilities, and comprehensive tests, along with project setup and version update.

  • New Instrumentation:
    • Adds GoogleGenAiInstrumentor in __init__.py for tracing Google GenAI API calls.
    • Supports synchronous and asynchronous methods for generate_content and generate_content_stream.
    • Attributes like model, temperature, and token usage are captured.
  • Configuration and Utilities:
    • Adds Config class in config.py for exception logging.
    • Utility functions in utils.py for handling span attributes and exceptions.
  • Testing:
    • Adds unit tests in test_generate_content.py for various scenarios including async and streaming.
    • Uses VCR cassettes for HTTP request recording in tests/cassettes/.
  • Project Setup:
    • Adds pyproject.toml, poetry.toml, and project.json for project configuration.
    • Includes .flake8 for linting configuration.
  • Version Update:
    • Updates version in version.py to 0.39.2.

This description was created by Ellipsis for f9c0b0edf1d8bc2b27595a1f25635ac3bf8a6d25. You can customize this summary. It will automatically update as commits are pushed.

dinmukhamedm avatar Apr 21 '25 17:04 dinmukhamedm

Does this share / reuse the instrumentation in the contrib repo?

https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation-genai/opentelemetry-instrumentation-google-genai

michaelsafyan avatar Apr 21 '25 20:04 michaelsafyan

@michaelsafyan No, but I guess it should.

My PR does instrument google_genai, but is certainly worse (both the code quality and functionality) than the contrib library mentioned. However, it sends the prompt/completion contents as attributes, while the new library does so as log events (accepted semconv).

I don't think it's easy to mix up two instrumentors for the same library. @nirga What are your thoughts on this? I remember, you mentioned a potential mechanism that would convert log events to span attributes, is there a prototype? Feel free to close this PR in favor of the contrib library

dinmukhamedm avatar Apr 21 '25 20:04 dinmukhamedm

I think it would be better to have Traceloop wrap/invoke the OTel Contrib instrumentation. That way, more (joint) effort can be put into improving the (shared) instrumentation for the Google Gen AI instrumentation library rather than competing efforts. That work could include adding options to support multiple different kinds of output formats.

michaelsafyan avatar Apr 21 '25 20:04 michaelsafyan

I completely agree, @michaelsafyan! The main blocker right now is aligning on the attribute format, since we can't use the current events format that's implemented. Once we finalize that, we can move forward with switching to the contrib implementation.

nirga avatar Apr 22 '25 11:04 nirga

Mind adding them?

Sure will do

dinmukhamedm avatar Apr 27 '25 09:04 dinmukhamedm