Skip to content

Integrate OpenTelemetry for tracing #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ dependencies = [
"langgraph-api",
"fastapi",
"google-genai",
"opentelemetry-api",
"opentelemetry-sdk",
"opentelemetry-exporter-otlp",
"opentelemetry-instrumentation-fastapi",
]


Expand Down
23 changes: 23 additions & 0 deletions backend/src/agent/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,33 @@
import pathlib
from fastapi import FastAPI, 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.
Expand Down