Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sentry integration-step to worker prerun celery signal #53

Merged
merged 6 commits into from
Dec 3, 2020
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
fail-fast: false
matrix:
python-version: [ "3.7", "3.8", "3.9" ]
django-version: [ "3.1.1", "3.1.2", "3.1.3" ]
django-version: [ "3.1.1", "3.1.2", "3.1.3", "3.1.4" ]
steps:
- name: Check out repository
uses: actions/checkout@v2
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

`3.1.1`_ - 04.12.2020
---------------------

**Features**

* Added a new setting, ``sentry_integration`` to the Celery integration, which sets ``transaction_id`` for Celery workers.

`3.1.0`_ - 18.11.2020
---------------------

Expand Down Expand Up @@ -222,5 +229,6 @@ see the `upgrading docs`_.
.. _upgrading docs: https://django-guid.readthedocs.io/en/latest/upgrading.html
.. _3.0.1: https://github.com/snok/django-guid/compare/3.0.0...3.0.1
.. _3.1.0: https://github.com/snok/django-guid/compare/3.0.1...3.1.0
.. _3.1.1: https://github.com/snok/django-guid/compare/3.1.0...3.1.1

.. _Celery: https://docs.celeryproject.org/en/stable/
2 changes: 1 addition & 1 deletion django_guid/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from django_guid.api import clear_guid, get_guid, set_guid # noqa F401

__version__ = '3.1.0'
__version__ = '3.1.1'
default_app_config = 'django_guid.apps.DjangoGuidConfig'
10 changes: 10 additions & 0 deletions django_guid/integrations/celery/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

from django.core.exceptions import ImproperlyConfigured

from django_guid.integrations import SentryIntegration

if TYPE_CHECKING:
from django_guid.integrations.celery import CeleryIntegration # pragma: no cover

Expand All @@ -24,10 +26,18 @@ def log_parent(self) -> bool:
def uuid_length(self) -> int:
return self.instance.uuid_length

@property
def sentry_integration(self) -> bool:
return self.instance.sentry_integration

def validate(self) -> None:
if not isinstance(self.use_django_logging, bool):
raise ImproperlyConfigured('The CeleryIntegration use_django_logging setting must be a boolean.')
if not isinstance(self.log_parent, bool):
raise ImproperlyConfigured('The CeleryIntegration log_parent setting must be a boolean.')
if type(self.uuid_length) is not int or not 1 <= self.uuid_length <= 32:
raise ImproperlyConfigured('The CeleryIntegration uuid_length setting must be an integer.')
if not isinstance(self.sentry_integration, bool):
raise ImproperlyConfigured('The CeleryIntegration sentry_integration setting must be a boolean.')
if self.sentry_integration:
SentryIntegration().setup()
9 changes: 8 additions & 1 deletion django_guid/integrations/celery/integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ class CeleryIntegration(Integration):

identifier = 'CeleryIntegration'

def __init__(self, use_django_logging: bool = False, log_parent: bool = False, uuid_length: int = 32) -> None:
def __init__(
self,
use_django_logging: bool = False,
log_parent: bool = False,
uuid_length: int = 32,
sentry_integration: bool = False,
) -> None:
"""
:param use_django_logging: If true, configures Celery to use the logging settings defined in settings.py
:param log_parent: If true, traces the origin of a task. Should be True if you wish to use the CeleryTracing log filter.
Expand All @@ -26,6 +32,7 @@ def __init__(self, use_django_logging: bool = False, log_parent: bool = False, u
self.log_parent = log_parent
self.use_django_logging = use_django_logging
self.uuid_length = uuid_length
self.sentry_integration = sentry_integration

def setup(self) -> None:
"""
Expand Down
15 changes: 15 additions & 0 deletions django_guid/integrations/celery/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,22 @@
from django_guid.utils import generate_guid

logger = logging.getLogger('django_guid.celery')

parent_header = 'CELERY_PARENT_ID'


def set_transaction_id(guid: str) -> None:
"""
Sets the Sentry transaction ID if the Celery sentry integration setting is True.
"""
if settings.integration_settings.celery.sentry_integration:
from sentry_sdk import configure_scope

with configure_scope() as scope:
logger.debug('Setting Sentry transaction_id to %s', guid)
scope.set_tag('transaction_id', guid)


@before_task_publish.connect
def publish_task_from_worker_or_request(headers: dict, **kwargs) -> None:
"""
Expand Down Expand Up @@ -41,10 +54,12 @@ def worker_prerun(task: Task, **kwargs) -> None:
if guid:
logger.info('Setting GUID %s', guid)
set_guid(guid)
set_transaction_id(guid)
else:
generated_guid = generate_guid(uuid_length=settings.integration_settings.celery.uuid_length)
logger.info('Generated GUID %s', generated_guid)
set_guid(generated_guid)
set_transaction_id(generated_guid)

if settings.integration_settings.celery.log_parent:
origin = task.request.get(parent_header)
Expand Down
1 change: 1 addition & 0 deletions docs/integrations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ These are the settings you can pass when instantiating the ``CeleryIntegration``
* **use_django_logging**: Tells celery to use the Django logging configuration (formatter).
* **log_parent**: Enables the ``CeleryTracing`` log filter described below.
* **uuid_length**: Lets you optionally trim the length of the integration generated UUIDs.
* **sentry_integration**: If you use Sentry, enabling this setting will make sure ``transaction_id`` is set (like in the SentryIntegration) for Celery workers.

Celery integration log filter
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
83 changes: 52 additions & 31 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading