Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature - Sentry integration #28

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ OPENAI_API_KEY_GPT4=
OPENAI_API_KEY_WEDNESDAY=
SECRET_KEY=
REDIS_URL=
SENTRY_DSN=
SLACK_WEBHOOK_URL=
19 changes: 17 additions & 2 deletions app/app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
import sentry_sdk

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi_pagination import add_pagination
from fastapi.exceptions import HTTPException, RequestValidationError
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware

from app.config.base import cached_endpoints, settings
from app.config.celery_utils import create_celery
from app.middlewares.cache_middleware import CacheMiddleware
from app.middlewares.rate_limiter_middleware import RateLimitMiddleware
from app.middlewares.request_id_injection import RequestIdInjection

from app.routes import api_router
from app.utils.exception_handler import exception_handler, validation_exception_handler, http_exception_handler


# Sentry Initialization
if settings.SENTRY_DSN:
sentry_sdk.init(
dsn=settings.SENTRY_DSN,
traces_sample_rate=1.0, # Sample rate of 100%
)

app = FastAPI(
title="FastAPI Template",
description="This is my first API use FastAPI",
Expand All @@ -22,7 +32,7 @@
celery = create_celery()
origins = settings.ALLOWED_HOSTS

# CORS middleware
# Middlewares
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
Expand All @@ -34,6 +44,11 @@
app.add_middleware(RequestIdInjection)
app.add_middleware(CacheMiddleware, cached_endpoints=cached_endpoints.CACHED_ENDPOINTS)

try:
app.add_middleware(SentryAsgiMiddleware)
except Exception as e:
print(f"Error while adding Sentry Middleware: {e}")

# Include the routers
app.include_router(api_router, prefix="/api")

Expand Down
1 change: 1 addition & 0 deletions app/config/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Config:
class Settings(BaseSettings):
SECRET_KEY: str
REDIS_URL: str
SENTRY_DSN: str
SLACK_WEBHOOK_URL: str
ALLOWED_HOSTS: list = ["*"]
CACHE_MAX_AGE: int = 60
Expand Down
8 changes: 8 additions & 0 deletions app/routes/home/home.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dependencies import circuit_breaker
from fastapi import APIRouter, HTTPException
from fastapi.responses import JSONResponse
from app.config.base import settings
from app.middlewares.request_id_injection import request_id_contextvar
from app.daos.home import external_service_call
from pybreaker import CircuitBreakerError
Expand All @@ -27,6 +28,13 @@ async def external_service_endpoint():
raise HTTPException(status_code=500, detail=str(e))


@home_router.get("/sentry-test", tags=["Home"])
def sentry_endpoint():
if not settings.SENTRY_DSN:
raise HTTPException(status_code=503, detail="Sentry DSN not found")
raise Exception("Testing Sentry")


@home_router.get("/{path:path}", include_in_schema=False)
async def catch_all(path: str):
return JSONResponse(status_code=404, content={"success": False, "message": f"Route not found for path: {path}"})
51 changes: 26 additions & 25 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
aiohttp==3.8.4
aiohttp>3.4
aiosignal==1.3.1
alembic>1
anyio==3.7.0
argilla>1.1
async-timeout==4.0.2
attrs==23.1.0
backoff==2.2.1
black
celery>=5.3.6
certifi==2023.5.7
cffi==1.15.1
chardet==5.1.0
charset-normalizer==3.1.0
click==8.1.3
clickhouse-connect==0.5.25
coloredlogs==15.0.1
contextvars
coverage==7.2.7
cryptography==40.0.2
dataclasses-json==0.5.7
Deprecated==1.2.14
email_validator
et-xmlfile==1.1.0
fastapi-pagination
fastapi==0.95.2
flake8
flatbuffers==23.5.26
frozenlist==1.3.3
ftfy==6.1.1
greenlet==2.0.2
h11
hiredis
httpcore
httptools==0.5.0
httpx
humanfriendly==10.0
idna==3.4
iniconfig==2.0.0
itsdangerous
joblib==1.2.0
langchain
lxml==4.9.2
lz4==4.3.2
Markdown==3.4.3
marshmallow==3.19.0
marshmallow-enum==1.5.1
marshmallow==3.19.0
monotonic==1.6
mpmath==1.3.0
msg-parser==1.2.0
Expand All @@ -48,27 +57,38 @@ openapi-schema-pydantic==1.2.4
openpyxl==3.1.2
overrides==7.3.1
packaging==23.1
pdf2image
pdfminer.six==20221105
Pillow>=9.0
pluggy==1.0.0
posthog==3.0.1
pre-commit
protobuf==4.23.2
pybreaker
pycparser==2.21
pydantic==1.10.8
pyjwt
pylint
PyMySQL==1.0.3
pypandoc==1.11
pytest==7.3.1
pytest-asyncio
pytest-cov
pytest-sqlalchemy-mock==0.1.5
pytest==7.3.1
pytest_mock_resources
python-dateutil==2.8.2
python-docx==0.8.11
python-dotenv==1.0.0
python-magic==0.4.15
python-pptx==0.6.21
python_on_whales
pytz==2023.3
PyYAML==6.0
redis>4
regex==2023.5.5
requests==2.31.0
rfc3986==1.5.0
sentry-sdk
six==1.16.0
sniffio==1.3.0
SQLAlchemy==2.0.15
Expand All @@ -81,35 +101,16 @@ tokenizers==0.13.3
tqdm==4.65.0
typing-inspect==0.9.0
typing_extensions==4.6.2
# unstructured==0.6.11
unstructured==0.6.11
urllib3==2.0.2
uvicorn==0.22.0
uvloop==0.17.0
watchfiles==0.19.0
wcwidth==0.2.6
websockets==11.0.3
werkzeug
wrapt==1.14.0
xlrd==2.0.1
XlsxWriter==3.1.2
yarl==1.9.2
zstandard==0.21.0
pytest_mock_resources
python_on_whales
pylint
pyjwt
itsdangerous
pdf2image
alembic
email-validator
werkzeug
fastapi-pagination
flake8
black
pre-commit
pybreaker
pytest-asyncio
contextvars
hiredis
redis>4
celery>=5.3.6
pytest-cov
zstandard==0.21.0
Loading