Skip to content

Commit c335af9

Browse files
maxzhangddncybulYun-Kim
authored
feat(google_genai): add tracing for google-genai (#13650)
[MLOB-2932] 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 - [x] 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](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [x] 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](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) 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](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting) [MLOB-2932]: https://datadoghq.atlassian.net/browse/MLOB-2932?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ --------- Co-authored-by: ncybul <[email protected]> Co-authored-by: Yun Kim <[email protected]>
1 parent 101a1b1 commit c335af9

30 files changed

+1129
-9
lines changed

.github/CODEOWNERS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ ddtrace/contrib/internal/anthropic @DataDog/ml-observ
148148
ddtrace/contrib/_anthropic.py @DataDog/ml-observability
149149
ddtrace/contrib/internal/google_generativeai @DataDog/ml-observability
150150
ddtrace/contrib/_google_generativeai.py @DataDog/ml-observability
151+
ddtrace/contrib/internal/google_genai @DataDog/ml-observability
152+
ddtrace/contrib/_google_genai.py @DataDog/ml-observability
151153
ddtrace/contrib/internal/vertexai @DataDog/ml-observability
152154
ddtrace/contrib/_vertexai.py @DataDog/ml-observability
153155
ddtrace/contrib/internal/langgraph @DataDog/ml-observability
@@ -169,6 +171,7 @@ tests/contrib/botocore/bedrock_utils.py @DataDog/ml-observ
169171
tests/contrib/botocore/bedrock_cassettes @DataDog/ml-observability
170172
tests/contrib/anthropic @DataDog/ml-observability
171173
tests/contrib/google_generativeai @DataDog/ml-observability
174+
tests/contrib/google_genai @DataDog/ml-observability
172175
tests/contrib/vertexai @DataDog/ml-observability
173176
tests/contrib/langgraph @DataDog/ml-observability
174177
tests/contrib/crewai @DataDog/ml-observability

.riot/requirements/1360370.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.10
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/1360370.in
6+
#
7+
annotated-types==0.7.0
8+
anyio==4.9.0
9+
attrs==25.3.0
10+
cachetools==5.5.2
11+
certifi==2025.6.15
12+
charset-normalizer==3.4.2
13+
coverage[toml]==7.9.1
14+
exceptiongroup==1.3.0
15+
google-auth==2.40.3
16+
google-genai==1.21.1
17+
h11==0.16.0
18+
httpcore==1.0.9
19+
httpx==0.28.1
20+
hypothesis==6.45.0
21+
idna==3.10
22+
iniconfig==2.1.0
23+
mock==5.2.0
24+
opentracing==2.4.0
25+
packaging==25.0
26+
pluggy==1.6.0
27+
pyasn1==0.6.1
28+
pyasn1-modules==0.4.2
29+
pydantic==2.11.7
30+
pydantic-core==2.33.2
31+
pygments==2.19.2
32+
pytest==8.4.1
33+
pytest-asyncio==1.0.0
34+
pytest-cov==6.2.1
35+
pytest-mock==3.14.1
36+
requests==2.32.4
37+
rsa==4.9.1
38+
sniffio==1.3.1
39+
sortedcontainers==2.4.0
40+
tenacity==8.5.0
41+
tomli==2.2.1
42+
typing-extensions==4.14.0
43+
typing-inspection==0.4.1
44+
urllib3==2.5.0
45+
websockets==15.0.1

.riot/requirements/153608c.txt

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.9
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/153608c.in
6+
#
7+
annotated-types==0.7.0
8+
anyio==4.9.0
9+
attrs==25.3.0
10+
cachetools==5.5.2
11+
certifi==2025.6.15
12+
charset-normalizer==3.4.2
13+
coverage[toml]==7.9.1
14+
exceptiongroup==1.3.0
15+
google-auth==2.40.3
16+
google-genai==1.21.1
17+
h11==0.16.0
18+
httpcore==1.0.9
19+
httpx==0.28.1
20+
hypothesis==6.45.0
21+
idna==3.10
22+
iniconfig==2.1.0
23+
mock==5.2.0
24+
opentracing==2.4.0
25+
packaging==25.0
26+
pluggy==1.6.0
27+
pyasn1==0.6.1
28+
pyasn1-modules==0.4.2
29+
pydantic==2.11.7
30+
pydantic-core==2.33.2
31+
pygments==2.19.2
32+
pytest==8.4.1
33+
pytest-asyncio==1.0.0
34+
pytest-cov==6.2.1
35+
pytest-mock==3.14.1
36+
requests==2.32.4
37+
rsa==4.9.1
38+
sniffio==1.3.1
39+
sortedcontainers==2.4.0
40+
tenacity==8.5.0
41+
tomli==2.2.1
42+
typing-extensions==4.14.0
43+
typing-inspection==0.4.1
44+
urllib3==2.5.0
45+
websockets==15.0.1

.riot/requirements/1bc194f.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.12
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/1bc194f.in
6+
#
7+
annotated-types==0.7.0
8+
anyio==4.9.0
9+
attrs==25.3.0
10+
cachetools==5.5.2
11+
certifi==2025.6.15
12+
charset-normalizer==3.4.2
13+
coverage[toml]==7.9.1
14+
google-auth==2.40.3
15+
google-genai==1.21.1
16+
h11==0.16.0
17+
httpcore==1.0.9
18+
httpx==0.28.1
19+
hypothesis==6.45.0
20+
idna==3.10
21+
iniconfig==2.1.0
22+
mock==5.2.0
23+
opentracing==2.4.0
24+
packaging==25.0
25+
pluggy==1.6.0
26+
pyasn1==0.6.1
27+
pyasn1-modules==0.4.2
28+
pydantic==2.11.7
29+
pydantic-core==2.33.2
30+
pygments==2.19.2
31+
pytest==8.4.1
32+
pytest-asyncio==1.0.0
33+
pytest-cov==6.2.1
34+
pytest-mock==3.14.1
35+
requests==2.32.4
36+
rsa==4.9.1
37+
sniffio==1.3.1
38+
sortedcontainers==2.4.0
39+
tenacity==8.5.0
40+
typing-extensions==4.14.0
41+
typing-inspection==0.4.1
42+
urllib3==2.5.0
43+
websockets==15.0.1

.riot/requirements/d7e97af.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.13
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/d7e97af.in
6+
#
7+
annotated-types==0.7.0
8+
anyio==4.9.0
9+
attrs==25.3.0
10+
cachetools==5.5.2
11+
certifi==2025.6.15
12+
charset-normalizer==3.4.2
13+
coverage[toml]==7.9.1
14+
google-auth==2.40.3
15+
google-genai==1.21.1
16+
h11==0.16.0
17+
httpcore==1.0.9
18+
httpx==0.28.1
19+
hypothesis==6.45.0
20+
idna==3.10
21+
iniconfig==2.1.0
22+
mock==5.2.0
23+
opentracing==2.4.0
24+
packaging==25.0
25+
pluggy==1.6.0
26+
pyasn1==0.6.1
27+
pyasn1-modules==0.4.2
28+
pydantic==2.11.7
29+
pydantic-core==2.33.2
30+
pygments==2.19.2
31+
pytest==8.4.1
32+
pytest-asyncio==1.0.0
33+
pytest-cov==6.2.1
34+
pytest-mock==3.14.1
35+
requests==2.32.4
36+
rsa==4.9.1
37+
sniffio==1.3.1
38+
sortedcontainers==2.4.0
39+
tenacity==8.5.0
40+
typing-extensions==4.14.0
41+
typing-inspection==0.4.1
42+
urllib3==2.5.0
43+
websockets==15.0.1

.riot/requirements/e9955b3.txt

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.11
3+
# by the following command:
4+
#
5+
# pip-compile --allow-unsafe --no-annotate .riot/requirements/e9955b3.in
6+
#
7+
annotated-types==0.7.0
8+
anyio==4.9.0
9+
attrs==25.3.0
10+
cachetools==5.5.2
11+
certifi==2025.6.15
12+
charset-normalizer==3.4.2
13+
coverage[toml]==7.9.1
14+
google-auth==2.40.3
15+
google-genai==1.21.1
16+
h11==0.16.0
17+
httpcore==1.0.9
18+
httpx==0.28.1
19+
hypothesis==6.45.0
20+
idna==3.10
21+
iniconfig==2.1.0
22+
mock==5.2.0
23+
opentracing==2.4.0
24+
packaging==25.0
25+
pluggy==1.6.0
26+
pyasn1==0.6.1
27+
pyasn1-modules==0.4.2
28+
pydantic==2.11.7
29+
pydantic-core==2.33.2
30+
pygments==2.19.2
31+
pytest==8.4.1
32+
pytest-asyncio==1.0.0
33+
pytest-cov==6.2.1
34+
pytest-mock==3.14.1
35+
requests==2.32.4
36+
rsa==4.9.1
37+
sniffio==1.3.1
38+
sortedcontainers==2.4.0
39+
tenacity==8.5.0
40+
typing-extensions==4.14.0
41+
typing-inspection==0.4.1
42+
urllib3==2.5.0
43+
websockets==15.0.1

ddtrace/_monkey.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"futures": True,
5151
"freezegun": True,
5252
"google_generativeai": True,
53+
"google_genai": True,
5354
"gevent": True,
5455
"graphql": True,
5556
"grpc": True,
@@ -160,6 +161,7 @@
160161
"httplib": ("http.client",),
161162
"kafka": ("confluent_kafka",),
162163
"google_generativeai": ("google.generativeai",),
164+
"google_genai": ("google.genai",),
163165
"langgraph": (
164166
"langgraph",
165167
"langgraph.graph",

ddtrace/contrib/_google_genai.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""
2+
The Google GenAI integration instruments the Google GenAI Python SDK to trace LLM requests made to
3+
Gemini and VertexAI models.
4+
5+
All traces submitted from the Google GenAI integration are tagged by:
6+
7+
- ``service``, ``env``, ``version``: see the `Unified Service Tagging docs <https://docs.datadoghq.com/getting_started/tagging/unified_service_tagging>`_.
8+
- model used in the request.
9+
- provider used in the request.
10+
11+
12+
Enabling
13+
~~~~~~~~
14+
15+
The Google GenAI integration is enabled automatically when you use
16+
:ref:`ddtrace-run<ddtracerun>` or :ref:`import ddtrace.auto<ddtraceauto>`.
17+
18+
Alternatively, use :func:`patch() <ddtrace.patch>` to manually enable the Google GenAI integration::
19+
20+
from ddtrace import config, patch
21+
22+
patch(google_genai=True)
23+
24+
Global Configuration
25+
~~~~~~~~~~~~~~~~~~~~
26+
27+
.. py:data:: ddtrace.config.google_genai["service"]
28+
29+
The service name reported by default for Google GenAI requests.
30+
31+
Alternatively, you can set this option with the ``DD_SERVICE`` or ``DD_GOOGLE_GENAI_SERVICE`` environment
32+
variables.
33+
34+
Default: ``DD_SERVICE``
35+
36+
37+
Instance Configuration
38+
~~~~~~~~~~~~~~~~~~~~~~
39+
40+
To configure the Google GenAI integration on a per-instance basis use the
41+
``Pin`` API::
42+
43+
from google import genai
44+
from ddtrace.trace import Pin
45+
46+
Pin.override(genai, service="my-google-genai-service")
47+
"""

ddtrace/contrib/integration_registry/registry.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ integrations:
383383
min: 20.12.1
384384
max: 24.11.1
385385

386+
- integration_name: google_genai
387+
is_external_package: true
388+
is_tested: true
389+
dependency_names:
390+
- google-genai
391+
tested_versions_by_dependency:
392+
google-genai:
393+
min: 1.21.1
394+
max: 1.21.1
395+
386396
- integration_name: google_generativeai
387397
is_external_package: true
388398
is_tested: true

0 commit comments

Comments
 (0)