|
| 1 | +# OpenTelemetry LangGraph instrumentation example |
| 2 | + |
| 3 | +<!-- TODO: link to devsite doc once it is published --> |
| 4 | + |
| 5 | +This sample is a LangGraph agent instrumented with OpenTelemetry to send traces and logs with |
| 6 | +GenAI prompts and responses, and metrics to Google Cloud Observability. |
| 7 | + |
| 8 | +The Agent is a SQL expert that has full access to an ephemeral SQLite database. The database is |
| 9 | +initially empty. It is built with the the LangGraph prebuilt [ReAct |
| 10 | +Agent](https://langchain-ai.github.io/langgraph/agents/agents/#basic-configuration#code) and the |
| 11 | +[SQLDatabaseToolkit](https://python.langchain.com/docs/integrations/tools/sql_database/). |
| 12 | + |
| 13 | +## Permissions |
| 14 | + |
| 15 | +This sample writes to Cloud Logging, Cloud Monitoring, and Cloud Trace. Grant yourself the |
| 16 | +following roles to run the example: |
| 17 | +- `roles/logging.logWriter` – see https://cloud.google.com/logging/docs/access-control#permissions_and_roles |
| 18 | +- `roles/monitoring.metricWriter` – see https://cloud.google.com/monitoring/access-control#predefined_roles |
| 19 | +- `roles/cloudtrace.agent` – see https://cloud.google.com/trace/docs/iam#trace-roles |
| 20 | + |
| 21 | +## Running the example |
| 22 | + |
| 23 | +The sample can easily be run in Cloud Shell. You can also use |
| 24 | +[Application Default Credentials][ADC] locally. |
| 25 | + |
| 26 | +```sh |
| 27 | +git clone https://github.com/GoogleCloudPlatform/opentelemetry-operations-python.git |
| 28 | +cd opentelemetry-operations-python/samples/langgraph-sql-agent |
| 29 | + |
| 30 | +# Capture GenAI prompts and responses |
| 31 | +export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true |
| 32 | +# Capture application logs automatically |
| 33 | +export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true |
| 34 | +uv run main.py |
| 35 | +``` |
| 36 | + |
| 37 | +## Viewing the results |
| 38 | + |
| 39 | +To view the generated traces with [Generative AI |
| 40 | +events](https://cloud.google.com/trace/docs/finding-traces#view_generative_ai_events) in the |
| 41 | +GCP console, use the [Trace Explorer](https://cloud.google.com/trace/docs/finding-traces). |
| 42 | + |
| 43 | +[ADC]: https://cloud.google.com/docs/authentication/application-default-credentials |
0 commit comments