Skip to content

Commit

Permalink
add sentry
Browse files Browse the repository at this point in the history
  • Loading branch information
pablonyx committed Oct 15, 2024
1 parent efe2e79 commit 1e582f1
Show file tree
Hide file tree
Showing 15 changed files with 2,454 additions and 381 deletions.
14 changes: 14 additions & 0 deletions backend/danswer/background/celery/celery_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any

import redis
import sentry_sdk
from celery import bootsteps # type: ignore
from celery import Celery
from celery import current_task
Expand All @@ -16,6 +17,7 @@
from celery.signals import worker_shutdown
from celery.states import READY_STATES
from celery.utils.log import get_task_logger
from sentry_sdk.integrations.celery import CeleryIntegration

from danswer.background.celery.celery_redis import RedisConnectorCredentialPair
from danswer.background.celery.celery_redis import RedisConnectorDeletion
Expand All @@ -36,12 +38,24 @@
from danswer.utils.logger import ColoredFormatter
from danswer.utils.logger import PlainFormatter
from danswer.utils.logger import setup_logger
from shared_configs.configs import SENTRY_DSN

logger = setup_logger()

# use this within celery tasks to get celery task specific logging
task_logger = get_task_logger(__name__)

if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[CeleryIntegration()],
traces_sample_rate=0.5,
)
logger.info("Sentry initialized")
else:
logger.debug("Sentry DSN not provided, skipping Sentry initialization")


celery_app = Celery(__name__)
celery_app.config_from_object(
"danswer.background.celery.celeryconfig"
Expand Down
13 changes: 13 additions & 0 deletions backend/danswer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Any
from typing import cast

import sentry_sdk
import uvicorn
from fastapi import APIRouter
from fastapi import FastAPI
Expand All @@ -15,6 +16,8 @@
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from httpx_oauth.clients.google import GoogleOAuth2
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
from sqlalchemy.orm import Session

from danswer import __version__
Expand Down Expand Up @@ -90,6 +93,7 @@
from danswer.utils.variable_functionality import global_version
from danswer.utils.variable_functionality import set_is_ee_based_on_env_variable
from shared_configs.configs import CORS_ALLOWED_ORIGIN
from shared_configs.configs import SENTRY_DSN

logger = setup_logger()

Expand Down Expand Up @@ -204,6 +208,15 @@ def get_application() -> FastAPI:
application = FastAPI(
title="Danswer Backend", version=__version__, lifespan=lifespan
)
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=1.0,
)
logger.info("Sentry initialized")
else:
logger.debug("Sentry DSN not provided, skipping Sentry initialization")

# Add the custom exception handler
application.add_exception_handler(status.HTTP_400_BAD_REQUEST, log_http_error)
Expand Down
13 changes: 13 additions & 0 deletions backend/model_server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
from contextlib import asynccontextmanager
from pathlib import Path

import sentry_sdk
import torch
import uvicorn
from fastapi import FastAPI
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
from transformers import logging as transformer_logging # type:ignore

from danswer import __version__
Expand All @@ -19,6 +22,7 @@
from shared_configs.configs import MIN_THREADS_ML_MODELS
from shared_configs.configs import MODEL_SERVER_ALLOWED_HOST
from shared_configs.configs import MODEL_SERVER_PORT
from shared_configs.configs import SENTRY_DSN

os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
Expand Down Expand Up @@ -81,6 +85,15 @@ def get_model_app() -> FastAPI:
application = FastAPI(
title="Danswer Model Server", version=__version__, lifespan=lifespan
)
if SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=[StarletteIntegration(), FastApiIntegration()],
traces_sample_rate=0.5,
)
logger.info("Sentry initialized")
else:
logger.debug("Sentry DSN not provided, skipping Sentry initialization")

application.include_router(management_router)
application.include_router(encoders_router)
Expand Down
1 change: 1 addition & 0 deletions backend/requirements/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,4 @@ dropbox==11.36.2
boto3-stubs[s3]==1.34.133
ultimate_sitemap_parser==0.5
stripe==10.12.0
sentry-sdk[fastapi,celery,starlette]==2.14.0
4 changes: 4 additions & 0 deletions backend/shared_configs/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
OPENAI_EMBEDDING_TIMEOUT = int(os.environ.get("OPENAI_EMBEDDING_TIMEOUT", "600"))


# Set up Sentry integration (for error logging)
SENTRY_DSN = os.environ.get("SENTRY_DSN")


# Fields which should only be set on new search setting
PRESERVED_SEARCH_FIELDS = [
"id",
Expand Down
12 changes: 12 additions & 0 deletions deployment/docker_compose/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ services:
- LOG_POSTGRES_CONN_COUNTS=${LOG_POSTGRES_CONN_COUNTS:-}
- CELERY_BROKER_POOL_LIMIT=${CELERY_BROKER_POOL_LIMIT:-}

# Analytics Configs
- SENTRY_DSN=${SENTRY_DSN:-}

# Chat Configs
- HARD_DELETE_CHATS=${HARD_DELETE_CHATS:-}

Expand Down Expand Up @@ -197,6 +200,9 @@ services:
- LOG_DANSWER_MODEL_INTERACTIONS=${LOG_DANSWER_MODEL_INTERACTIONS:-}
- LOG_VESPA_TIMING_INFORMATION=${LOG_VESPA_TIMING_INFORMATION:-}

# Analytics Configs
- SENTRY_DSN=${SENTRY_DSN:-}

# Enterprise Edition stuff
- ENABLE_PAID_ENTERPRISE_EDITION_FEATURES=${ENABLE_PAID_ENTERPRISE_EDITION_FEATURES:-false}
extra_hosts:
Expand Down Expand Up @@ -254,6 +260,9 @@ services:
- MIN_THREADS_ML_MODELS=${MIN_THREADS_ML_MODELS:-}
# Set to debug to get more fine-grained logs
- LOG_LEVEL=${LOG_LEVEL:-info}

# Analytics Configs
- SENTRY_DSN=${SENTRY_DSN:-}
volumes:
# Not necessary, this is just to reduce download time during startup
- model_cache_huggingface:/root/.cache/huggingface/
Expand Down Expand Up @@ -283,6 +292,9 @@ services:
# Set to debug to get more fine-grained logs
- LOG_LEVEL=${LOG_LEVEL:-info}
- CLIENT_EMBEDDING_TIMEOUT=${CLIENT_EMBEDDING_TIMEOUT:-}

# Analytics Configs
- SENTRY_DSN=${SENTRY_DSN:-}
volumes:
# Not necessary, this is just to reduce download time during startup
- indexing_huggingface_model_cache:/root/.cache/huggingface/
Expand Down
2 changes: 1 addition & 1 deletion web/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.env.sentry-build-plugin
# dependencies
/node_modules
/.pnp
Expand Down
10 changes: 10 additions & 0 deletions web/instrumentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export async function register() {
if (process.env.NEXT_RUNTIME === "nodejs") {
await import("./sentry.client.config");
await import("./sentry.server.config");
}

if (process.env.NEXT_RUNTIME === "edge") {
await import("./sentry.edge.config");
}
}
12 changes: 11 additions & 1 deletion web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,14 @@ const nextConfig = {
},
};

module.exports = nextConfig;
const { withSentryConfig } = require("@sentry/nextjs");

module.exports = withSentryConfig(nextConfig, {
org: "danswer",
project: "javascript-nextjs",

// An auth token is required for uploading source maps.
authToken: process.env.SENTRY_AUTH_TOKEN,

silent: false, // Can be used to suppress logs
});
Loading

0 comments on commit 1e582f1

Please sign in to comment.