sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

KeyError: 'warnings' in catch_warnings

Open leonard-henriquez opened this issue 4 months ago • 2 comments

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.11.0

Steps to Reproduce

Steps:

  • Use sentry
  • In a temporal workflow
  • And issue a warning

Problem Description: The issue arises because the Sentry SDK assumes that the warnings module is always present. However, when running in a safe environment like Temporal (and other similar environments), this module may be restricted for security reasons, as these environments limit access to certain global state modules (like warnings).

Proposed Solution: Sentry should not assume the presence of the warnings module. The catch_warnings function should first check if the warnings module exists, and handle its absence gracefully. This adjustment should ensure better compatibility with secure environments like Temporal.

Expected Result

> poetry run python run_workflow.py "generate_follow_ups"

CLI 1.1.0 (Server 1.25.0, UI 2.30.3)

Server:  localhost:7233
Metrics: http://localhost:61488/metrics
Temporal environment and worker started. Press Ctrl+C to stop.
Workflow started with ID: c5835ec9-d4c3-4aba-919d-09e488ee2a78
time=2024-09-29T01:06:09.558 level=WARN msg="error in prometheus reporter" error="a previously registered descriptor with the same fully-qualified name as Desc{fqName: \"task_schedule_to_start_latency\", help: \"task_schedule_to_start_latency histogram\", constLabels: {}, variableLabels: {service_name,operation,namespace,taskqueue}} has different label names or a different help string"
2024-09-29T08:06:12.624266+00:00 [info     ] [GenerateFollowUps] Getting followups for the campaign 0191bd3b-e688-7350-802a-cbda765557ff. [main]
2024-09-29T08:06:13.603476+00:00 [info     ] [GetFollowUpsToGenerate] Found 100 follow ups to generate. [main] span={'trace_id': 46501998728614327432160006987081809693, 'span_id': 4107830682838171212, 'parent_span_id': 3188224582942380771}

Actual Result

> poetry run python run_workflow.py "generate_follow_ups"

CLI 1.1.0 (Server 1.25.0, UI 2.30.3)

Server:  localhost:7233
UI:      http://localhost:8233
Metrics: http://localhost:53003/metrics
Temporal environment and worker started. Press Ctrl+C to stop.
Workflow started with ID: 96826e23-7073-4134-9c9f-b62456e51d40
Failed activation on workflow GenerateFollowUps with ID 96826e23-7073-4134-9c9f-b62456e51d40 and run ID d0700809-fdb5-477b-9ace-0895e4715f40
Traceback (most recent call last):
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_workflow_instance.py", line 369, in activate
    self._run_once(check_conditions=index == 1 or index == 2)
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_workflow_instance.py", line 1767, in _run_once
    raise self._current_activation_error
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_workflow_instance.py", line 1785, in _run_top_level_workflow_function
    await coro
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_workflow_instance.py", line 849, in run_workflow
    result = await self._inbound.execute_workflow(input)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/contrib/opentelemetry.py", line 356, in execute_workflow
    return await super().execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_interceptor.py", line 328, in execute_workflow
    return await self.next.execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/testing/_workflow.py", line 493, in execute_workflow
    return await super().execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_interceptor.py", line 328, in execute_workflow
    return await self.next.execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/contrib/opentelemetry.py", line 356, in execute_workflow
    return await super().execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/_interceptor.py", line 328, in execute_workflow
    return await self.next.execute_workflow(input)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/app/utils/worker/error_tracking.py", line 80, in execute_workflow
    with Hub(Hub.current):
             ^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/sentry_sdk/hub.py", line 103, in current
    with _suppress_hub_deprecation_warning():
  File "/Users/leonard/.asdf/installs/python/3.12.2/lib/python3.12/contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/sentry_sdk/hub.py", line 87, in _suppress_hub_deprecation_warning
    with warnings.catch_warnings():
         ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/leonard/.asdf/installs/python/3.12.2/lib/python3.12/warnings.py", line 466, in __init__
    self._module = sys.modules['warnings'] if module is None else module
                   ~~~~~~~~~~~^^^^^^^^^^^^
  File "/Users/leonard/code/agent/apps/backend/.venv/lib/python3.12/site-packages/temporalio/worker/workflow_sandbox/_importer.py", line 393, in __getitem__
    return self.current[key]
           ~~~~~~~~~~~~^^^^^
KeyError: 'warnings'

leonard-henriquez avatar Sep 29 '24 08:09 leonard-henriquez