Skip to content

feat(google_genai): [MLOB-2932] add apm tracing for google-genai #13650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 69 commits into
base: main
Choose a base branch
from

Conversation

maxzhangdd
Copy link

@maxzhangdd maxzhangdd commented Jun 11, 2025

This PR adds support for APM tracing of Google's GenAI Python SDK. Traces currently only contain UST tags as well as provider and model (LLMObs tracing of inputs/outputs and metadata will be done in a later PR).
Traced calls:

  • google.genai.models.Models.generate_content
  • google.genai.models.Models.generate_content_stream
  • google.genai.models.AsyncModels.generate_content
  • google.genai.models.AsyncModels.generate_content_stream

Checklist

  • PR author has checked that all the criteria below are met
  • The PR description includes an overview of the change
  • The PR description articulates the motivation for the change
  • The change includes tests OR the PR description describes a testing strategy
  • The PR description notes risks associated with the change, if any
  • Newly-added code is easy to change
  • The change follows the library release note guidelines
  • The change includes or references documentation updates if necessary
  • Backport labels are set (if applicable)

Reviewer Checklist

  • Reviewer has checked that all the criteria below are met
  • Title is accurate
  • All changes are related to the pull request's stated goal
  • Avoids breaking API changes
  • Testing strategy adequately addresses listed risks
  • Newly-added code is easy to change
  • Release note makes sense to a user of the library
  • If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment
  • Backport labels are set in a manner that is consistent with the release branch maintenance policy

Copy link
Contributor

github-actions bot commented Jun 11, 2025

CODEOWNERS have been resolved as:

.riot/requirements/1de4a65.txt                                          @DataDog/apm-python
.riot/requirements/7d83e7d.txt                                          @DataDog/apm-python
.riot/requirements/97b1ae2.txt                                          @DataDog/apm-python
.riot/requirements/ce785c0.txt                                          @DataDog/apm-python
.riot/requirements/f5e518d.txt                                          @DataDog/apm-python
ddtrace/contrib/_google_genai.py                                        @DataDog/ml-observability
ddtrace/contrib/internal/google_genai/_utils.py                         @DataDog/ml-observability
ddtrace/contrib/internal/google_genai/patch.py                          @DataDog/ml-observability
ddtrace/llmobs/_integrations/google_genai.py                            @DataDog/ml-observability
releasenotes/notes/google_genai_apm_tracing-a88d4a4dada947d6.yaml       @DataDog/apm-python
tests/contrib/google_genai/__init__.py                                  @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/cassettes/v1/generate_content.yaml           @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/cassettes/v1/generate_content_stream.yaml    @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/conftest.py                                  @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/test_google_genai.py                         @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/test_google_genai_patch.py                   @DataDog/apm-core-python @DataDog/apm-idm-python
tests/contrib/google_genai/utils.py                                     @DataDog/apm-core-python @DataDog/apm-idm-python
tests/snapshots/tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content.json  @DataDog/apm-python
tests/snapshots/tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content_error.json  @DataDog/apm-python
tests/snapshots/tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content_stream.json  @DataDog/apm-python
tests/snapshots/tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content_stream_error.json  @DataDog/apm-python
tests/snapshots/tests.contrib.google_genai.test_google_genai.test_google_genai_vertex_generate_content.json  @DataDog/apm-python
.github/CODEOWNERS                                                      @DataDog/python-guild @DataDog/apm-core-python
ddtrace/_monkey.py                                                      @DataDog/apm-core-python
ddtrace/contrib/integration_registry/registry.yaml                      @DataDog/apm-core-python @DataDog/apm-idm-python
ddtrace/llmobs/_integrations/__init__.py                                @DataDog/ml-observability
ddtrace/settings/_config.py                                             @DataDog/apm-core-python
docs/integrations.rst                                                   @DataDog/python-guild
docs/spelling_wordlist.txt                                              @DataDog/python-guild
riotfile.py                                                             @DataDog/apm-python
supported_versions_output.json                                          @DataDog/apm-core-python
supported_versions_table.csv                                            @DataDog/apm-core-python
tests/llmobs/suitespec.yml                                              @DataDog/ml-observability

Copy link
Contributor

github-actions bot commented Jun 11, 2025

Bootstrap import analysis

Comparison of import times between this PR and base.

Summary

The average import time from this PR is: 275 ± 3 ms.

The average import time from base is: 280 ± 4 ms.

The import time difference between this PR and base is: -4.6 ± 0.2 ms.

Import time breakdown

The following import paths have shrunk:

ddtrace.auto 2.229 ms (0.81%)
ddtrace.bootstrap.sitecustomize 1.548 ms (0.56%)
ddtrace.bootstrap.preload 1.548 ms (0.56%)
ddtrace.internal.remoteconfig.client 0.683 ms (0.25%)
ddtrace 0.682 ms (0.25%)
ddtrace.internal._unpatched 0.032 ms (0.01%)
json 0.032 ms (0.01%)
json.decoder 0.032 ms (0.01%)
re 0.032 ms (0.01%)
enum 0.032 ms (0.01%)
types 0.032 ms (0.01%)

@maxzhangdd maxzhangdd changed the title WIP feat(google_genai): [MLOB-2932] add apm tracing for google-genai Jun 13, 2025
@maxzhangdd maxzhangdd closed this Jun 13, 2025
@maxzhangdd maxzhangdd reopened this Jun 13, 2025
@pr-commenter
Copy link

pr-commenter bot commented Jun 13, 2025

Benchmarks

Benchmark execution time: 2025-06-17 23:33:48

Comparing candidate commit 23ab599 in PR branch maxzhang/google-genai-integration with baseline commit 5592908 in branch main.

Found 0 performance improvements and 3 performance regressions! Performance is the same for 558 metrics, 3 unstable metrics.

scenario:iastaspectsospath-ospathbasename_aspect

  • 🟥 execution_time [+741.751ns; +850.827ns] or [+17.595%; +20.182%]

scenario:iastaspectsospath-ospathjoin_aspect

  • 🟥 execution_time [+819.918ns; +957.207ns] or [+13.361%; +15.599%]

scenario:iastaspectsospath-ospathnormcase_aspect

  • 🟥 execution_time [+306.928ns; +389.669ns] or [+8.855%; +11.242%]

Comment on lines 4 to 5
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/quickstart
# for vertex, it seems like the best way to associate provider name with each call is based on the model name prefix
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked this link and it doesn't seem to show the model names in the below context. Is this the correct link?

Suggested change
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/quickstart
# for vertex, it seems like the best way to associate provider name with each call is based on the model name prefix
# https://cloud.google.com/vertex-ai/generative-ai/docs/model-garden/quickstart
# VertexAI: the best way to associate provider name with each call is checking the model name prefix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find a definitive source for providers.

Unlike what we initially thought, its hard to get the provider from the full path since it is not required and users can simply provide a model name. : https://github.com/googleapis/python-genai/blob/main/google/genai/models.py#L6005

So this code is a bit more best-effort. Gemini only exports google provided models whereas vertex lists supported models on the left side of the provided link. I just manually mapped supported models to providers.

Let me know if you have any suggestions on how to improve this part.


@pytest.mark.snapshot(token="tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content_async")
async def test_google_genai_generate_content_async(google_genai_vcr, genai):
with google_genai_vcr.use_cassette("generate_content_async.yaml"):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the request/response from sync and async generate_content methods are the same. Can we just reuse the same snapshot and cassette files? For the sake of minimizing test files we need to maintain
i.e.

@pytest.mark.snapshot(token="tests.contrib.google_genai.test_google_genai.test_google_genai_generate_content")
...
    with google_genai_vcr.use_cassette("generate_content.yaml"):

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was able to deduplicate cassettes and snapshots. but for snapshots, I had to add an ignore on resource to prevent this:

span mismatch on 'resource': got 'AsyncModels.generate_content_stream' which does not match expected 'Models.generate_content_stream'..

is this a good idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants