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

Sentry message improvement #279

Merged
merged 8 commits into from
Mar 29, 2021
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
5 changes: 5 additions & 0 deletions docs/source/actions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,11 @@ A notification on `Sentry`_ will look like the following one:
:scale: 50 %
:alt: Sentry Notification

The message will have tags based on the failed monitor names (after replacing
whitespace, special symbols etc.), but as the tag length is limited to 32 chars
you should use ``@monitors.name`` to set monitor names that will produce useful
tag names.

The following settings are needed to make this action workable:

.. _SPIDERMON_SENTRY_DSN:
Expand Down
16 changes: 15 additions & 1 deletion spidermon/contrib/actions/sentry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import logging

from slugify import slugify

logger = logging.getLogger(__name__)

from sentry_sdk import configure_scope
Expand Down Expand Up @@ -100,12 +102,24 @@ def get_message(self):

return message

def get_tags(self, message):
tags = {
"spider_name": message.get("spider_name", ""),
"project_name": self.project_name,
}
for failed_monitor in message.get("failed_monitors", []):
key = slugify(failed_monitor.split("/")[-1], max_length=32, separator="_")
tags[key] = 1
return tags

def send_message(self, message):

sentry_client = Client(dsn=self.sentry_dsn, environment=self.environment)

with configure_scope() as scope:
scope.set_tag("project_name", self.project_name)
tags = self.get_tags(message)
for key, val in tags.items():
scope.set_tag(key, val)

scope.set_extra("job_link", message.get("job_link", ""))
scope.set_extra("spider_name", message.get("spider_name", ""))
Expand Down