forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_otel_instrumentation.py
46 lines (34 loc) · 1.93 KB
/
setup_otel_instrumentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Configures Open Telemetry instrumentation for Gen AI SDK.
Configuring observability for the Gen AI SDK involves two steps:
1. Ensuring that data is written to Open Telemetry APIs when
the Gen AI SDK is used.
2. Ensuring that the Open Telemetry APIs route data to some
observability backend(s) for storing the data.
This file addresses #1. This also means that this file can be used
for observability of the Gen AI SDK in cases where you choose an
observability backend other than those of Google Cloud Observability.\
See also:
- https://github.com/open-telemetry/opentelemetry-python-contrib/
- /instrumentation-genai/opentelemetry-instrumentation-google-genai
- /examples/manual
"""
# [START setup_otel_instrumentation_snippet]
from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
def setup_otel_instrumentation() -> None:
"""Add telemetry collection and instrumentation to key libraries.
This function is responsible for monkey-patching multiple libraries of
interest to inject telemetry collection and instrumentation, routing
relevant telemetry information to the Open Telemetry library.
"""
# Instrument the Gen AI SDK library (PyPi: "google-genai"). This
# monkey-patches that library to inject Open Telemetry instrumentation.
GoogleGenAiSdkInstrumentor().instrument()
# Instrument the Python Requests library (PyPi: "requests"). This
# monkey-patches that library to inject Open Telemetry instrumentation.
# The requests library is a dependency of the Gen AI SDK library; it is
# used to invoke the Vertex AI API or the Gemini API. Instrumenting this
# lower-level dependency of the Gen AI SDK provides more information
# about the timing and operation at lower layers of the stack.
RequestsInstrumentor().instrument()
# [END setup_otel_instrumentation_snippet]