Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions sentry_sdk/integrations/celery/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
)
from sentry_sdk.integrations.celery.utils import _now_seconds_since_epoch
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import StreamedSpan
from sentry_sdk.tracing import BAGGAGE_HEADER_NAME, Span, TransactionSource
from sentry_sdk.tracing_utils import Baggage, has_span_streaming_enabled
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
event_from_exception,
reraise,
Expand Down Expand Up @@ -143,8 +145,12 @@ def event_processor(event: "Event", hint: "Hint") -> "Optional[Event]":
extra = event.setdefault("extra", {})
extra["celery-job"] = {
"task_name": task.name,
"args": args,
"kwargs": kwargs,
"args": (
args if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
),
"kwargs": (
kwargs if should_send_default_pii() else SENSITIVE_DATA_SUBSTITUTE
),
}

if "exc_info" in hint:
Expand Down
14 changes: 12 additions & 2 deletions sentry_sdk/integrations/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
from sentry_sdk.api import continue_trace
from sentry_sdk.integrations import _check_minimum_version, DidNotEnable, Integration
from sentry_sdk.integrations.logging import ignore_logger
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.tracing import TransactionSource
from sentry_sdk.utils import (
SENSITIVE_DATA_SUBSTITUTE,
capture_internal_exceptions,
ensure_integration_enabled,
event_from_exception,
Expand Down Expand Up @@ -140,8 +142,16 @@ def event_processor(event: "Event", hint: "dict[str, Any]") -> "Event":
rq_job = {
"job_id": job.id,
"func": job.func_name,
"args": job.args,
"kwargs": job.kwargs,
"args": (
job.args
if should_send_default_pii()
else SENSITIVE_DATA_SUBSTITUTE
),
"kwargs": (
job.kwargs
if should_send_default_pii()
else SENSITIVE_DATA_SUBSTITUTE
),
"description": job.description,
}

Expand Down
43 changes: 34 additions & 9 deletions tests/integrations/celery/test_celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
_wrap_task_run,
)
from sentry_sdk.integrations.celery.beat import _get_headers
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
from tests.conftest import ApproxDict


Expand Down Expand Up @@ -118,11 +119,18 @@ def celery_invocation(request):


@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize("send_default_pii", [True, False])
def test_simple_with_performance(
capture_events, capture_items, init_celery, celery_invocation, span_streaming
capture_events,
capture_items,
init_celery,
celery_invocation,
span_streaming,
send_default_pii,
):
celery = init_celery(
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
_experiments={"trace_lifecycle": "stream" if span_streaming else "static"},
)

Expand Down Expand Up @@ -158,18 +166,28 @@ def dummy_task(x, y):

assert error_event["transaction"] == "dummy_task"
assert "celery_task_id" in error_event["tags"]
assert error_event["extra"]["celery-job"] == dict(
task_name="dummy_task", **expected_context
)
if send_default_pii:
assert error_event["extra"]["celery-job"] == dict(
task_name="dummy_task", **expected_context
)
else:
assert error_event["extra"]["celery-job"] == {
"task_name": "dummy_task",
"args": SENSITIVE_DATA_SUBSTITUTE,
"kwargs": SENSITIVE_DATA_SUBSTITUTE,
}

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


def test_simple_without_performance(capture_events, init_celery, celery_invocation):
celery = init_celery(traces_sample_rate=None)
@pytest.mark.parametrize("send_default_pii", [True, False])
def test_simple_without_performance(
capture_events, init_celery, celery_invocation, send_default_pii
):
celery = init_celery(traces_sample_rate=None, send_default_pii=send_default_pii)
events = capture_events()

@celery.task(name="dummy_task")
Expand All @@ -194,9 +212,16 @@ def dummy_task(x, y):
)
assert error_event["transaction"] == "dummy_task"
assert "celery_task_id" in error_event["tags"]
assert error_event["extra"]["celery-job"] == dict(
task_name="dummy_task", **expected_context
)
if send_default_pii:
assert error_event["extra"]["celery-job"] == dict(
task_name="dummy_task", **expected_context
)
else:
assert error_event["extra"]["celery-job"] == {
"task_name": "dummy_task",
"args": SENSITIVE_DATA_SUBSTITUTE,
"kwargs": SENSITIVE_DATA_SUBSTITUTE,
}

(exception,) = error_event["exception"]["values"]
assert exception["type"] == "ZeroDivisionError"
Expand Down
47 changes: 36 additions & 11 deletions tests/integrations/rq/test_rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import sentry_sdk
from sentry_sdk import start_transaction
from sentry_sdk.integrations.rq import RqIntegration
from sentry_sdk.utils import parse_version
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE, parse_version


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


def test_basic(sentry_init, capture_events):
sentry_init(integrations=[RqIntegration()])
@pytest.mark.parametrize("send_default_pii", [True, False])
def test_basic(sentry_init, capture_events, send_default_pii):
sentry_init(integrations=[RqIntegration()], send_default_pii=send_default_pii)
events = capture_events()

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

extra = event["extra"]["rq-job"]
assert extra["args"] == []
assert extra["kwargs"] == {"foo": 42}
if send_default_pii:
assert extra["args"] == []
assert extra["kwargs"] == {"foo": 42}
else:
assert extra["args"] == SENSITIVE_DATA_SUBSTITUTE
assert extra["kwargs"] == SENSITIVE_DATA_SUBSTITUTE
assert extra["description"] == "tests.integrations.rq.test_rq.crashing_job(foo=42)"
assert extra["func"] == "tests.integrations.rq.test_rq.crashing_job"
assert "job_id" in extra
Expand Down Expand Up @@ -96,12 +101,18 @@ def test_transport_shutdown(sentry_init, capture_events_forksafe):
assert exception["type"] == "ZeroDivisionError"


@pytest.mark.parametrize("send_default_pii", [True, False])
def test_transaction_with_error(
sentry_init,
capture_events,
DictionaryContaining, # noqa:N803
send_default_pii,
):
sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
sentry_init(
integrations=[RqIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
)
events = capture_events()

queue = rq.Queue(connection=FakeStrictRedis())
Expand All @@ -125,8 +136,14 @@ def test_transaction_with_error(
assert envelope["transaction"] == error_event["transaction"]
assert envelope["extra"]["rq-job"] == DictionaryContaining(
{
"args": ["Charlie", "Katie"],
"kwargs": {"shoes": "flip-flops"},
"args": (
["Charlie", "Katie"] if send_default_pii else SENSITIVE_DATA_SUBSTITUTE
),
"kwargs": (
{"shoes": "flip-flops"}
if send_default_pii
else SENSITIVE_DATA_SUBSTITUTE
),
"func": "tests.integrations.rq.test_rq.chew_up_shoes",
"description": "tests.integrations.rq.test_rq.chew_up_shoes('Charlie', 'Katie', shoes='flip-flops')",
}
Expand Down Expand Up @@ -196,12 +213,18 @@ def test_tracing_disabled(
)


@pytest.mark.parametrize("send_default_pii", [True, False])
def test_transaction_no_error(
sentry_init,
capture_events,
DictionaryContaining, # noqa:N803
send_default_pii,
):
sentry_init(integrations=[RqIntegration()], traces_sample_rate=1.0)
sentry_init(
integrations=[RqIntegration()],
traces_sample_rate=1.0,
send_default_pii=send_default_pii,
)
events = capture_events()

queue = rq.Queue(connection=FakeStrictRedis())
Expand All @@ -217,8 +240,10 @@ def test_transaction_no_error(
assert envelope["transaction"] == "tests.integrations.rq.test_rq.do_trick"
assert envelope["extra"]["rq-job"] == DictionaryContaining(
{
"args": ["Maisey"],
"kwargs": {"trick": "kangaroo"},
"args": ["Maisey"] if send_default_pii else SENSITIVE_DATA_SUBSTITUTE,
"kwargs": (
{"trick": "kangaroo"} if send_default_pii else SENSITIVE_DATA_SUBSTITUTE
),
"func": "tests.integrations.rq.test_rq.do_trick",
"description": "tests.integrations.rq.test_rq.do_trick('Maisey', trick='kangaroo')",
}
Expand Down
Loading