diff --git a/chromadb/server/fastapi/__init__.py b/chromadb/server/fastapi/__init__.py index b66bf33bda6..647cc70d777 100644 --- a/chromadb/server/fastapi/__init__.py +++ b/chromadb/server/fastapi/__init__.py @@ -38,6 +38,7 @@ from starlette.requests import Request import logging +from chromadb.telemetry.opentelemetry.fastapi import instrument_fastapi from chromadb.types import Database, Tenant from chromadb.telemetry.product import ServerContext, ProductTelemetryClient from chromadb.telemetry.opentelemetry import ( @@ -248,6 +249,7 @@ def __init__(self, settings: Settings): self._app.include_router(self.router) use_route_names_as_operation_ids(self._app) + instrument_fastapi(self._app) def app(self) -> fastapi.FastAPI: return self._app diff --git a/chromadb/telemetry/opentelemetry/fastapi.py b/chromadb/telemetry/opentelemetry/fastapi.py new file mode 100644 index 00000000000..34825794555 --- /dev/null +++ b/chromadb/telemetry/opentelemetry/fastapi.py @@ -0,0 +1,10 @@ +from typing import List, Optional +from fastapi import FastAPI +from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor + + +def instrument_fastapi(app: FastAPI, excluded_urls: Optional[List[str]] = None) -> None: + """Instrument FastAPI to emit OpenTelemetry spans.""" + FastAPIInstrumentor.instrument_app( + app, excluded_urls=",".join(excluded_urls) if excluded_urls else None + ) diff --git a/pyproject.toml b/pyproject.toml index 04d7e79c27a..cdeeb8aed20 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ dependencies = [ 'onnxruntime >= 1.14.1', 'opentelemetry-api>=1.2.0', 'opentelemetry-exporter-otlp-proto-grpc>=1.2.0', + 'opentelemetry-instrumentation-fastapi>=0.41b0', 'opentelemetry-sdk>=1.2.0', 'tokenizers >= 0.13.2', 'pypika >= 0.48.9', diff --git a/requirements.txt b/requirements.txt index 4ad9f29aeb2..2932cdd91d4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -10,6 +10,7 @@ numpy>=1.22.4; python_version >= '3.8' onnxruntime>=1.14.1 opentelemetry-api>=1.2.0 opentelemetry-exporter-otlp-proto-grpc>=1.2.0 +opentelemetry-instrumentation-fastapi>=0.41b0 opentelemetry-sdk>=1.2.0 overrides==7.3.1 posthog==2.4.0