Skip to content

Commit

Permalink
[ENH]: FastAPI Instrumentation for improved traceability (#1281)
Browse files Browse the repository at this point in the history
## Description of changes

*Summarize the changes made by this PR.*
 - Improvements & Bug fixes
	 - ...
 - New functionality
- Added FastAPI OTEL instrumentation; this leads to much more consistent
and cohesive traces (before, we had some disjoint traces as the context
was not contiguous, from top to bottom)

## Test plan
*How are these changes tested?*

- [ ] Tests pass locally with `pytest` for python, `yarn test` for js

## Documentation Changes
N/A


![image](https://github.com/chroma-core/chroma/assets/1157440/74fe9603-2f57-42ff-b1c4-d93744aba985)
  • Loading branch information
tazarov authored Nov 13, 2023
1 parent 7aa2e3c commit 12b6712
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 @@ -50,6 +50,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 @@ -266,6 +267,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 12b6712

Please sign in to comment.