Skip to content

Commit

Permalink
Merge pull request #28 from wednesday-solutions/feat/sentry
Browse files Browse the repository at this point in the history
feature - Sentry integration
  • Loading branch information
himanshu-wedensday authored Mar 14, 2024
2 parents bbf623f + 6f30f8c commit 36519cb
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 27 deletions.
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

1 comment on commit 36519cb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
app
   app.py312423%10–61
app/config
   __init__.py2150%4
   base.py31390%42–44
   celery_config.py16160%1–25
   celery_utils.py20200%1–28
   redis_config.py4325%2–6
app/constants
   jwt_utils.py15150%1–19
app/daos
   home.py880%1–14
   users.py63630%1–115
app/middlewares
   cache_middleware.py50500%1–64
   rate_limiter_middleware.py24240%1–32
   request_id_injection.py16160%1–23
app/models
   __init__.py220%11–13
   users.py25250%1–35
app/routes
   __init__.py11110%1–13
app/routes/cache_router
   __init__.py220%1–3
   cache_samples.py11110%1–15
app/routes/celery_router
   __init__.py220%1–3
   celery_samples.py11110%1–14
app/routes/home
   __init__.py220%1–3
   home.py30300%1–40
app/routes/users
   __init__.py220%1–3
   users.py38380%1–59
app/schemas/users
   users_request.py41410%1–70
   users_response.py880%1–11
app/sessions
   db.py50500%1–75
app/tests
   test_basic.py151313%5–27
app/utils
   exception_handler.py16160%1–32
   redis_utils.py330%1–5
   slack_notification_utils.py13130%1–29
   user_utils.py23230%1–32
app/wrappers
   cache_wrappers.py17170%1–24
TOTAL6025636% 

Tests Skipped Failures Errors Time
1 0 💤 0 ❌ 1 🔥 0.690s ⏱️

Please sign in to comment.