From 0919f68903a927779f0f0d19c085e8ab65df33c3 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 9 Jul 2025 08:15:01 +0000 Subject: [PATCH] Integrate OpenTelemetry for tracing - Add OpenTelemetry dependencies to pyproject.toml. - Configure OpenTelemetry in app.py for FastAPI instrumentation. - Instruct user on disabling Langsmith via environment variables and configuring OTLP endpoint. --- backend/pyproject.toml | 4 ++++ backend/src/agent/app.py | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/backend/pyproject.toml b/backend/pyproject.toml index 09eb5988..33c8a4d5 100644 --- a/backend/pyproject.toml +++ b/backend/pyproject.toml @@ -18,6 +18,10 @@ dependencies = [ "langgraph-api", "fastapi", "google-genai", + "opentelemetry-api", + "opentelemetry-sdk", + "opentelemetry-exporter-otlp", + "opentelemetry-instrumentation-fastapi", ] diff --git a/backend/src/agent/app.py b/backend/src/agent/app.py index 090e2edf..4e8d00fd 100644 --- a/backend/src/agent/app.py +++ b/backend/src/agent/app.py @@ -3,10 +3,32 @@ from fastapi import FastAPI, Request, Response from fastapi.staticfiles import StaticFiles import fastapi.exceptions +from opentelemetry import trace +from opentelemetry.sdk.trace import TracerProvider +from opentelemetry.sdk.trace.export import BatchSpanProcessor +from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor # Define the FastAPI app app = FastAPI() +# Configure OpenTelemetry +trace.set_tracer_provider(TracerProvider()) +tracer = trace.get_tracer(__name__) + +# Configure OTLP exporter +# Ensure an OTLP endpoint is available, e.g., from an OpenTelemetry Collector +# For local testing, you might run a collector in Docker: +# docker run -d -p 4317:4317 -p 4318:4318 otel/opentelemetry-collector:latest +otlp_exporter = OTLPSpanExporter( + # endpoint="http://localhost:4317", # Uncomment and set if your collector is not on localhost + # insecure=True # Set to False if using TLS +) +trace.get_tracer_provider().add_span_processor(BatchSpanProcessor(otlp_exporter)) + +# Instrument FastAPI +FastAPIInstrumentor.instrument_app(app) + def create_frontend_router(build_dir="../frontend/dist"): """Creates a router to serve the React frontend.