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.
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.comThis sample writes to Cloud Logging, Cloud Monitoring, and Cloud Trace. Grant yourself the following roles to run the example:
roles/logging.logWriter– see https://cloud.google.com/logging/docs/access-control#permissions_and_rolesroles/monitoring.metricWriter– see https://cloud.google.com/monitoring/access-control#predefined_rolesroles/telemetry.writer– see https://cloud.google.com/trace/docs/iam#telemetry-roles
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=trueCreate a virtual environment and run the sample:
python -m venv venv/
source venv/bin/activate
pip install -r requirements.txt
python main.pyAlternatively if you have uv installed:
uv run main.pyTo view the generated traces with Generative AI
events in the
GCP console, use the Trace Explorer. Filter for spans named invoke agent.