|
| 1 | +OpenTelemetry Google GenAI SDK Manual Instrumentation Example |
| 2 | +============================================ |
| 3 | + |
| 4 | +This is an example of how to instrument Google GenAI SDK calls when configuring |
| 5 | +OpenTelemetry SDK and Instrumentations manually. |
| 6 | + |
| 7 | +When `main.py <main.py>`_ is run, it exports traces, logs, and metrics to an OTLP |
| 8 | +compatible endpoint. Traces include details such as the model used and the |
| 9 | +duration of the chat request. Logs capture the chat request and the generated |
| 10 | +response, providing a comprehensive view of the performance and behavior of |
| 11 | +your GenAI SDK requests. Metrics include aggregate statistics such as the aggregate |
| 12 | +token usage as well as the latency distribution of the GenAI operations. |
| 13 | + |
| 14 | +Note: `.env <.env>`_ file configures additional environment variables: |
| 15 | + |
| 16 | +- `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true` configures |
| 17 | +Google GenAI SDK instrumentation to capture prompt and completion contents on |
| 18 | +events. |
| 19 | + |
| 20 | +Setup |
| 21 | +----- |
| 22 | + |
| 23 | +An OTLP compatible endpoint should be listening for traces, logs, and metrics on |
| 24 | +http://localhost:4317. If not, update "OTEL_EXPORTER_OTLP_ENDPOINT" as well. |
| 25 | + |
| 26 | +Next, set up a virtual environment like this: |
| 27 | + |
| 28 | +:: |
| 29 | + |
| 30 | + python3 -m venv .venv |
| 31 | + source .venv/bin/activate |
| 32 | + pip install "python-dotenv[cli]" |
| 33 | + pip install -r requirements.txt |
| 34 | + |
| 35 | +Run |
| 36 | +--- |
| 37 | + |
| 38 | +Run the example like this: |
| 39 | + |
| 40 | +:: |
| 41 | + |
| 42 | + export PROMPT="Your prompt here" |
| 43 | + dotenv run -- python main.py |
| 44 | + |
0 commit comments