|
| 1 | +import unittest.mock |
| 2 | +from collections import abc |
| 3 | + |
| 4 | +import pytest |
| 5 | +import sentry_sdk |
| 6 | +import temporalio.activity |
| 7 | +import temporalio.workflow |
| 8 | +from sentry_sdk.integrations.asyncio import AsyncioIntegration |
| 9 | +from temporalio.client import Client |
| 10 | +from temporalio.worker import Worker |
| 11 | +from temporalio.worker.workflow_sandbox import ( |
| 12 | + SandboxedWorkflowRunner, |
| 13 | + SandboxRestrictions, |
| 14 | +) |
| 15 | + |
| 16 | +from sentry.activity import broken_activity, working_activity |
| 17 | +from sentry.interceptor import SentryInterceptor |
| 18 | +from sentry.workflow import SentryExampleWorkflow, SentryExampleWorkflowInput |
| 19 | +from tests.sentry.fake_sentry_transport import FakeSentryTransport |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def transport() -> FakeSentryTransport: |
| 24 | + """Fixture to provide a fake transport for Sentry SDK.""" |
| 25 | + return FakeSentryTransport() |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(autouse=True) |
| 29 | +def sentry_init(transport: FakeSentryTransport) -> None: |
| 30 | + """Initialize Sentry for testing.""" |
| 31 | + sentry_sdk.init( |
| 32 | + # Pass __callable__ explicitly so SDK treats it as Callable[[Event], None] |
| 33 | + # it confuses it otherwise |
| 34 | + transport=transport.__callable__, |
| 35 | + integrations=[ |
| 36 | + AsyncioIntegration(), |
| 37 | + ], |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +@pytest.fixture |
| 42 | +async def worker(client: Client) -> abc.AsyncIterator[Worker]: |
| 43 | + """Fixture to provide a worker for testing.""" |
| 44 | + async with Worker( |
| 45 | + client, |
| 46 | + task_queue="sentry-task-queue", |
| 47 | + workflows=[SentryExampleWorkflow], |
| 48 | + activities=[broken_activity, working_activity], |
| 49 | + interceptors=[SentryInterceptor()], |
| 50 | + workflow_runner=SandboxedWorkflowRunner( |
| 51 | + restrictions=SandboxRestrictions.default.with_passthrough_modules( |
| 52 | + "sentry_sdk" |
| 53 | + ) |
| 54 | + ), |
| 55 | + ) as worker: |
| 56 | + yield worker |
| 57 | + |
| 58 | + |
| 59 | +async def test_sentry_interceptor_reports_no_errors_when_workflow_succeeds( |
| 60 | + client: Client, worker: Worker, transport: FakeSentryTransport |
| 61 | +) -> None: |
| 62 | + """Test that Sentry interceptor reports no errors when workflow succeeds.""" |
| 63 | + # WHEN |
| 64 | + try: |
| 65 | + await client.execute_workflow( |
| 66 | + SentryExampleWorkflow.run, |
| 67 | + SentryExampleWorkflowInput(option="working"), |
| 68 | + id="sentry-workflow-id", |
| 69 | + task_queue=worker.task_queue, |
| 70 | + ) |
| 71 | + except Exception: |
| 72 | + pytest.fail("Workflow should not raise an exception") |
| 73 | + |
| 74 | + # THEN |
| 75 | + assert len(transport.events) == 0, "No events should be captured" |
| 76 | + |
| 77 | + |
| 78 | +async def test_sentry_interceptor_captures_errors( |
| 79 | + client: Client, worker: Worker, transport: FakeSentryTransport |
| 80 | +) -> None: |
| 81 | + """Test that errors are captured with correct Sentry metadata.""" |
| 82 | + # WHEN |
| 83 | + try: |
| 84 | + await client.execute_workflow( |
| 85 | + SentryExampleWorkflow.run, |
| 86 | + SentryExampleWorkflowInput(option="broken"), |
| 87 | + id="sentry-workflow-id", |
| 88 | + task_queue=worker.task_queue, |
| 89 | + ) |
| 90 | + pytest.fail("Workflow should raise an exception") |
| 91 | + except Exception: |
| 92 | + pass |
| 93 | + |
| 94 | + # THEN |
| 95 | + # there should be two events: one for the failed activity and one for the failed workflow |
| 96 | + assert len(transport.events) == 2, "Two events should be captured" |
| 97 | + |
| 98 | + # Check the first event - should be the activity exception |
| 99 | + # -------------------------------------------------------- |
| 100 | + event = transport.events[0] |
| 101 | + |
| 102 | + # Check exception was captured |
| 103 | + assert event["exception"]["values"][0]["type"] == "Exception" |
| 104 | + assert event["exception"]["values"][0]["value"] == "Activity failed!" |
| 105 | + |
| 106 | + # Check useful metadata were were captured as tags |
| 107 | + assert event["tags"] == { |
| 108 | + "temporal.execution_type": "activity", |
| 109 | + "module": "sentry.activity.broken_activity", |
| 110 | + "temporal.workflow.type": "SentryExampleWorkflow", |
| 111 | + "temporal.workflow.id": "sentry-workflow-id", |
| 112 | + "temporal.activity.id": "1", |
| 113 | + "temporal.activity.type": "broken_activity", |
| 114 | + "temporal.activity.task_queue": "sentry-task-queue", |
| 115 | + "temporal.workflow.namespace": "default", |
| 116 | + "temporal.workflow.run_id": unittest.mock.ANY, |
| 117 | + } |
| 118 | + |
| 119 | + # Check activity input was captured as context |
| 120 | + assert event["contexts"]["temporal.activity.input"] == { |
| 121 | + "message": "Hello, Temporal!", |
| 122 | + } |
| 123 | + |
| 124 | + # Check activity info was captured as context |
| 125 | + activity_info = temporalio.activity.Info( |
| 126 | + **event["contexts"]["temporal.activity.info"] # type: ignore |
| 127 | + ) |
| 128 | + assert activity_info.activity_type == "broken_activity" |
| 129 | + |
| 130 | + # Check the second event - should be the workflow exception |
| 131 | + # --------------------------------------------------------- |
| 132 | + event = transport.events[1] |
| 133 | + |
| 134 | + # Check exception was captured |
| 135 | + assert event["exception"]["values"][0]["type"] == "ApplicationError" |
| 136 | + assert event["exception"]["values"][0]["value"] == "Activity failed!" |
| 137 | + |
| 138 | + # Check useful metadata were were captured as tags |
| 139 | + assert event["tags"] == { |
| 140 | + "temporal.execution_type": "workflow", |
| 141 | + "module": "sentry.workflow.SentryExampleWorkflow.run", |
| 142 | + "temporal.workflow.type": "SentryExampleWorkflow", |
| 143 | + "temporal.workflow.id": "sentry-workflow-id", |
| 144 | + "temporal.workflow.task_queue": "sentry-task-queue", |
| 145 | + "temporal.workflow.namespace": "default", |
| 146 | + "temporal.workflow.run_id": unittest.mock.ANY, |
| 147 | + } |
| 148 | + |
| 149 | + # Check workflow input was captured as context |
| 150 | + assert event["contexts"]["temporal.workflow.input"] == { |
| 151 | + "option": "broken", |
| 152 | + } |
| 153 | + |
| 154 | + # Check workflow info was captured as context |
| 155 | + workflow_info = temporalio.workflow.Info( |
| 156 | + **event["contexts"]["temporal.workflow.info"] # type: ignore |
| 157 | + ) |
| 158 | + assert workflow_info.workflow_type == "SentryExampleWorkflow" |
0 commit comments