Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 2.41 KB

File metadata and controls

60 lines (45 loc) · 2.41 KB

OpenTelemetry LangGraph instrumentation example

This sample is a LangGraph agent instrumented with OpenTelemetry to send traces and logs with GenAI prompts and responses, and metrics to Google Cloud Observability.

The Agent is a SQL expert that has full access to an ephemeral SQLite database. The database is initially empty. It is built with the the LangGraph prebuilt ReAct Agent and the SQLDatabaseToolkit.

APIs and Permissions

Enable the relevant Cloud Observability APIs if they aren't already enabled.

gcloud services enable telemetry.googleapis.com logging.googleapis.com monitoring.googleapis.com cloudtrace.googleapis.com

This sample writes to Cloud Logging, Cloud Monitoring, and Cloud Trace. Grant yourself the following roles to run the example:

Running the example

The sample can easily be run in Cloud Shell. You can also use Application Default Credentials locally. Clone and set environment variables:

git clone https://github.com/GoogleCloudPlatform/opentelemetry-operations-python.git
cd opentelemetry-operations-python/samples/langgraph-sql-agent

# Capture GenAI prompts and responses
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true
# Capture application logs automatically
export OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true

Create a virtual environment and run the sample:

python -m venv venv/
source venv/bin/activate
pip install -r requirements.txt
python main.py

Alternatively if you have uv installed:

uv run main.py

Viewing the results

To view the generated traces with Generative AI events in the GCP console, use the Trace Explorer. Filter for spans named invoke agent.