Skip to content

Commit 61424de

Browse files
authored
add sentry (#2786)
* add sentry * nit * nit * add requirement to ee * try to ensure sentry is installed in integration tests
1 parent 4c2cf8b commit 61424de

File tree

17 files changed

+2460
-384
lines changed

17 files changed

+2460
-384
lines changed

backend/danswer/background/celery/celery_app.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from typing import Any
55

66
import redis
7+
import sentry_sdk
78
from celery import bootsteps # type: ignore
89
from celery import Celery
910
from celery import current_task
@@ -16,6 +17,7 @@
1617
from celery.signals import worker_shutdown
1718
from celery.states import READY_STATES
1819
from celery.utils.log import get_task_logger
20+
from sentry_sdk.integrations.celery import CeleryIntegration
1921

2022
from danswer.background.celery.celery_redis import RedisConnectorCredentialPair
2123
from danswer.background.celery.celery_redis import RedisConnectorDeletion
@@ -36,12 +38,24 @@
3638
from danswer.utils.logger import ColoredFormatter
3739
from danswer.utils.logger import PlainFormatter
3840
from danswer.utils.logger import setup_logger
41+
from shared_configs.configs import SENTRY_DSN
3942

4043
logger = setup_logger()
4144

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

48+
if SENTRY_DSN:
49+
sentry_sdk.init(
50+
dsn=SENTRY_DSN,
51+
integrations=[CeleryIntegration()],
52+
traces_sample_rate=0.5,
53+
)
54+
logger.info("Sentry initialized")
55+
else:
56+
logger.debug("Sentry DSN not provided, skipping Sentry initialization")
57+
58+
4559
celery_app = Celery(__name__)
4660
celery_app.config_from_object(
4761
"danswer.background.celery.celeryconfig"

backend/danswer/main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any
66
from typing import cast
77

8+
import sentry_sdk
89
import uvicorn
910
from fastapi import APIRouter
1011
from fastapi import FastAPI
@@ -15,6 +16,8 @@
1516
from fastapi.middleware.cors import CORSMiddleware
1617
from fastapi.responses import JSONResponse
1718
from httpx_oauth.clients.google import GoogleOAuth2
19+
from sentry_sdk.integrations.fastapi import FastApiIntegration
20+
from sentry_sdk.integrations.starlette import StarletteIntegration
1821
from sqlalchemy.orm import Session
1922

2023
from danswer import __version__
@@ -89,6 +92,7 @@
8992
from danswer.utils.variable_functionality import global_version
9093
from danswer.utils.variable_functionality import set_is_ee_based_on_env_variable
9194
from shared_configs.configs import CORS_ALLOWED_ORIGIN
95+
from shared_configs.configs import SENTRY_DSN
9296

9397
logger = setup_logger()
9498

@@ -201,6 +205,15 @@ def get_application() -> FastAPI:
201205
application = FastAPI(
202206
title="Danswer Backend", version=__version__, lifespan=lifespan
203207
)
208+
if SENTRY_DSN:
209+
sentry_sdk.init(
210+
dsn=SENTRY_DSN,
211+
integrations=[StarletteIntegration(), FastApiIntegration()],
212+
traces_sample_rate=0.5,
213+
)
214+
logger.info("Sentry initialized")
215+
else:
216+
logger.debug("Sentry DSN not provided, skipping Sentry initialization")
204217

205218
# Add the custom exception handler
206219
application.add_exception_handler(status.HTTP_400_BAD_REQUEST, log_http_error)

backend/model_server/main.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
from contextlib import asynccontextmanager
55
from pathlib import Path
66

7+
import sentry_sdk
78
import torch
89
import uvicorn
910
from fastapi import FastAPI
11+
from sentry_sdk.integrations.fastapi import FastApiIntegration
12+
from sentry_sdk.integrations.starlette import StarletteIntegration
1013
from transformers import logging as transformer_logging # type:ignore
1114

1215
from danswer import __version__
@@ -19,6 +22,7 @@
1922
from shared_configs.configs import MIN_THREADS_ML_MODELS
2023
from shared_configs.configs import MODEL_SERVER_ALLOWED_HOST
2124
from shared_configs.configs import MODEL_SERVER_PORT
25+
from shared_configs.configs import SENTRY_DSN
2226

2327
os.environ["TOKENIZERS_PARALLELISM"] = "false"
2428
os.environ["HF_HUB_DISABLE_TELEMETRY"] = "1"
@@ -81,6 +85,15 @@ def get_model_app() -> FastAPI:
8185
application = FastAPI(
8286
title="Danswer Model Server", version=__version__, lifespan=lifespan
8387
)
88+
if SENTRY_DSN:
89+
sentry_sdk.init(
90+
dsn=SENTRY_DSN,
91+
integrations=[StarletteIntegration(), FastApiIntegration()],
92+
traces_sample_rate=0.5,
93+
)
94+
logger.info("Sentry initialized")
95+
else:
96+
logger.debug("Sentry DSN not provided, skipping Sentry initialization")
8497

8598
application.include_router(management_router)
8699
application.include_router(encoders_router)

backend/requirements/default.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ dropbox==11.36.2
8181
boto3-stubs[s3]==1.34.133
8282
ultimate_sitemap_parser==0.5
8383
stripe==10.12.0
84-
mistune==0.8.4
84+
mistune==0.8.4
85+
sentry-sdk==2.14.0

backend/requirements/ee.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
python3-saml==1.15.0
1+
python3-saml==1.15.0

backend/requirements/model_server.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ torch==2.2.0
1212
transformers==4.39.2
1313
uvicorn==0.21.1
1414
voyageai==0.2.3
15-
litellm==1.49.5
15+
litellm==1.49.5
16+
sentry-sdk[fastapi,celery,starlette]==2.14.0

backend/shared_configs/configs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
os.environ.get("STRICT_CHUNK_TOKEN_LIMIT", "").lower() == "true"
7272
)
7373

74+
# Set up Sentry integration (for error logging)
75+
SENTRY_DSN = os.environ.get("SENTRY_DSN")
76+
77+
7478
# Fields which should only be set on new search setting
7579
PRESERVED_SEARCH_FIELDS = [
7680
"id",

deployment/docker_compose/docker-compose.dev.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ services:
9090
- LOG_POSTGRES_CONN_COUNTS=${LOG_POSTGRES_CONN_COUNTS:-}
9191
- CELERY_BROKER_POOL_LIMIT=${CELERY_BROKER_POOL_LIMIT:-}
9292

93+
# Analytics Configs
94+
- SENTRY_DSN=${SENTRY_DSN:-}
95+
9396
# Chat Configs
9497
- HARD_DELETE_CHATS=${HARD_DELETE_CHATS:-}
9598

@@ -197,6 +200,9 @@ services:
197200
- LOG_DANSWER_MODEL_INTERACTIONS=${LOG_DANSWER_MODEL_INTERACTIONS:-}
198201
- LOG_VESPA_TIMING_INFORMATION=${LOG_VESPA_TIMING_INFORMATION:-}
199202

203+
# Analytics Configs
204+
- SENTRY_DSN=${SENTRY_DSN:-}
205+
200206
# Enterprise Edition stuff
201207
- ENABLE_PAID_ENTERPRISE_EDITION_FEATURES=${ENABLE_PAID_ENTERPRISE_EDITION_FEATURES:-false}
202208
extra_hosts:
@@ -254,6 +260,9 @@ services:
254260
- MIN_THREADS_ML_MODELS=${MIN_THREADS_ML_MODELS:-}
255261
# Set to debug to get more fine-grained logs
256262
- LOG_LEVEL=${LOG_LEVEL:-info}
263+
264+
# Analytics Configs
265+
- SENTRY_DSN=${SENTRY_DSN:-}
257266
volumes:
258267
# Not necessary, this is just to reduce download time during startup
259268
- model_cache_huggingface:/root/.cache/huggingface/
@@ -283,6 +292,9 @@ services:
283292
# Set to debug to get more fine-grained logs
284293
- LOG_LEVEL=${LOG_LEVEL:-info}
285294
- CLIENT_EMBEDDING_TIMEOUT=${CLIENT_EMBEDDING_TIMEOUT:-}
295+
296+
# Analytics Configs
297+
- SENTRY_DSN=${SENTRY_DSN:-}
286298
volumes:
287299
# Not necessary, this is just to reduce download time during startup
288300
- indexing_huggingface_model_cache:/root/.cache/huggingface/

web/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2-
2+
.env.sentry-build-plugin
33
# dependencies
44
/node_modules
55
/.pnp

web/instrumentation.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export async function register() {
2+
if (process.env.NEXT_PUBLIC_SENTRY_DSN) {
3+
if (process.env.NEXT_RUNTIME === "nodejs") {
4+
await import("./sentry.client.config");
5+
await import("./sentry.server.config");
6+
}
7+
8+
if (process.env.NEXT_RUNTIME === "edge") {
9+
await import("./sentry.edge.config");
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)