From 7d1e7ce545c4986b2056c094e65610cf40659d81 Mon Sep 17 00:00:00 2001 From: jasonxie Date: Thu, 12 Oct 2023 21:32:43 +0800 Subject: [PATCH] feat: add sentry integration --- poetry.lock | 76 +++++++++++++++++++++++++++++++- pyproject.toml | 1 + server/app/blog/graph/queries.py | 1 + server/settings.py | 25 ++++++++++- 4 files changed, 101 insertions(+), 2 deletions(-) diff --git a/poetry.lock b/poetry.lock index 5f66ced..bb4ffda 100644 --- a/poetry.lock +++ b/poetry.lock @@ -161,6 +161,17 @@ d = ["aiohttp (>=3.7.4)"] jupyter = ["ipython (>=7.8.0)", "tokenize-rt (>=3.2.0)"] uvloop = ["uvloop (>=0.15.2)"] +[[package]] +name = "certifi" +version = "2023.7.22" +description = "Python package for providing Mozilla's CA Bundle." +optional = false +python-versions = ">=3.6" +files = [ + {file = "certifi-2023.7.22-py3-none-any.whl", hash = "sha256:92d6037539857d8206b8f6ae472e8b77db8058fec5937a1ef3f54304089edbb9"}, + {file = "certifi-2023.7.22.tar.gz", hash = "sha256:539cc1d13202e33ca466e88b2807e29f4c13049d6d87031a3c110744495cb082"}, +] + [[package]] name = "cffi" version = "1.16.0" @@ -958,6 +969,52 @@ files = [ {file = "ruff-0.0.291.tar.gz", hash = "sha256:c61109661dde9db73469d14a82b42a88c7164f731e6a3b0042e71394c1c7ceed"}, ] +[[package]] +name = "sentry-sdk" +version = "1.32.0" +description = "Python client for Sentry (https://sentry.io)" +optional = false +python-versions = "*" +files = [ + {file = "sentry-sdk-1.32.0.tar.gz", hash = "sha256:935e8fbd7787a3702457393b74b13d89a5afb67185bc0af85c00cb27cbd42e7c"}, + {file = "sentry_sdk-1.32.0-py2.py3-none-any.whl", hash = "sha256:eeb0b3550536f3bbc05bb1c7e0feb3a78d74acb43b607159a606ed2ec0a33a4d"}, +] + +[package.dependencies] +certifi = "*" +django = {version = ">=1.8", optional = true, markers = "extra == \"django\""} +urllib3 = {version = ">=1.26.11", markers = "python_version >= \"3.6\""} + +[package.extras] +aiohttp = ["aiohttp (>=3.5)"] +arq = ["arq (>=0.23)"] +asyncpg = ["asyncpg (>=0.23)"] +beam = ["apache-beam (>=2.12)"] +bottle = ["bottle (>=0.12.13)"] +celery = ["celery (>=3)"] +chalice = ["chalice (>=1.16.0)"] +clickhouse-driver = ["clickhouse-driver (>=0.2.0)"] +django = ["django (>=1.8)"] +falcon = ["falcon (>=1.4)"] +fastapi = ["fastapi (>=0.79.0)"] +flask = ["blinker (>=1.1)", "flask (>=0.11)", "markupsafe"] +grpcio = ["grpcio (>=1.21.1)"] +httpx = ["httpx (>=0.16.0)"] +huey = ["huey (>=2)"] +loguru = ["loguru (>=0.5)"] +opentelemetry = ["opentelemetry-distro (>=0.35b0)"] +opentelemetry-experimental = ["opentelemetry-distro (>=0.40b0,<1.0)", "opentelemetry-instrumentation-aiohttp-client (>=0.40b0,<1.0)", "opentelemetry-instrumentation-django (>=0.40b0,<1.0)", "opentelemetry-instrumentation-fastapi (>=0.40b0,<1.0)", "opentelemetry-instrumentation-flask (>=0.40b0,<1.0)", "opentelemetry-instrumentation-requests (>=0.40b0,<1.0)", "opentelemetry-instrumentation-sqlite3 (>=0.40b0,<1.0)", "opentelemetry-instrumentation-urllib (>=0.40b0,<1.0)"] +pure-eval = ["asttokens", "executing", "pure-eval"] +pymongo = ["pymongo (>=3.1)"] +pyspark = ["pyspark (>=2.4.4)"] +quart = ["blinker (>=1.1)", "quart (>=0.16.1)"] +rq = ["rq (>=0.6)"] +sanic = ["sanic (>=0.8)"] +sqlalchemy = ["sqlalchemy (>=1.2)"] +starlette = ["starlette (>=0.19.1)"] +starlite = ["starlite (>=1.48)"] +tornado = ["tornado (>=5)"] + [[package]] name = "service-identity" version = "23.1.0" @@ -1219,6 +1276,23 @@ files = [ {file = "tzdata-2023.3.tar.gz", hash = "sha256:11ef1e08e54acb0d4f95bdb1be05da659673de4acbd21bf9c69e94cc5e907a3a"}, ] +[[package]] +name = "urllib3" +version = "2.0.6" +description = "HTTP library with thread-safe connection pooling, file post, and more." +optional = false +python-versions = ">=3.7" +files = [ + {file = "urllib3-2.0.6-py3-none-any.whl", hash = "sha256:7a7c7003b000adf9e7ca2a377c9688bbc54ed41b985789ed576570342a375cd2"}, + {file = "urllib3-2.0.6.tar.gz", hash = "sha256:b19e1a85d206b56d7df1d5e683df4a7725252a964e3993648dd0fb5a1c157564"}, +] + +[package.extras] +brotli = ["brotli (>=1.0.9)", "brotlicffi (>=0.8.0)"] +secure = ["certifi", "cryptography (>=1.9)", "idna (>=2.0.0)", "pyopenssl (>=17.1.0)", "urllib3-secure-extra"] +socks = ["pysocks (>=1.5.6,!=1.5.7,<2.0)"] +zstd = ["zstandard (>=0.18.0)"] + [[package]] name = "wcwidth" version = "0.2.6" @@ -1303,4 +1377,4 @@ testing = ["coverage (>=5.0.3)", "zope.event", "zope.testing"] [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "21f17fc3669cea94959c728c8c8d86b55eadfe165625f8c9c2cfafc446bcf8fb" +content-hash = "c9e17f5480aca895224a8d6daf237eef8bf8f6cfc01320f873387517606b7445" diff --git a/pyproject.toml b/pyproject.toml index a1e19f8..3bee8db 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ strawberry-graphql = {extras = ["channels"], version = "^0.209.6"} daphne = "^4.0.0" django-debug-toolbar = "^4.2.0" graphql-sync-dataloaders = "^0.1.1" +sentry-sdk = {extras = ["django"], version = "^1.32.0"} [tool.poetry.group.dev.dependencies] diff --git a/server/app/blog/graph/queries.py b/server/app/blog/graph/queries.py index 0107f70..42410a2 100644 --- a/server/app/blog/graph/queries.py +++ b/server/app/blog/graph/queries.py @@ -18,3 +18,4 @@ class Query: tags: list[blog_types.Tag] = strawberry_django.field() categories: list[blog_types.Category] = strawberry_django.field() comments: list[blog_types.Comment] = strawberry_django.field() + test_error: float = strawberry.field(resolver=lambda: 1 / 0) diff --git a/server/settings.py b/server/settings.py index 1b36da3..ee4b284 100644 --- a/server/settings.py +++ b/server/settings.py @@ -9,9 +9,13 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.2/ref/settings/ """ - +import os from pathlib import Path +import sentry_sdk +from sentry_sdk.integrations.django import DjangoIntegration +from sentry_sdk.integrations.strawberry import StrawberryIntegration + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -146,3 +150,22 @@ "127.0.0.1", "localhost", ] + +sentry_sdk.init( + dsn=os.environ.get("SENTRY_DSN"), + # Set traces_sample_rate to 1.0 to capture 100% + # of transactions for performance monitoring. + traces_sample_rate=1.0, + # Set profiles_sample_rate to 1.0 to profile 100% + # of sampled transactions. + # We recommend adjusting this value in production. + profiles_sample_rate=1.0, + integrations=[ + # Set async_execution to True if you have at least one async resolver + StrawberryIntegration(async_execution=False), + DjangoIntegration( + transaction_style="url", + ), + ], + send_default_pii=True, +)