sentry-sdk 0.7.5__py2.py3-none-any.whl → 2.46.0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sentry_sdk/__init__.py +48 -30
- sentry_sdk/_compat.py +74 -61
- sentry_sdk/_init_implementation.py +84 -0
- sentry_sdk/_log_batcher.py +172 -0
- sentry_sdk/_lru_cache.py +47 -0
- sentry_sdk/_metrics_batcher.py +167 -0
- sentry_sdk/_queue.py +289 -0
- sentry_sdk/_types.py +338 -0
- sentry_sdk/_werkzeug.py +98 -0
- sentry_sdk/ai/__init__.py +7 -0
- sentry_sdk/ai/monitoring.py +137 -0
- sentry_sdk/ai/utils.py +144 -0
- sentry_sdk/api.py +496 -80
- sentry_sdk/attachments.py +75 -0
- sentry_sdk/client.py +1023 -103
- sentry_sdk/consts.py +1438 -66
- sentry_sdk/crons/__init__.py +10 -0
- sentry_sdk/crons/api.py +62 -0
- sentry_sdk/crons/consts.py +4 -0
- sentry_sdk/crons/decorator.py +135 -0
- sentry_sdk/debug.py +15 -14
- sentry_sdk/envelope.py +369 -0
- sentry_sdk/feature_flags.py +71 -0
- sentry_sdk/hub.py +611 -280
- sentry_sdk/integrations/__init__.py +276 -49
- sentry_sdk/integrations/_asgi_common.py +108 -0
- sentry_sdk/integrations/_wsgi_common.py +180 -44
- sentry_sdk/integrations/aiohttp.py +291 -42
- sentry_sdk/integrations/anthropic.py +439 -0
- sentry_sdk/integrations/argv.py +9 -8
- sentry_sdk/integrations/ariadne.py +161 -0
- sentry_sdk/integrations/arq.py +247 -0
- sentry_sdk/integrations/asgi.py +341 -0
- sentry_sdk/integrations/asyncio.py +144 -0
- sentry_sdk/integrations/asyncpg.py +208 -0
- sentry_sdk/integrations/atexit.py +17 -10
- sentry_sdk/integrations/aws_lambda.py +377 -62
- sentry_sdk/integrations/beam.py +176 -0
- sentry_sdk/integrations/boto3.py +137 -0
- sentry_sdk/integrations/bottle.py +221 -0
- sentry_sdk/integrations/celery/__init__.py +529 -0
- sentry_sdk/integrations/celery/beat.py +293 -0
- sentry_sdk/integrations/celery/utils.py +43 -0
- sentry_sdk/integrations/chalice.py +134 -0
- sentry_sdk/integrations/clickhouse_driver.py +177 -0
- sentry_sdk/integrations/cloud_resource_context.py +280 -0
- sentry_sdk/integrations/cohere.py +274 -0
- sentry_sdk/integrations/dedupe.py +48 -14
- sentry_sdk/integrations/django/__init__.py +584 -191
- sentry_sdk/integrations/django/asgi.py +245 -0
- sentry_sdk/integrations/django/caching.py +204 -0
- sentry_sdk/integrations/django/middleware.py +187 -0
- sentry_sdk/integrations/django/signals_handlers.py +91 -0
- sentry_sdk/integrations/django/templates.py +79 -5
- sentry_sdk/integrations/django/transactions.py +49 -22
- sentry_sdk/integrations/django/views.py +96 -0
- sentry_sdk/integrations/dramatiq.py +226 -0
- sentry_sdk/integrations/excepthook.py +50 -13
- sentry_sdk/integrations/executing.py +67 -0
- sentry_sdk/integrations/falcon.py +272 -0
- sentry_sdk/integrations/fastapi.py +141 -0
- sentry_sdk/integrations/flask.py +142 -88
- sentry_sdk/integrations/gcp.py +239 -0
- sentry_sdk/integrations/gnu_backtrace.py +99 -0
- sentry_sdk/integrations/google_genai/__init__.py +301 -0
- sentry_sdk/integrations/google_genai/consts.py +16 -0
- sentry_sdk/integrations/google_genai/streaming.py +155 -0
- sentry_sdk/integrations/google_genai/utils.py +576 -0
- sentry_sdk/integrations/gql.py +162 -0
- sentry_sdk/integrations/graphene.py +151 -0
- sentry_sdk/integrations/grpc/__init__.py +168 -0
- sentry_sdk/integrations/grpc/aio/__init__.py +7 -0
- sentry_sdk/integrations/grpc/aio/client.py +95 -0
- sentry_sdk/integrations/grpc/aio/server.py +100 -0
- sentry_sdk/integrations/grpc/client.py +91 -0
- sentry_sdk/integrations/grpc/consts.py +1 -0
- sentry_sdk/integrations/grpc/server.py +66 -0
- sentry_sdk/integrations/httpx.py +178 -0
- sentry_sdk/integrations/huey.py +174 -0
- sentry_sdk/integrations/huggingface_hub.py +378 -0
- sentry_sdk/integrations/langchain.py +1132 -0
- sentry_sdk/integrations/langgraph.py +337 -0
- sentry_sdk/integrations/launchdarkly.py +61 -0
- sentry_sdk/integrations/litellm.py +287 -0
- sentry_sdk/integrations/litestar.py +315 -0
- sentry_sdk/integrations/logging.py +307 -96
- sentry_sdk/integrations/loguru.py +213 -0
- sentry_sdk/integrations/mcp.py +566 -0
- sentry_sdk/integrations/modules.py +14 -31
- sentry_sdk/integrations/openai.py +725 -0
- sentry_sdk/integrations/openai_agents/__init__.py +61 -0
- sentry_sdk/integrations/openai_agents/consts.py +1 -0
- sentry_sdk/integrations/openai_agents/patches/__init__.py +5 -0
- sentry_sdk/integrations/openai_agents/patches/agent_run.py +140 -0
- sentry_sdk/integrations/openai_agents/patches/error_tracing.py +77 -0
- sentry_sdk/integrations/openai_agents/patches/models.py +50 -0
- sentry_sdk/integrations/openai_agents/patches/runner.py +45 -0
- sentry_sdk/integrations/openai_agents/patches/tools.py +77 -0
- sentry_sdk/integrations/openai_agents/spans/__init__.py +5 -0
- sentry_sdk/integrations/openai_agents/spans/agent_workflow.py +21 -0
- sentry_sdk/integrations/openai_agents/spans/ai_client.py +42 -0
- sentry_sdk/integrations/openai_agents/spans/execute_tool.py +48 -0
- sentry_sdk/integrations/openai_agents/spans/handoff.py +19 -0
- sentry_sdk/integrations/openai_agents/spans/invoke_agent.py +86 -0
- sentry_sdk/integrations/openai_agents/utils.py +199 -0
- sentry_sdk/integrations/openfeature.py +35 -0
- sentry_sdk/integrations/opentelemetry/__init__.py +7 -0
- sentry_sdk/integrations/opentelemetry/consts.py +5 -0
- sentry_sdk/integrations/opentelemetry/integration.py +58 -0
- sentry_sdk/integrations/opentelemetry/propagator.py +117 -0
- sentry_sdk/integrations/opentelemetry/span_processor.py +391 -0
- sentry_sdk/integrations/otlp.py +82 -0
- sentry_sdk/integrations/pure_eval.py +141 -0
- sentry_sdk/integrations/pydantic_ai/__init__.py +47 -0
- sentry_sdk/integrations/pydantic_ai/consts.py +1 -0
- sentry_sdk/integrations/pydantic_ai/patches/__init__.py +4 -0
- sentry_sdk/integrations/pydantic_ai/patches/agent_run.py +215 -0
- sentry_sdk/integrations/pydantic_ai/patches/graph_nodes.py +110 -0
- sentry_sdk/integrations/pydantic_ai/patches/model_request.py +40 -0
- sentry_sdk/integrations/pydantic_ai/patches/tools.py +98 -0
- sentry_sdk/integrations/pydantic_ai/spans/__init__.py +3 -0
- sentry_sdk/integrations/pydantic_ai/spans/ai_client.py +246 -0
- sentry_sdk/integrations/pydantic_ai/spans/execute_tool.py +49 -0
- sentry_sdk/integrations/pydantic_ai/spans/invoke_agent.py +112 -0
- sentry_sdk/integrations/pydantic_ai/utils.py +223 -0
- sentry_sdk/integrations/pymongo.py +214 -0
- sentry_sdk/integrations/pyramid.py +112 -68
- sentry_sdk/integrations/quart.py +237 -0
- sentry_sdk/integrations/ray.py +165 -0
- sentry_sdk/integrations/redis/__init__.py +48 -0
- sentry_sdk/integrations/redis/_async_common.py +116 -0
- sentry_sdk/integrations/redis/_sync_common.py +119 -0
- sentry_sdk/integrations/redis/consts.py +19 -0
- sentry_sdk/integrations/redis/modules/__init__.py +0 -0
- sentry_sdk/integrations/redis/modules/caches.py +118 -0
- sentry_sdk/integrations/redis/modules/queries.py +65 -0
- sentry_sdk/integrations/redis/rb.py +32 -0
- sentry_sdk/integrations/redis/redis.py +69 -0
- sentry_sdk/integrations/redis/redis_cluster.py +107 -0
- sentry_sdk/integrations/redis/redis_py_cluster_legacy.py +50 -0
- sentry_sdk/integrations/redis/utils.py +148 -0
- sentry_sdk/integrations/rq.py +95 -37
- sentry_sdk/integrations/rust_tracing.py +284 -0
- sentry_sdk/integrations/sanic.py +294 -123
- sentry_sdk/integrations/serverless.py +48 -19
- sentry_sdk/integrations/socket.py +96 -0
- sentry_sdk/integrations/spark/__init__.py +4 -0
- sentry_sdk/integrations/spark/spark_driver.py +316 -0
- sentry_sdk/integrations/spark/spark_worker.py +116 -0
- sentry_sdk/integrations/sqlalchemy.py +142 -0
- sentry_sdk/integrations/starlette.py +737 -0
- sentry_sdk/integrations/starlite.py +292 -0
- sentry_sdk/integrations/statsig.py +37 -0
- sentry_sdk/integrations/stdlib.py +235 -29
- sentry_sdk/integrations/strawberry.py +394 -0
- sentry_sdk/integrations/sys_exit.py +70 -0
- sentry_sdk/integrations/threading.py +158 -28
- sentry_sdk/integrations/tornado.py +84 -52
- sentry_sdk/integrations/trytond.py +50 -0
- sentry_sdk/integrations/typer.py +60 -0
- sentry_sdk/integrations/unleash.py +33 -0
- sentry_sdk/integrations/unraisablehook.py +53 -0
- sentry_sdk/integrations/wsgi.py +201 -119
- sentry_sdk/logger.py +96 -0
- sentry_sdk/metrics.py +81 -0
- sentry_sdk/monitor.py +120 -0
- sentry_sdk/profiler/__init__.py +49 -0
- sentry_sdk/profiler/continuous_profiler.py +730 -0
- sentry_sdk/profiler/transaction_profiler.py +839 -0
- sentry_sdk/profiler/utils.py +195 -0
- sentry_sdk/py.typed +0 -0
- sentry_sdk/scope.py +1713 -85
- sentry_sdk/scrubber.py +177 -0
- sentry_sdk/serializer.py +405 -0
- sentry_sdk/session.py +177 -0
- sentry_sdk/sessions.py +275 -0
- sentry_sdk/spotlight.py +242 -0
- sentry_sdk/tracing.py +1486 -0
- sentry_sdk/tracing_utils.py +1236 -0
- sentry_sdk/transport.py +806 -134
- sentry_sdk/types.py +52 -0
- sentry_sdk/utils.py +1625 -465
- sentry_sdk/worker.py +54 -25
- sentry_sdk-2.46.0.dist-info/METADATA +268 -0
- sentry_sdk-2.46.0.dist-info/RECORD +189 -0
- {sentry_sdk-0.7.5.dist-info → sentry_sdk-2.46.0.dist-info}/WHEEL +1 -1
- sentry_sdk-2.46.0.dist-info/entry_points.txt +2 -0
- sentry_sdk-2.46.0.dist-info/licenses/LICENSE +21 -0
- sentry_sdk/integrations/celery.py +0 -119
- sentry_sdk-0.7.5.dist-info/LICENSE +0 -9
- sentry_sdk-0.7.5.dist-info/METADATA +0 -36
- sentry_sdk-0.7.5.dist-info/RECORD +0 -39
- {sentry_sdk-0.7.5.dist-info → sentry_sdk-2.46.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import sys
|
|
2
|
+
|
|
3
|
+
import sentry_sdk
|
|
4
|
+
from sentry_sdk.consts import OP, SPANSTATUS
|
|
5
|
+
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
|
|
6
|
+
from sentry_sdk.integrations.logging import ignore_logger
|
|
7
|
+
from sentry_sdk.scope import should_send_default_pii
|
|
8
|
+
from sentry_sdk.tracing import Transaction, TransactionSource
|
|
9
|
+
from sentry_sdk.utils import (
|
|
10
|
+
capture_internal_exceptions,
|
|
11
|
+
ensure_integration_enabled,
|
|
12
|
+
event_from_exception,
|
|
13
|
+
SENSITIVE_DATA_SUBSTITUTE,
|
|
14
|
+
parse_version,
|
|
15
|
+
reraise,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
import arq.worker
|
|
20
|
+
from arq.version import VERSION as ARQ_VERSION
|
|
21
|
+
from arq.connections import ArqRedis
|
|
22
|
+
from arq.worker import JobExecutionFailed, Retry, RetryJob, Worker
|
|
23
|
+
except ImportError:
|
|
24
|
+
raise DidNotEnable("Arq is not installed")
|
|
25
|
+
|
|
26
|
+
from typing import TYPE_CHECKING
|
|
27
|
+
|
|
28
|
+
if TYPE_CHECKING:
|
|
29
|
+
from typing import Any, Dict, Optional, Union
|
|
30
|
+
|
|
31
|
+
from sentry_sdk._types import EventProcessor, Event, ExcInfo, Hint
|
|
32
|
+
|
|
33
|
+
from arq.cron import CronJob
|
|
34
|
+
from arq.jobs import Job
|
|
35
|
+
from arq.typing import WorkerCoroutine
|
|
36
|
+
from arq.worker import Function
|
|
37
|
+
|
|
38
|
+
ARQ_CONTROL_FLOW_EXCEPTIONS = (JobExecutionFailed, Retry, RetryJob)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class ArqIntegration(Integration):
|
|
42
|
+
identifier = "arq"
|
|
43
|
+
origin = f"auto.queue.{identifier}"
|
|
44
|
+
|
|
45
|
+
@staticmethod
|
|
46
|
+
def setup_once():
|
|
47
|
+
# type: () -> None
|
|
48
|
+
|
|
49
|
+
try:
|
|
50
|
+
if isinstance(ARQ_VERSION, str):
|
|
51
|
+
version = parse_version(ARQ_VERSION)
|
|
52
|
+
else:
|
|
53
|
+
version = ARQ_VERSION.version[:2]
|
|
54
|
+
|
|
55
|
+
except (TypeError, ValueError):
|
|
56
|
+
version = None
|
|
57
|
+
|
|
58
|
+
_check_minimum_version(ArqIntegration, version)
|
|
59
|
+
|
|
60
|
+
patch_enqueue_job()
|
|
61
|
+
patch_run_job()
|
|
62
|
+
patch_create_worker()
|
|
63
|
+
|
|
64
|
+
ignore_logger("arq.worker")
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def patch_enqueue_job():
|
|
68
|
+
# type: () -> None
|
|
69
|
+
old_enqueue_job = ArqRedis.enqueue_job
|
|
70
|
+
original_kwdefaults = old_enqueue_job.__kwdefaults__
|
|
71
|
+
|
|
72
|
+
async def _sentry_enqueue_job(self, function, *args, **kwargs):
|
|
73
|
+
# type: (ArqRedis, str, *Any, **Any) -> Optional[Job]
|
|
74
|
+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
|
|
75
|
+
if integration is None:
|
|
76
|
+
return await old_enqueue_job(self, function, *args, **kwargs)
|
|
77
|
+
|
|
78
|
+
with sentry_sdk.start_span(
|
|
79
|
+
op=OP.QUEUE_SUBMIT_ARQ, name=function, origin=ArqIntegration.origin
|
|
80
|
+
):
|
|
81
|
+
return await old_enqueue_job(self, function, *args, **kwargs)
|
|
82
|
+
|
|
83
|
+
_sentry_enqueue_job.__kwdefaults__ = original_kwdefaults
|
|
84
|
+
ArqRedis.enqueue_job = _sentry_enqueue_job
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def patch_run_job():
|
|
88
|
+
# type: () -> None
|
|
89
|
+
old_run_job = Worker.run_job
|
|
90
|
+
|
|
91
|
+
async def _sentry_run_job(self, job_id, score):
|
|
92
|
+
# type: (Worker, str, int) -> None
|
|
93
|
+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
|
|
94
|
+
if integration is None:
|
|
95
|
+
return await old_run_job(self, job_id, score)
|
|
96
|
+
|
|
97
|
+
with sentry_sdk.isolation_scope() as scope:
|
|
98
|
+
scope._name = "arq"
|
|
99
|
+
scope.clear_breadcrumbs()
|
|
100
|
+
|
|
101
|
+
transaction = Transaction(
|
|
102
|
+
name="unknown arq task",
|
|
103
|
+
status="ok",
|
|
104
|
+
op=OP.QUEUE_TASK_ARQ,
|
|
105
|
+
source=TransactionSource.TASK,
|
|
106
|
+
origin=ArqIntegration.origin,
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
with sentry_sdk.start_transaction(transaction):
|
|
110
|
+
return await old_run_job(self, job_id, score)
|
|
111
|
+
|
|
112
|
+
Worker.run_job = _sentry_run_job
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def _capture_exception(exc_info):
|
|
116
|
+
# type: (ExcInfo) -> None
|
|
117
|
+
scope = sentry_sdk.get_current_scope()
|
|
118
|
+
|
|
119
|
+
if scope.transaction is not None:
|
|
120
|
+
if exc_info[0] in ARQ_CONTROL_FLOW_EXCEPTIONS:
|
|
121
|
+
scope.transaction.set_status(SPANSTATUS.ABORTED)
|
|
122
|
+
return
|
|
123
|
+
|
|
124
|
+
scope.transaction.set_status(SPANSTATUS.INTERNAL_ERROR)
|
|
125
|
+
|
|
126
|
+
event, hint = event_from_exception(
|
|
127
|
+
exc_info,
|
|
128
|
+
client_options=sentry_sdk.get_client().options,
|
|
129
|
+
mechanism={"type": ArqIntegration.identifier, "handled": False},
|
|
130
|
+
)
|
|
131
|
+
sentry_sdk.capture_event(event, hint=hint)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _make_event_processor(ctx, *args, **kwargs):
|
|
135
|
+
# type: (Dict[Any, Any], *Any, **Any) -> EventProcessor
|
|
136
|
+
def event_processor(event, hint):
|
|
137
|
+
# type: (Event, Hint) -> Optional[Event]
|
|
138
|
+
|
|
139
|
+
with capture_internal_exceptions():
|
|
140
|
+
scope = sentry_sdk.get_current_scope()
|
|
141
|
+
if scope.transaction is not None:
|
|
142
|
+
scope.transaction.name = ctx["job_name"]
|
|
143
|
+
event["transaction"] = ctx["job_name"]
|
|
144
|
+
|
|
145
|
+
tags = event.setdefault("tags", {})
|
|
146
|
+
tags["arq_task_id"] = ctx["job_id"]
|
|
147
|
+
tags["arq_task_retry"] = ctx["job_try"] > 1
|
|
148
|
+
extra = event.setdefault("extra", {})
|
|
149
|
+
extra["arq-job"] = {
|
|
150
|
+
"task": ctx["job_name"],
|
|
151
|
+
"args": (
|
|
152
|
+
args if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
|
|
153
|
+
),
|
|
154
|
+
"kwargs": (
|
|
155
|
+
kwargs if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
|
|
156
|
+
),
|
|
157
|
+
"retry": ctx["job_try"],
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return event
|
|
161
|
+
|
|
162
|
+
return event_processor
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _wrap_coroutine(name, coroutine):
|
|
166
|
+
# type: (str, WorkerCoroutine) -> WorkerCoroutine
|
|
167
|
+
|
|
168
|
+
async def _sentry_coroutine(ctx, *args, **kwargs):
|
|
169
|
+
# type: (Dict[Any, Any], *Any, **Any) -> Any
|
|
170
|
+
integration = sentry_sdk.get_client().get_integration(ArqIntegration)
|
|
171
|
+
if integration is None:
|
|
172
|
+
return await coroutine(ctx, *args, **kwargs)
|
|
173
|
+
|
|
174
|
+
sentry_sdk.get_isolation_scope().add_event_processor(
|
|
175
|
+
_make_event_processor({**ctx, "job_name": name}, *args, **kwargs)
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
try:
|
|
179
|
+
result = await coroutine(ctx, *args, **kwargs)
|
|
180
|
+
except Exception:
|
|
181
|
+
exc_info = sys.exc_info()
|
|
182
|
+
_capture_exception(exc_info)
|
|
183
|
+
reraise(*exc_info)
|
|
184
|
+
|
|
185
|
+
return result
|
|
186
|
+
|
|
187
|
+
return _sentry_coroutine
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def patch_create_worker():
|
|
191
|
+
# type: () -> None
|
|
192
|
+
old_create_worker = arq.worker.create_worker
|
|
193
|
+
|
|
194
|
+
@ensure_integration_enabled(ArqIntegration, old_create_worker)
|
|
195
|
+
def _sentry_create_worker(*args, **kwargs):
|
|
196
|
+
# type: (*Any, **Any) -> Worker
|
|
197
|
+
settings_cls = args[0]
|
|
198
|
+
|
|
199
|
+
if isinstance(settings_cls, dict):
|
|
200
|
+
if "functions" in settings_cls:
|
|
201
|
+
settings_cls["functions"] = [
|
|
202
|
+
_get_arq_function(func)
|
|
203
|
+
for func in settings_cls.get("functions", [])
|
|
204
|
+
]
|
|
205
|
+
if "cron_jobs" in settings_cls:
|
|
206
|
+
settings_cls["cron_jobs"] = [
|
|
207
|
+
_get_arq_cron_job(cron_job)
|
|
208
|
+
for cron_job in settings_cls.get("cron_jobs", [])
|
|
209
|
+
]
|
|
210
|
+
|
|
211
|
+
if hasattr(settings_cls, "functions"):
|
|
212
|
+
settings_cls.functions = [
|
|
213
|
+
_get_arq_function(func) for func in settings_cls.functions
|
|
214
|
+
]
|
|
215
|
+
if hasattr(settings_cls, "cron_jobs"):
|
|
216
|
+
settings_cls.cron_jobs = [
|
|
217
|
+
_get_arq_cron_job(cron_job)
|
|
218
|
+
for cron_job in (settings_cls.cron_jobs or [])
|
|
219
|
+
]
|
|
220
|
+
|
|
221
|
+
if "functions" in kwargs:
|
|
222
|
+
kwargs["functions"] = [
|
|
223
|
+
_get_arq_function(func) for func in kwargs.get("functions", [])
|
|
224
|
+
]
|
|
225
|
+
if "cron_jobs" in kwargs:
|
|
226
|
+
kwargs["cron_jobs"] = [
|
|
227
|
+
_get_arq_cron_job(cron_job) for cron_job in kwargs.get("cron_jobs", [])
|
|
228
|
+
]
|
|
229
|
+
|
|
230
|
+
return old_create_worker(*args, **kwargs)
|
|
231
|
+
|
|
232
|
+
arq.worker.create_worker = _sentry_create_worker
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
def _get_arq_function(func):
|
|
236
|
+
# type: (Union[str, Function, WorkerCoroutine]) -> Function
|
|
237
|
+
arq_func = arq.worker.func(func)
|
|
238
|
+
arq_func.coroutine = _wrap_coroutine(arq_func.name, arq_func.coroutine)
|
|
239
|
+
|
|
240
|
+
return arq_func
|
|
241
|
+
|
|
242
|
+
|
|
243
|
+
def _get_arq_cron_job(cron_job):
|
|
244
|
+
# type: (CronJob) -> CronJob
|
|
245
|
+
cron_job.coroutine = _wrap_coroutine(cron_job.name, cron_job.coroutine)
|
|
246
|
+
|
|
247
|
+
return cron_job
|
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
"""
|
|
2
|
+
An ASGI middleware.
|
|
3
|
+
|
|
4
|
+
Based on Tom Christie's `sentry-asgi <https://github.com/encode/sentry-asgi>`.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import inspect
|
|
9
|
+
from copy import deepcopy
|
|
10
|
+
from functools import partial
|
|
11
|
+
|
|
12
|
+
import sentry_sdk
|
|
13
|
+
from sentry_sdk.api import continue_trace
|
|
14
|
+
from sentry_sdk.consts import OP
|
|
15
|
+
from sentry_sdk.integrations._asgi_common import (
|
|
16
|
+
_get_headers,
|
|
17
|
+
_get_request_data,
|
|
18
|
+
_get_url,
|
|
19
|
+
)
|
|
20
|
+
from sentry_sdk.integrations._wsgi_common import (
|
|
21
|
+
DEFAULT_HTTP_METHODS_TO_CAPTURE,
|
|
22
|
+
nullcontext,
|
|
23
|
+
)
|
|
24
|
+
from sentry_sdk.sessions import track_session
|
|
25
|
+
from sentry_sdk.tracing import (
|
|
26
|
+
SOURCE_FOR_STYLE,
|
|
27
|
+
TransactionSource,
|
|
28
|
+
)
|
|
29
|
+
from sentry_sdk.utils import (
|
|
30
|
+
ContextVar,
|
|
31
|
+
event_from_exception,
|
|
32
|
+
HAS_REAL_CONTEXTVARS,
|
|
33
|
+
CONTEXTVARS_ERROR_MESSAGE,
|
|
34
|
+
logger,
|
|
35
|
+
transaction_from_function,
|
|
36
|
+
_get_installed_modules,
|
|
37
|
+
)
|
|
38
|
+
from sentry_sdk.tracing import Transaction
|
|
39
|
+
|
|
40
|
+
from typing import TYPE_CHECKING
|
|
41
|
+
|
|
42
|
+
if TYPE_CHECKING:
|
|
43
|
+
from typing import Any
|
|
44
|
+
from typing import Dict
|
|
45
|
+
from typing import Optional
|
|
46
|
+
from typing import Tuple
|
|
47
|
+
|
|
48
|
+
from sentry_sdk._types import Event, Hint
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
_asgi_middleware_applied = ContextVar("sentry_asgi_middleware_applied")
|
|
52
|
+
|
|
53
|
+
_DEFAULT_TRANSACTION_NAME = "generic ASGI request"
|
|
54
|
+
|
|
55
|
+
TRANSACTION_STYLE_VALUES = ("endpoint", "url")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def _capture_exception(exc, mechanism_type="asgi"):
|
|
59
|
+
# type: (Any, str) -> None
|
|
60
|
+
|
|
61
|
+
event, hint = event_from_exception(
|
|
62
|
+
exc,
|
|
63
|
+
client_options=sentry_sdk.get_client().options,
|
|
64
|
+
mechanism={"type": mechanism_type, "handled": False},
|
|
65
|
+
)
|
|
66
|
+
sentry_sdk.capture_event(event, hint=hint)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _looks_like_asgi3(app):
|
|
70
|
+
# type: (Any) -> bool
|
|
71
|
+
"""
|
|
72
|
+
Try to figure out if an application object supports ASGI3.
|
|
73
|
+
|
|
74
|
+
This is how uvicorn figures out the application version as well.
|
|
75
|
+
"""
|
|
76
|
+
if inspect.isclass(app):
|
|
77
|
+
return hasattr(app, "__await__")
|
|
78
|
+
elif inspect.isfunction(app):
|
|
79
|
+
return asyncio.iscoroutinefunction(app)
|
|
80
|
+
else:
|
|
81
|
+
call = getattr(app, "__call__", None) # noqa
|
|
82
|
+
return asyncio.iscoroutinefunction(call)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class SentryAsgiMiddleware:
|
|
86
|
+
__slots__ = (
|
|
87
|
+
"app",
|
|
88
|
+
"__call__",
|
|
89
|
+
"transaction_style",
|
|
90
|
+
"mechanism_type",
|
|
91
|
+
"span_origin",
|
|
92
|
+
"http_methods_to_capture",
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
app, # type: Any
|
|
98
|
+
unsafe_context_data=False, # type: bool
|
|
99
|
+
transaction_style="endpoint", # type: str
|
|
100
|
+
mechanism_type="asgi", # type: str
|
|
101
|
+
span_origin="manual", # type: str
|
|
102
|
+
http_methods_to_capture=DEFAULT_HTTP_METHODS_TO_CAPTURE, # type: Tuple[str, ...]
|
|
103
|
+
asgi_version=None, # type: Optional[int]
|
|
104
|
+
):
|
|
105
|
+
# type: (...) -> None
|
|
106
|
+
"""
|
|
107
|
+
Instrument an ASGI application with Sentry. Provides HTTP/websocket
|
|
108
|
+
data to sent events and basic handling for exceptions bubbling up
|
|
109
|
+
through the middleware.
|
|
110
|
+
|
|
111
|
+
:param unsafe_context_data: Disable errors when a proper contextvars installation could not be found. We do not recommend changing this from the default.
|
|
112
|
+
"""
|
|
113
|
+
if not unsafe_context_data and not HAS_REAL_CONTEXTVARS:
|
|
114
|
+
# We better have contextvars or we're going to leak state between
|
|
115
|
+
# requests.
|
|
116
|
+
raise RuntimeError(
|
|
117
|
+
"The ASGI middleware for Sentry requires Python 3.7+ "
|
|
118
|
+
"or the aiocontextvars package." + CONTEXTVARS_ERROR_MESSAGE
|
|
119
|
+
)
|
|
120
|
+
if transaction_style not in TRANSACTION_STYLE_VALUES:
|
|
121
|
+
raise ValueError(
|
|
122
|
+
"Invalid value for transaction_style: %s (must be in %s)"
|
|
123
|
+
% (transaction_style, TRANSACTION_STYLE_VALUES)
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
asgi_middleware_while_using_starlette_or_fastapi = (
|
|
127
|
+
mechanism_type == "asgi" and "starlette" in _get_installed_modules()
|
|
128
|
+
)
|
|
129
|
+
if asgi_middleware_while_using_starlette_or_fastapi:
|
|
130
|
+
logger.warning(
|
|
131
|
+
"The Sentry Python SDK can now automatically support ASGI frameworks like Starlette and FastAPI. "
|
|
132
|
+
"Please remove 'SentryAsgiMiddleware' from your project. "
|
|
133
|
+
"See https://docs.sentry.io/platforms/python/guides/asgi/ for more information."
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
self.transaction_style = transaction_style
|
|
137
|
+
self.mechanism_type = mechanism_type
|
|
138
|
+
self.span_origin = span_origin
|
|
139
|
+
self.app = app
|
|
140
|
+
self.http_methods_to_capture = http_methods_to_capture
|
|
141
|
+
|
|
142
|
+
if asgi_version is None:
|
|
143
|
+
if _looks_like_asgi3(app):
|
|
144
|
+
asgi_version = 3
|
|
145
|
+
else:
|
|
146
|
+
asgi_version = 2
|
|
147
|
+
|
|
148
|
+
if asgi_version == 3:
|
|
149
|
+
self.__call__ = self._run_asgi3
|
|
150
|
+
elif asgi_version == 2:
|
|
151
|
+
self.__call__ = self._run_asgi2 # type: ignore
|
|
152
|
+
|
|
153
|
+
def _capture_lifespan_exception(self, exc):
|
|
154
|
+
# type: (Exception) -> None
|
|
155
|
+
"""Capture exceptions raise in application lifespan handlers.
|
|
156
|
+
|
|
157
|
+
The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
|
|
158
|
+
"""
|
|
159
|
+
return _capture_exception(exc=exc, mechanism_type=self.mechanism_type)
|
|
160
|
+
|
|
161
|
+
def _capture_request_exception(self, exc):
|
|
162
|
+
# type: (Exception) -> None
|
|
163
|
+
"""Capture exceptions raised in incoming request handlers.
|
|
164
|
+
|
|
165
|
+
The separate function is needed to support overriding in derived integrations that use different catching mechanisms.
|
|
166
|
+
"""
|
|
167
|
+
return _capture_exception(exc=exc, mechanism_type=self.mechanism_type)
|
|
168
|
+
|
|
169
|
+
def _run_asgi2(self, scope):
|
|
170
|
+
# type: (Any) -> Any
|
|
171
|
+
async def inner(receive, send):
|
|
172
|
+
# type: (Any, Any) -> Any
|
|
173
|
+
return await self._run_app(scope, receive, send, asgi_version=2)
|
|
174
|
+
|
|
175
|
+
return inner
|
|
176
|
+
|
|
177
|
+
async def _run_asgi3(self, scope, receive, send):
|
|
178
|
+
# type: (Any, Any, Any) -> Any
|
|
179
|
+
return await self._run_app(scope, receive, send, asgi_version=3)
|
|
180
|
+
|
|
181
|
+
async def _run_app(self, scope, receive, send, asgi_version):
|
|
182
|
+
# type: (Any, Any, Any, int) -> Any
|
|
183
|
+
is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
|
|
184
|
+
is_lifespan = scope["type"] == "lifespan"
|
|
185
|
+
if is_recursive_asgi_middleware or is_lifespan:
|
|
186
|
+
try:
|
|
187
|
+
if asgi_version == 2:
|
|
188
|
+
return await self.app(scope)(receive, send)
|
|
189
|
+
else:
|
|
190
|
+
return await self.app(scope, receive, send)
|
|
191
|
+
|
|
192
|
+
except Exception as exc:
|
|
193
|
+
self._capture_lifespan_exception(exc)
|
|
194
|
+
raise exc from None
|
|
195
|
+
|
|
196
|
+
_asgi_middleware_applied.set(True)
|
|
197
|
+
try:
|
|
198
|
+
with sentry_sdk.isolation_scope() as sentry_scope:
|
|
199
|
+
with track_session(sentry_scope, session_mode="request"):
|
|
200
|
+
sentry_scope.clear_breadcrumbs()
|
|
201
|
+
sentry_scope._name = "asgi"
|
|
202
|
+
processor = partial(self.event_processor, asgi_scope=scope)
|
|
203
|
+
sentry_scope.add_event_processor(processor)
|
|
204
|
+
|
|
205
|
+
ty = scope["type"]
|
|
206
|
+
(
|
|
207
|
+
transaction_name,
|
|
208
|
+
transaction_source,
|
|
209
|
+
) = self._get_transaction_name_and_source(
|
|
210
|
+
self.transaction_style,
|
|
211
|
+
scope,
|
|
212
|
+
)
|
|
213
|
+
|
|
214
|
+
method = scope.get("method", "").upper()
|
|
215
|
+
transaction = None
|
|
216
|
+
if ty in ("http", "websocket"):
|
|
217
|
+
if ty == "websocket" or method in self.http_methods_to_capture:
|
|
218
|
+
transaction = continue_trace(
|
|
219
|
+
_get_headers(scope),
|
|
220
|
+
op="{}.server".format(ty),
|
|
221
|
+
name=transaction_name,
|
|
222
|
+
source=transaction_source,
|
|
223
|
+
origin=self.span_origin,
|
|
224
|
+
)
|
|
225
|
+
else:
|
|
226
|
+
transaction = Transaction(
|
|
227
|
+
op=OP.HTTP_SERVER,
|
|
228
|
+
name=transaction_name,
|
|
229
|
+
source=transaction_source,
|
|
230
|
+
origin=self.span_origin,
|
|
231
|
+
)
|
|
232
|
+
|
|
233
|
+
if transaction:
|
|
234
|
+
transaction.set_tag("asgi.type", ty)
|
|
235
|
+
|
|
236
|
+
transaction_context = (
|
|
237
|
+
sentry_sdk.start_transaction(
|
|
238
|
+
transaction,
|
|
239
|
+
custom_sampling_context={"asgi_scope": scope},
|
|
240
|
+
)
|
|
241
|
+
if transaction is not None
|
|
242
|
+
else nullcontext()
|
|
243
|
+
)
|
|
244
|
+
with transaction_context:
|
|
245
|
+
try:
|
|
246
|
+
|
|
247
|
+
async def _sentry_wrapped_send(event):
|
|
248
|
+
# type: (Dict[str, Any]) -> Any
|
|
249
|
+
if transaction is not None:
|
|
250
|
+
is_http_response = (
|
|
251
|
+
event.get("type") == "http.response.start"
|
|
252
|
+
and "status" in event
|
|
253
|
+
)
|
|
254
|
+
if is_http_response:
|
|
255
|
+
transaction.set_http_status(event["status"])
|
|
256
|
+
|
|
257
|
+
return await send(event)
|
|
258
|
+
|
|
259
|
+
if asgi_version == 2:
|
|
260
|
+
return await self.app(scope)(
|
|
261
|
+
receive, _sentry_wrapped_send
|
|
262
|
+
)
|
|
263
|
+
else:
|
|
264
|
+
return await self.app(
|
|
265
|
+
scope, receive, _sentry_wrapped_send
|
|
266
|
+
)
|
|
267
|
+
except Exception as exc:
|
|
268
|
+
self._capture_request_exception(exc)
|
|
269
|
+
raise exc from None
|
|
270
|
+
finally:
|
|
271
|
+
_asgi_middleware_applied.set(False)
|
|
272
|
+
|
|
273
|
+
def event_processor(self, event, hint, asgi_scope):
|
|
274
|
+
# type: (Event, Hint, Any) -> Optional[Event]
|
|
275
|
+
request_data = event.get("request", {})
|
|
276
|
+
request_data.update(_get_request_data(asgi_scope))
|
|
277
|
+
event["request"] = deepcopy(request_data)
|
|
278
|
+
|
|
279
|
+
# Only set transaction name if not already set by Starlette or FastAPI (or other frameworks)
|
|
280
|
+
transaction = event.get("transaction")
|
|
281
|
+
transaction_source = (event.get("transaction_info") or {}).get("source")
|
|
282
|
+
already_set = (
|
|
283
|
+
transaction is not None
|
|
284
|
+
and transaction != _DEFAULT_TRANSACTION_NAME
|
|
285
|
+
and transaction_source
|
|
286
|
+
in [
|
|
287
|
+
TransactionSource.COMPONENT,
|
|
288
|
+
TransactionSource.ROUTE,
|
|
289
|
+
TransactionSource.CUSTOM,
|
|
290
|
+
]
|
|
291
|
+
)
|
|
292
|
+
if not already_set:
|
|
293
|
+
name, source = self._get_transaction_name_and_source(
|
|
294
|
+
self.transaction_style, asgi_scope
|
|
295
|
+
)
|
|
296
|
+
event["transaction"] = name
|
|
297
|
+
event["transaction_info"] = {"source": source}
|
|
298
|
+
|
|
299
|
+
return event
|
|
300
|
+
|
|
301
|
+
# Helper functions.
|
|
302
|
+
#
|
|
303
|
+
# Note: Those functions are not public API. If you want to mutate request
|
|
304
|
+
# data to your liking it's recommended to use the `before_send` callback
|
|
305
|
+
# for that.
|
|
306
|
+
|
|
307
|
+
def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
|
|
308
|
+
# type: (SentryAsgiMiddleware, str, Any) -> Tuple[str, str]
|
|
309
|
+
name = None
|
|
310
|
+
source = SOURCE_FOR_STYLE[transaction_style]
|
|
311
|
+
ty = asgi_scope.get("type")
|
|
312
|
+
|
|
313
|
+
if transaction_style == "endpoint":
|
|
314
|
+
endpoint = asgi_scope.get("endpoint")
|
|
315
|
+
# Webframeworks like Starlette mutate the ASGI env once routing is
|
|
316
|
+
# done, which is sometime after the request has started. If we have
|
|
317
|
+
# an endpoint, overwrite our generic transaction name.
|
|
318
|
+
if endpoint:
|
|
319
|
+
name = transaction_from_function(endpoint) or ""
|
|
320
|
+
else:
|
|
321
|
+
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
|
|
322
|
+
source = TransactionSource.URL
|
|
323
|
+
|
|
324
|
+
elif transaction_style == "url":
|
|
325
|
+
# FastAPI includes the route object in the scope to let Sentry extract the
|
|
326
|
+
# path from it for the transaction name
|
|
327
|
+
route = asgi_scope.get("route")
|
|
328
|
+
if route:
|
|
329
|
+
path = getattr(route, "path", None)
|
|
330
|
+
if path is not None:
|
|
331
|
+
name = path
|
|
332
|
+
else:
|
|
333
|
+
name = _get_url(asgi_scope, "http" if ty == "http" else "ws", host=None)
|
|
334
|
+
source = TransactionSource.URL
|
|
335
|
+
|
|
336
|
+
if name is None:
|
|
337
|
+
name = _DEFAULT_TRANSACTION_NAME
|
|
338
|
+
source = TransactionSource.ROUTE
|
|
339
|
+
return name, source
|
|
340
|
+
|
|
341
|
+
return name, source
|