Skip to content

Commit

Permalink
feat: FastAPI Instrumentation for improved traceability
Browse files Browse the repository at this point in the history
  • Loading branch information
tazarov committed Oct 24, 2023
1 parent 0b0b785 commit c22e909
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions chromadb/server/fastapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions chromadb/telemetry/opentelemetry/fastapi.py
Original file line number Diff line number Diff line change
@@ -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
)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c22e909

Please sign in to comment.