<참고> https://velog.io/@bluesyblues/sentry-%EC%A0%81%EC%9A%A9-%EA%B3%BC%EC%A0%95
Sentry, Python/FastAPI 에러 로그 모니터링
글. 수알치 오상문
Sentry는 단순히 Python 프로그램이나 FastAPI 프레임워크 뿐만 아니라 다양한 언어/프레임워크 환경에서 발생되는 에러 로그를 모니터링 사이트에 전달해준다.
1. 기본 예제 따라하기
Sentry는 에러 로그 처리 및 모니터링을 지원한다.
(1) https://sentry.io/ 사이트에 가입한다.
홈 페이지 우측상단 'GET STARTED' 클릭하고 가입 진행한다.
(2) https://sentry.io/onboarding/101story/select-platform/
원하는 플랫폼을 선택하고(예제에서는 PYHTON 선택), 우측하단 'Create Project' 단추를 클릭한다.
(3) 'Slack' 선택하고 'Next' 클릭한다.
(4) 화면에 나오는 것처럼 패키지를 설치한다.
pip3 install --upgrade sentry-sdk
(5) 예제 파이썬 파일을 만든다. Sentry_test.py
dsn 부분이 모니터링 정보를 받을 서버이다. (예제에서 복사한 것을 사용하면 된다.)
import sentry_sdk
sentry_sdk.init(
dsn="https://153154c4143945fea67b61069c1f84f@o1299114.ingest.sentry.io/6531321",
# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production.
traces_sample_rate=1.0
)
division_by_zero = 1 / 0 # 에러 유발 코드
(6) 예제를 실행하면 에러가 발생될 것이다.
ZeroDivisionError: division by zero
(7) 아래 사이트에 접속해보자.
https://sentry.io/organizations/101story/issues/
에러가 모니터링 되고 있음을 확인할 수 있다.
2. FastAPI에서 사용하기
아래 코드는 FastAPI 적용 코드 예제이다. dsn 값은 sentry > settings > project > client keys(DSN) 참조한다.
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
import sentry_sdk
sentry_sdk.init(
dsn="dsn",
environment="dev"
)
app = FastAPI()
app.add_middleware(SentryAsgiMiddleware)
import sentry_sdk
try:
# 0으로 나누기 에러 시도...
a = 10/0
except Exception as ex:
sentry_sdk.capture_message(str(ex))
[주의] python 3.7 미만은 aiocontextvars 설치 필요
[참고] https://docs.sentry.io/platforms/python/
Python
On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application. Get started using a g
docs.sentry.io
[참고] https://sentry.io/welcome/
Application Monitoring and Error Tracking Software
Self-hosted and cloud-based application monitoring that helps software teams see clearer, solve quicker, & learn continuously.
sentry.io
'Python 활용' 카테고리의 다른 글
Python, MySQL POOL connection 예제 (0) | 2022.06.24 |
---|---|
Python, MySQL Connector 다운로드 공식 사이트 (0) | 2022.06.24 |
python, 특정 함수 주기적 실행 예제 2 (0) | 2022.06.19 |
cx_Oracle 예제 깃허브 (0) | 2022.06.19 |
python, 특정 함수 주기적 실행 예제 1 (0) | 2022.06.19 |