Skip to content

Commit f95733e

Browse files
committed
ref(queues): Update integrations
1 parent 891bffc commit f95733e

4 files changed

Lines changed: 90 additions & 24 deletions

File tree

sentry_sdk/integrations/celery/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
)
1515
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
1616
from sentry_sdk.integrations.logging import ignore_logger
17+
from sentry_sdk.scope import should_send_default_pii
1718
from sentry_sdk.traces import StreamedSpan
1819
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource
1920
from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled
2021
from sentry_sdk.utils import (
22+
SENSITIVE_DATA_SUBSTITUTE,
2123
capture_internal_exceptions,
2224
event_from_exception,
2325
reraise,
@@ -143,8 +145,12 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
143145
extra = event.setdefault("extra", {})
144146
extra["celery-job"] = {
145147
"task_name": task.name,
146-
"args": args,
147-
"kwargs": kwargs,
148+
"args": (
149+
args if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
150+
),
151+
"kwargs": (
152+
kwargs if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
153+
),
148154
}
149155

150156
if "exc_info" in hint:

sentry_sdk/integrations/rq.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
from sentry_sdk.api import continue_trace
66
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
77
from sentry_sdk.integrations.logging import ignore_logger
8+
from sentry_sdk.scope import should_send_default_pii
89
from sentry_sdk.tracing import TransactionSource
910
from sentry_sdk.utils import (
11+
SENSITIVE_DATA_SUBSTITUTE,
1012
capture_internal_exceptions,
1113
ensure_integration_enabled,
1214
event_from_exception,
@@ -140,8 +142,16 @@ def event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
140142
rq_job = {
141143
"job_id": job.id,
142144
"func": job.func_name,
143-
"args": job.args,
144-
"kwargs": job.kwargs,
145+
"args": (
146+
job.args
147+
if should_send_default_pii()
148+
else SENSITIVE_DATA_SUBSTITUTE
149+
),
150+
"kwargs": (
151+
job.kwargs
152+
if should_send_default_pii()
153+
else SENSITIVE_DATA_SUBSTITUTE
154+
),
145155
"description": job.description,
146156
}
147157

tests/integrations/celery/test_celery.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
_wrap_task_run,
1414
)
1515
from sentry_sdk.integrations.celery.beat import _get_headers
16+
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
1617
from tests.conftest import ApproxDict
1718

1819

@@ -118,11 +119,18 @@ def celery_invocation(request):
118119

119120

120121
@pytest.mark.parametrize("span_streaming", [True, False])
122+
@pytest.mark.parametrize("send_default_pii", [True, False])
121123
def test_simple_with_performance(
122-
capture_events, capture_items, init_celery, celery_invocation, span_streaming
124+
capture_events,
125+
capture_items,
126+
init_celery,
127+
celery_invocation,
128+
span_streaming,
129+
send_default_pii,
123130
):
124131
celery = init_celery(
125132
traces_sample_rate=1.0,
133+
send_default_pii=send_default_pii,
126134
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
127135
)
128136

@@ -158,18 +166,28 @@ def dummy_task(x, y):
158166

159167
assert error_event["transaction"] == "dummy_task"
160168
assert "celery_task_id" in error_event["tags"]
161-
assert error_event["extra"]["celery-job"] == dict(
162-
task_name="dummy_task", **expected_context
163-
)
169+
if send_default_pii:
170+
assert error_event["extra"]["celery-job"] == dict(
171+
task_name="dummy_task", **expected_context
172+
)
173+
else:
174+
assert error_event["extra"]["celery-job"] == {
175+
"task_name": "dummy_task",
176+
"args": SENSITIVE_DATA_SUBSTITUTE,
177+
"kwargs": SENSITIVE_DATA_SUBSTITUTE,
178+
}
164179

165180
(exception,) = error_event["exception"]["values"]
166181
assert exception["type"] == "ZeroDivisionError"
167182
assert exception["mechanism"]["type"] == "celery"
168183
assert exception["stacktrace"]["frames"][0]["vars"]["foo"] == "42"
169184

170185

171-
def test_simple_without_performance(capture_events, init_celery, celery_invocation):
172-
celery = init_celery(traces_sample_rate=None)
186+
@pytest.mark.parametrize("send_default_pii", [True, False])
187+
def test_simple_without_performance(
188+
capture_events, init_celery, celery_invocation, send_default_pii
189+
):
190+
celery = init_celery(traces_sample_rate=None, send_default_pii=send_default_pii)
173191
events = capture_events()
174192

175193
@celery.task(name="dummy_task")
@@ -194,9 +212,16 @@ def dummy_task(x, y):
194212
)
195213
assert error_event["transaction"] == "dummy_task"
196214
assert "celery_task_id" in error_event["tags"]
197-
assert error_event["extra"]["celery-job"] == dict(
198-
task_name="dummy_task", **expected_context
199-
)
215+
if send_default_pii:
216+
assert error_event["extra"]["celery-job"] == dict(
217+
task_name="dummy_task", **expected_context
218+
)
219+
else:
220+
assert error_event["extra"]["celery-job"] == {
221+
"task_name": "dummy_task",
222+
"args": SENSITIVE_DATA_SUBSTITUTE,
223+
"kwargs": SENSITIVE_DATA_SUBSTITUTE,
224+
}
200225

201226
(exception,) = error_event["exception"]["values"]
202227
assert exception["type"] == "ZeroDivisionError"

tests/integrations/rq/test_rq.py

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import sentry_sdk
88
from sentry_sdk import start_transaction
99
from sentry_sdk.integrations.rq import RqIntegration
10-
from sentry_sdk.utils import parse_version
10+
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE, parse_version
1111

1212

1313
@pytest.fixture(autouse=True)
@@ -46,8 +46,9 @@ def do_trick(dog, trick):
4646
return "{}, can you {}? Good dog!".format(dog, trick)
4747

4848

49-
def test_basic(sentry_init, capture_events):
50-
sentry_init(integrations=[RqIntegration()])
49+
@pytest.mark.parametrize("send_default_pii", [True, False])
50+
def test_basic(sentry_init, capture_events, send_default_pii):
51+
sentry_init(integrations=[RqIntegration()], send_default_pii=send_default_pii)
5152
events = capture_events()
5253

5354
queue = rq.Queue(connection=FakeStrictRedis())
@@ -66,8 +67,12 @@ def test_basic(sentry_init, capture_events):
6667
assert event["transaction"] == "tests.integrations.rq.test_rq.crashing_job"
6768

6869
extra = event["extra"]["rq-job"]
69-
assert extra["args"] == []
70-
assert extra["kwargs"] == {"foo": 42}
70+
if send_default_pii:
71+
assert extra["args"] == []
72+
assert extra["kwargs"] == {"foo": 42}
73+
else:
74+
assert extra["args"] == SENSITIVE_DATA_SUBSTITUTE
75+
assert extra["kwargs"] == SENSITIVE_DATA_SUBSTITUTE
7176
assert extra["description"] == "tests.integrations.rq.test_rq.crashing_job(foo=42)"
7277
assert extra["func"] == "tests.integrations.rq.test_rq.crashing_job"
7378
assert "job_id" in extra
@@ -96,12 +101,18 @@ def test_transport_shutdown(sentry_init, capture_events_forksafe):
96101
assert exception["type"] == "ZeroDivisionError"
97102

98103

104+
@pytest.mark.parametrize("send_default_pii", [True, False])
99105
def test_transaction_with_error(
100106
sentry_init,
101107
capture_events,
102108
DictionaryContaining, # noqa:N803
109+
send_default_pii,
103110
):
104-
sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
111+
sentry_init(
112+
integrations=[RqIntegration()],
113+
traces_sample_rate=1.0,
114+
send_default_pii=send_default_pii,
115+
)
105116
events = capture_events()
106117

107118
queue = rq.Queue(connection=FakeStrictRedis())
@@ -125,8 +136,14 @@ def test_transaction_with_error(
125136
assert envelope["transaction"] == error_event["transaction"]
126137
assert envelope["extra"]["rq-job"] == DictionaryContaining(
127138
{
128-
"args": ["Charlie", "Katie"],
129-
"kwargs": {"shoes": "flip-flops"},
139+
"args": (
140+
["Charlie", "Katie"] if send_default_pii else SENSITIVE_DATA_SUBSTITUTE
141+
),
142+
"kwargs": (
143+
{"shoes": "flip-flops"}
144+
if send_default_pii
145+
else SENSITIVE_DATA_SUBSTITUTE
146+
),
130147
"func": "tests.integrations.rq.test_rq.chew_up_shoes",
131148
"description": "tests.integrations.rq.test_rq.chew_up_shoes('Charlie', 'Katie', shoes='flip-flops')",
132149
}
@@ -196,12 +213,18 @@ def test_tracing_disabled(
196213
)
197214

198215

216+
@pytest.mark.parametrize("send_default_pii", [True, False])
199217
def test_transaction_no_error(
200218
sentry_init,
201219
capture_events,
202220
DictionaryContaining, # noqa:N803
221+
send_default_pii,
203222
):
204-
sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
223+
sentry_init(
224+
integrations=[RqIntegration()],
225+
traces_sample_rate=1.0,
226+
send_default_pii=send_default_pii,
227+
)
205228
events = capture_events()
206229

207230
queue = rq.Queue(connection=FakeStrictRedis())
@@ -217,8 +240,10 @@ def test_transaction_no_error(
217240
assert envelope["transaction"] == "tests.integrations.rq.test_rq.do_trick"
218241
assert envelope["extra"]["rq-job"] == DictionaryContaining(
219242
{
220-
"args": ["Maisey"],
221-
"kwargs": {"trick": "kangaroo"},
243+
"args": ["Maisey"] if send_default_pii else SENSITIVE_DATA_SUBSTITUTE,
244+
"kwargs": (
245+
{"trick": "kangaroo"} if send_default_pii else SENSITIVE_DATA_SUBSTITUTE
246+
),
222247
"func": "tests.integrations.rq.test_rq.do_trick",
223248
"description": "tests.integrations.rq.test_rq.do_trick('Maisey', trick='kangaroo')",
224249
}

0 commit comments

Comments
 (0)