Skip to content

Commit

Permalink
Merge branch 'master' into txiao/feat/add-thread-data-to-spans
Browse files Browse the repository at this point in the history
  • Loading branch information
Zylphrex authored Mar 20, 2024
2 parents 5bf2efa + 500e087 commit 6f3a00e
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 26 deletions.
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,36 @@
# Changelog

## 1.43.0

### Various fixes & improvements

- Add optional `keep_alive` (#2842) by @sentrivana

If you're experiencing frequent network issues between the SDK and Sentry,
you can try turning on TCP keep-alive:

```python
import sentry_sdk

sentry_sdk.init(
# ...your usual settings...
keep_alive=True,
)
```

- Add support for Celery Redbeat cron tasks (#2643) by @kwigley

The SDK now supports the Redbeat scheduler in addition to the default
Celery Beat scheduler for auto instrumenting crons. See
[the docs](https://docs.sentry.io/platforms/python/integrations/celery/crons/)
for more information about how to set this up.

- `aws_event` can be an empty list (#2849) by @sentrivana
- Re-export `Event` in `types.py` (#2829) by @szokeasaurusrex
- Small API docs improvement (#2828) by @antonpirker
- Fixed OpenAI tests (#2834) by @antonpirker
- Bump `checkouts/data-schemas` from `ed078ed` to `8232f17` (#2832) by @dependabot

## 1.42.0

### Various fixes & improvements
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ lint: .venv
apidocs: .venv
@$(VENV_PATH)/bin/pip install --editable .
@$(VENV_PATH)/bin/pip install -U -r ./docs-requirements.txt
rm -rf docs/_build
@$(VENV_PATH)/bin/sphinx-build -vv -W -b html docs/ docs/_build
.PHONY: apidocs

Expand Down
2 changes: 1 addition & 1 deletion checkouts/data-schemas
Submodule data-schemas updated 2 files
+3 −3 seer/seer.py
+569 −599 seer/seer_api.json
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
copyright = "2019-{}, Sentry Team and Contributors".format(datetime.now().year)
author = "Sentry Team and Contributors"

release = "1.42.0"
release = "1.43.0"
version = ".".join(release.split(".")[:2]) # The short X.Y version.


Expand Down
3 changes: 2 additions & 1 deletion sentry_sdk/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ def __init__(
ignore_errors=[], # type: Sequence[Union[type, str]] # noqa: B006
max_request_body_size="medium", # type: str
socket_options=None, # type: Optional[List[Tuple[int, int, int | bytes]]]
keep_alive=False, # type: bool
before_send=None, # type: Optional[EventProcessor]
before_breadcrumb=None, # type: Optional[BreadcrumbProcessor]
debug=None, # type: Optional[bool]
Expand Down Expand Up @@ -332,4 +333,4 @@ def _get_default_options():
del _get_default_options


VERSION = "1.42.0"
VERSION = "1.43.0"
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
# will be the same for all events in the list, since they're all hitting
# the lambda in the same request.)

if isinstance(aws_event, list):
if isinstance(aws_event, list) and len(aws_event) >= 1:
request_data = aws_event[0]
batch_size = len(aws_event)
else:
Expand Down
62 changes: 62 additions & 0 deletions sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
except ImportError:
raise DidNotEnable("Celery not installed")

try:
from redbeat.schedulers import RedBeatScheduler # type: ignore
except ImportError:
RedBeatScheduler = None


CELERY_CONTROL_FLOW_EXCEPTIONS = (Retry, Ignore, Reject)

Expand All @@ -76,6 +81,7 @@ def __init__(

if monitor_beat_tasks:
_patch_beat_apply_entry()
_patch_redbeat_maybe_due()
_setup_celery_beat_signals()

@staticmethod
Expand Down Expand Up @@ -535,6 +541,62 @@ def sentry_apply_entry(*args, **kwargs):
Scheduler.apply_entry = sentry_apply_entry


def _patch_redbeat_maybe_due():
# type: () -> None

if RedBeatScheduler is None:
return

original_maybe_due = RedBeatScheduler.maybe_due

def sentry_maybe_due(*args, **kwargs):
# type: (*Any, **Any) -> None
scheduler, schedule_entry = args
app = scheduler.app

celery_schedule = schedule_entry.schedule
monitor_name = schedule_entry.name

hub = Hub.current
integration = hub.get_integration(CeleryIntegration)
if integration is None:
return original_maybe_due(*args, **kwargs)

if match_regex_list(monitor_name, integration.exclude_beat_tasks):
return original_maybe_due(*args, **kwargs)

with hub.configure_scope() as scope:
# When tasks are started from Celery Beat, make sure each task has its own trace.
scope.set_new_propagation_context()

monitor_config = _get_monitor_config(celery_schedule, app, monitor_name)

is_supported_schedule = bool(monitor_config)
if is_supported_schedule:
headers = schedule_entry.options.pop("headers", {})
headers.update(
{
"sentry-monitor-slug": monitor_name,
"sentry-monitor-config": monitor_config,
}
)

check_in_id = capture_checkin(
monitor_slug=monitor_name,
monitor_config=monitor_config,
status=MonitorStatus.IN_PROGRESS,
)
headers.update({"sentry-monitor-check-in-id": check_in_id})

# Set the Sentry configuration in the options of the ScheduleEntry.
# Those will be picked up in `apply_async` and added to the headers.
schedule_entry.options["headers"] = headers

return original_maybe_due(*args, **kwargs)

RedBeatScheduler.maybe_due = sentry_maybe_due


def _setup_celery_beat_signals():
# type: () -> None
task_success.connect(crons_task_success)
Expand Down
55 changes: 38 additions & 17 deletions sentry_sdk/scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,28 @@ def clear(self):

@_attr_setter
def level(self, value):
# type: (Optional[LogLevelStr]) -> None
"""When set this overrides the level. Deprecated in favor of set_level."""
# type: (LogLevelStr) -> None
"""
When set this overrides the level.
.. deprecated:: 1.0.0
Use :func:`set_level` instead.
:param value: The level to set.
"""
logger.warning(
"Deprecated: use .set_level() instead. This will be removed in the future."
)

self._level = value

def set_level(self, value):
# type: (Optional[LogLevelStr]) -> None
"""Sets the level for the scope."""
# type: (LogLevelStr) -> None
"""
Sets the level for the scope.
:param value: The level to set.
"""
self._level = value

@_attr_setter
Expand Down Expand Up @@ -555,20 +570,24 @@ def profile(self, profile):

self._profile = profile

def set_tag(
self,
key, # type: str
value, # type: Any
):
# type: (...) -> None
"""Sets a tag for a key to a specific value."""
def set_tag(self, key, value):
# type: (str, Any) -> None
"""
Sets a tag for a key to a specific value.
:param key: Key of the tag to set.
:param value: Value of the tag to set.
"""
self._tags[key] = value

def remove_tag(
self, key # type: str
):
# type: (...) -> None
"""Removes a specific tag."""
def remove_tag(self, key):
# type: (str) -> None
"""
Removes a specific tag.
:param key: Key of the tag to remove.
"""
self._tags.pop(key, None)

def set_context(
Expand All @@ -577,7 +596,9 @@ def set_context(
value, # type: Dict[str, Any]
):
# type: (...) -> None
"""Binds a context at a certain key to a specific value."""
"""
Binds a context at a certain key to a specific value.
"""
self._contexts[key] = value

def remove_context(
Expand Down
35 changes: 33 additions & 2 deletions sentry_sdk/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import gzip
import socket
import time
from datetime import timedelta
from collections import defaultdict
Expand All @@ -21,6 +22,7 @@
from typing import Callable
from typing import Dict
from typing import Iterable
from typing import List
from typing import Optional
from typing import Tuple
from typing import Type
Expand All @@ -40,6 +42,21 @@
from urllib import getproxies # type: ignore


KEEP_ALIVE_SOCKET_OPTIONS = []
for option in [
(socket.SOL_SOCKET, lambda: getattr(socket, "SO_KEEPALIVE"), 1), # noqa: B009
(socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPIDLE"), 45), # noqa: B009
(socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPINTVL"), 10), # noqa: B009
(socket.SOL_TCP, lambda: getattr(socket, "TCP_KEEPCNT"), 6), # noqa: B009
]:
try:
KEEP_ALIVE_SOCKET_OPTIONS.append((option[0], option[1](), option[2]))
except AttributeError:
# a specific option might not be available on specific systems,
# e.g. TCP_KEEPIDLE doesn't exist on macOS
pass


class Transport(object):
"""Baseclass for all transports.
Expand Down Expand Up @@ -446,8 +463,22 @@ def _get_pool_options(self, ca_certs):
"ca_certs": ca_certs or certifi.where(),
}

if self.options["socket_options"]:
options["socket_options"] = self.options["socket_options"]
socket_options = None # type: Optional[List[Tuple[int, int, int | bytes]]]

if self.options["socket_options"] is not None:
socket_options = self.options["socket_options"]

if self.options["keep_alive"]:
if socket_options is None:
socket_options = []

used_options = {(o[0], o[1]) for o in socket_options}
for default_option in KEEP_ALIVE_SOCKET_OPTIONS:
if (default_option[0], default_option[1]) not in used_options:
socket_options.append(default_option)

if socket_options is not None:
options["socket_options"] = socket_options

return options

Expand Down
14 changes: 14 additions & 0 deletions sentry_sdk/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
This module contains type definitions for the Sentry SDK's public API.
The types are re-exported from the internal module `sentry_sdk._types`.
Disclaimer: Since types are a form of documentation, type definitions
may change in minor releases. Removing a type would be considered a
breaking change, and so we will only remove type definitions in major
releases.
"""

from typing import TYPE_CHECKING

if TYPE_CHECKING:
from sentry_sdk._types import Event, Hint # noqa: F401
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_file_text(file_name):

setup(
name="sentry-sdk",
version="1.42.0",
version="1.43.0",
author="Sentry Team and Contributors",
author_email="hello@sentry.io",
url="https://github.com/getsentry/sentry-python",
Expand Down Expand Up @@ -50,6 +50,7 @@ def get_file_text(file_name):
"beam": ["apache-beam>=2.12"],
"bottle": ["bottle>=0.12.13"],
"celery": ["celery>=3"],
"celery-redbeat": ["celery-redbeat>=2"],
"chalice": ["chalice>=1.16.0"],
"clickhouse-driver": ["clickhouse-driver>=0.2.0"],
"django": ["django>=1.8"],
Expand Down
1 change: 1 addition & 0 deletions tests/integrations/aws_lambda/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,7 @@ def test_handler(event, context):
True,
2,
),
(b"[]", False, 1),
],
)
def test_non_dict_event(
Expand Down
Loading

0 comments on commit 6f3a00e

Please sign in to comment.