Skip to content

Commit c17e428

Browse files
argajaabmassDylanRussell
authored
Update google genai instrumentation to work with latest semantic convention, allow for uploading content. (open-telemetry#3772)
* Update google genai instrumentation to latest semantic convention. Co-authored-by: Aaron Abbott <[email protected]> * fix: log roles as str, event attrs as objects. * fix: proper event name * refactor: remove unused message models. * refactor: use OTel imported semconv attributes. * refactor: Inject upload_hook in Instrumentor. * refactor: rename upload hook to completion hook. * test: add tests for non streaming case and tool_call_wrapper. * test: add tool call instrumentation tests and nonstreaming recording in spans test. * fix: remove print * feature: add blobpart and filepart to message handling * fix: encode bytes as base64 when dumping to json string. * fix: always call completion hook, independently of recording settings. * test: update requirements for instrumentation-google-genai oldest env * test: bump google-genai lib version in -oldest test env. * test: fix event recording test. * Update FakeCredentials * Fix tests * fix lint issues * fix: aggregate streaming messages into one event. * fix: remove gen_ai.system from latest sem_conv * refactor: import encoder from utils-genai * build: update deps versions. * refactor: deduplicate creating attributes, use gen_ai_json_dumps * refactor: move blobpart and filedatapart to instrumentation-google-genai package. * test: add comment for headers. * test: fix span attributes formatting. * fix: lint * fix: use LogRecord instead of Event. * docs: update changelog * build: update uv.lock * test: disable lint * build: unpin opentelemetry-util-genai version * test: add e2e completion_hook test --------- Co-authored-by: Aaron Abbott <[email protected]> Co-authored-by: Dylan Russell <[email protected]> Co-authored-by: DylanRussell <[email protected]>
1 parent 5fdc5d5 commit c17e428

File tree

36 files changed

+2399
-901
lines changed

36 files changed

+2399
-901
lines changed

instrumentation-genai/opentelemetry-instrumentation-google-genai/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
- Implement the new semantic convention changes made in https://github.com/open-telemetry/semantic-conventions/pull/2179.
11+
A single event (`gen_ai.client.inference.operation.details`) is used to capture Chat History. This is opt-in,
12+
an environment variable OTEL_SEMCONV_STABILITY_OPT_IN needs to be set to `gen_ai_latest_experimental` to see them ([#3386](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3386))
13+
- Support CompletionHook for upload to cloud storage.
14+
1015
## Version 0.3b0 (2025-07-08)
1116

1217
- Add automatic instrumentation to tool call functions ([#3446](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3446))

instrumentation-genai/opentelemetry-instrumentation-google-genai/pyproject.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ classifiers = [
3838
]
3939
dependencies = [
4040
"opentelemetry-api ~=1.37",
41-
"opentelemetry-instrumentation >=0.52b1, <2",
42-
"opentelemetry-semantic-conventions >=0.52b1, <2"
43-
]
41+
"opentelemetry-instrumentation >=0.58b0, <2",
42+
"opentelemetry-semantic-conventions >=0.58b0, <2",
43+
# TODO https://github.com/open-telemetry/opentelemetry-python-contrib/issues/3786: restrict
44+
# version after the first release
45+
"opentelemetry-util-genai",]
4446

4547
[project.optional-dependencies]
4648
instruments = [
47-
"google-genai >= 1.0.0"
49+
"google-genai >= 1.32.0"
4850
]
4951

5052
[project.entry-points.opentelemetry_instrumentor]

instrumentation-genai/opentelemetry-instrumentation-google-genai/src/opentelemetry/instrumentation/google_genai/flags.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,25 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import os
15+
from os import environ
16+
from typing import Union
1617

17-
_CONTENT_RECORDING_ENV_VAR = (
18-
"OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT"
18+
from opentelemetry.instrumentation._semconv import _StabilityMode
19+
from opentelemetry.util.genai.environment_variables import (
20+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT,
1921
)
22+
from opentelemetry.util.genai.types import ContentCapturingMode
23+
from opentelemetry.util.genai.utils import get_content_capturing_mode
2024

2125

22-
def is_content_recording_enabled():
23-
return os.getenv(_CONTENT_RECORDING_ENV_VAR, "false").lower() == "true"
26+
def is_content_recording_enabled(
27+
mode: _StabilityMode,
28+
) -> Union[bool, ContentCapturingMode]:
29+
if mode == _StabilityMode.DEFAULT:
30+
capture_content = environ.get(
31+
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT, "false"
32+
)
33+
return capture_content.lower() == "true"
34+
if mode == _StabilityMode.GEN_AI_LATEST_EXPERIMENTAL:
35+
return get_content_capturing_mode()
36+
raise RuntimeError(f"{mode} mode not supported")

0 commit comments

Comments
 (0)