Skip to content

Commit

Permalink
feat(sentry): add Sentry watcher (#120)
Browse files Browse the repository at this point in the history
* Add dependency Sentry

* Add and initialize Sentry

* Add Sentry configuration info to .env.example
  • Loading branch information
zilaei authored Aug 14, 2024
1 parent bb30b0c commit 0941368
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 2 deletions.
12 changes: 12 additions & 0 deletions fai-rag-app/fai-backend/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,15 @@ CHAT_MODEL=gpt-4o

# Model name to use for RAG scoring prompt
SCORING_MODEL=gpt-3.5-turbo

# Enable/Disable Sentry error tracking
SENTRY_ENABLED=false

# Sentry configuration
# Refs: https://docs.sentry.io/platforms/python/configuration/
#
SENTRY_DSN=your-sentry-dsn
SENTRY_LOGGING_LEVEL=ERROR
SENTRY_EVENT_LEVEL=ERROR
SENTRY_TRACE_SAMPLE_RATE=0.1
SENTRY_ENVIRONMENT=development
6 changes: 6 additions & 0 deletions fai-rag-app/fai-backend/fai_backend/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ class Settings(BaseSettings, extra=Extra.ignore):
DEFAULT_LANGUAGE: str = 'en'
FILE_UPLOAD_PATH: str = 'uploads'
LLM_BACKEND: Literal['parrot', 'openai'] = 'parrot'
SENTRY_ENABLED: bool = False
SENTRY_DSN: SecretStr = ''
SENTRY_LOGGING_LEVEL: str = 'ERROR'
SENTRY_EVENT_LEVEL: str = 'ERROR'
SENTRY_TRACE_SAMPLE_RATE: float = 0.1
SENTRY_ENVIRONMENT: str = 'development'

class Config:
env_file = '.env'
Expand Down
9 changes: 8 additions & 1 deletion fai-rag-app/fai-backend/fai_backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from fastapi import Depends, FastAPI, Header, Request
from fastapi.middleware.cors import CORSMiddleware
from sentry_sdk import Hub, capture_message
from sse_starlette import EventSourceResponse, ServerSentEvent
from starlette.responses import HTMLResponse, RedirectResponse

Expand All @@ -26,18 +27,24 @@
from fai_backend.repositories import chat_history_repo
from fai_backend.schema import ProjectUser
from fai_backend.serializer.impl.base64 import Base64Serializer
from fai_backend.setup import setup_db, setup_project
from fai_backend.setup import setup_db, setup_project, setup_sentry
from fai_backend.vector.routes import router as vector_router


@asynccontextmanager
async def lifespan(_app: FastAPI):
console.log('Try setup Sentry')
await setup_sentry()
console.log('Try setup db')
await setup_db()
console.log('Try setup initial project')
await setup_project()
yield
console.log('😴 Unmounting app ...')
console.log('Shutting down Sentry')
client = Hub.current.client
if client is not None:
client.close(timeout=2.0)


app = FastAPI(title='FAI RAG App', redirect_slashes=True, lifespan=lifespan)
Expand Down
Empty file.
63 changes: 63 additions & 0 deletions fai-rag-app/fai-backend/fai_backend/sentry/watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import logging

import sentry_sdk
from sentry_sdk.integrations.logging import LoggingIntegration


class Watcher:
"""
A class to configure and initialize Sentry integration for a Python application.
Attributes:
-----------
dns : str
The Sentry DSN (Data Source Name) pointing to the Sentry project.
level : int
The minimum logging level at which logs should be captured.
event_level : int
The minimum logging level at which logs should be sent as events to Sentry.
trace_sample_rate : float
The rate at which traces should be sampled and sent to Sentry. Range [0.0, 1.0].
Methods:
--------
initialize():
Initializes the Sentry SDK with the provided configuration.
get_config() -> dict[str, str]:
Returns the current Sentry configuration.
"""

def __init__(
self,
dsn: str,
environment: str,
level: str = 'ERROR',
event_level: str = 'ERROR',
trace_sample_rate: float = 0.1
) -> None:
self.dsn = dsn
self.level = logging.getLevelName(level)
self.event_level = logging.getLevelName(event_level)
self.trace_sample_rate = trace_sample_rate
self.environment = environment

def initialize(self) -> None:
sentry_logging = LoggingIntegration(
level=self.level,
event_level=self.event_level
)

sentry_sdk.init(
dsn=self.dsn,
integrations=[sentry_logging],
traces_sample_rate=self.trace_sample_rate,
environment=self.environment
)

def get_config(self) -> dict[str, str]:
return {
"dsn": self.dsn,
"level": self.level,
"event_level": self.event_level,
"trace_sample_rate": self.trace_sample_rate
}
16 changes: 16 additions & 0 deletions fai-rag-app/fai-backend/fai_backend/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from fai_backend.projects.schema import ProjectMember, ProjectRole
from fai_backend.repositories import ConversationDocument, PinCodeModel, ProjectModel, projects_repo
from fai_backend.assistant.models import AssistantTemplate, AssistantChatHistoryModel
from fai_backend.sentry.watcher import Watcher


def use_route_names_as_operation_ids(app: FastAPI) -> None:
Expand Down Expand Up @@ -91,3 +92,18 @@ async def setup_db():
database=client[settings.MONGO_DB_NAME],
document_models=[ProjectModel, PinCodeModel, ConversationDocument, AssistantChatHistoryModel],
)


async def setup_sentry():
if not settings.SENTRY_ENABLED:
return

sentry_logger = Watcher(
dsn=settings.SENTRY_DSN.get_secret_value(),
environment=settings.SENTRY_ENVIRONMENT,
level=settings.SENTRY_LOGGING_LEVEL,
event_level=settings.SENTRY_EVENT_LEVEL,
trace_sample_rate=settings.SENTRY_TRACE_SAMPLE_RATE
)

sentry_logger.initialize()
51 changes: 50 additions & 1 deletion fai-rag-app/fai-backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fai-rag-app/fai-backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ langstream = "~0.3.1"
openai = "~1.33.0"
python-dotenv = "~1.0.1"
sse-starlette = "~2.1.0"
sentry-sdk = "1.31.0"

[tool.poetry.group.unstructured.dependencies]
unstructured = { extras = ["md", "pdf", "docx"], version = "0.13.7" }
Expand Down

0 comments on commit 0941368

Please sign in to comment.