Пример #1
0
def send_notification_as_email(
    notification: BaseNotification,
    recipients: Iterable[Team | User],
    shared_context: Mapping[str, Any],
    extra_context_by_actor_id: Mapping[int, Mapping[str, Any]] | None,
) -> None:
    for recipient in recipients:
        with sentry_sdk.start_span(op="notification.send_email",
                                   description="one_recipient"):
            if isinstance(recipient, Team):
                # TODO(mgaeta): MessageBuilder only works with Users so filter out Teams for now.
                continue
            log_message(notification, recipient)

            with sentry_sdk.start_span(op="notification.send_email",
                                       description="build_message"):
                msg = MessageBuilder(
                    **get_builder_args(notification, recipient, shared_context,
                                       extra_context_by_actor_id))

            with sentry_sdk.start_span(op="notification.send_email",
                                       description="send_message"):
                # TODO: find better way of handling this
                add_users_kwargs = {}
                if isinstance(notification, ProjectNotification):
                    add_users_kwargs["project"] = notification.project
                msg.add_users([recipient.id], **add_users_kwargs)
                msg.send_async()
            notification.record_notification_sent(recipient,
                                                  ExternalProviders.EMAIL)
Пример #2
0
def _notify_recipient(
    notification: BaseNotification,
    recipient: Team | User,
    attachments: List[SlackAttachment],
    channel: str,
    integration: Integration,
) -> None:
    with sentry_sdk.start_span(op="notification.send_slack", description="notify_recipient"):
        # Make a local copy to which we can append.
        local_attachments = copy(attachments)

        token: str = integration.metadata["access_token"]

        # Add optional billing related attachment.
        additional_attachment = get_additional_attachment(integration, notification.organization)
        if additional_attachment:
            local_attachments.append(additional_attachment)

        # unfurl_links and unfurl_media are needed to preserve the intended message format
        # and prevent the app from replying with help text to the unfurl
        payload = {
            "token": token,
            "channel": channel,
            "link_names": 1,
            "unfurl_links": False,
            "unfurl_media": False,
            "text": notification.get_notification_title(),
            "attachments": json.dumps(local_attachments),
        }

        log_params = {
            "notification": notification,
            "recipient": recipient.id,
            "channel_id": channel,
        }
        post_message.apply_async(
            kwargs={
                "payload": payload,
                "log_error_message": "notification.fail.slack_post",
                "log_params": log_params,
            }
        )
    # recording data outside of span
    notification.record_notification_sent(recipient, ExternalProviders.SLACK)
Пример #3
0
def log_message(notification: BaseNotification,
                recipient: Team | User) -> None:
    extra = notification.get_log_params(recipient)
    logger.info("mail.adapter.notify.mail_user", extra={**extra})
    notification.record_notification_sent(recipient, ExternalProviders.EMAIL)
Пример #4
0
def send_notification_as_slack(
    notification: BaseNotification,
    recipients: Iterable[Team | User],
    shared_context: Mapping[str, Any],
    extra_context_by_actor_id: Mapping[int, Mapping[str, Any]] | None,
) -> None:
    """Send an "activity" or "alert rule" notification to a Slack user or team."""
    data = get_integrations_by_channel_by_recipient(notification.organization,
                                                    recipients)
    for recipient, channels_to_integrations in data.items():
        is_multiple = (True if len(
            [integration
             for integration in channels_to_integrations]) > 1 else False)
        if is_multiple:
            logger.info(
                "notification.multiple.slack_post",
                extra={
                    "notification": notification,
                    "recipient": recipient.id,
                },
            )
        extra_context = (extra_context_by_actor_id
                         or {}).get(recipient.actor_id, {})
        context = get_context(notification, recipient, shared_context,
                              extra_context)
        attachments = get_attachments(notification, recipient, context)

        for channel, integration in channels_to_integrations.items():
            # make a local copy for appending
            local_attachments = copy(attachments)
            token: str = integration.metadata["access_token"]
            # getsentry might add a billing related attachment
            additional_attachment = get_additional_attachment(
                integration, notification.organization)
            if additional_attachment:
                local_attachments.append(additional_attachment)

            # unfurl_links and unfurl_media are needed to preserve the intended message format
            # and prevent the app from replying with help text to the unfurl
            payload = {
                "token": token,
                "channel": channel,
                "link_names": 1,
                "unfurl_links": False,
                "unfurl_media": False,
                "text": notification.get_notification_title(),
                "attachments": json.dumps(local_attachments),
            }
            log_params = {
                "notification": notification,
                "recipient": recipient.id,
                "channel_id": channel,
                "is_multiple": is_multiple,
            }
            post_message.apply_async(
                kwargs={
                    "payload": payload,
                    "log_error_message": "notification.fail.slack_post",
                    "log_params": log_params,
                })
            notification.record_notification_sent(recipient,
                                                  ExternalProviders.SLACK)

    key = notification.metrics_key
    metrics.incr(
        f"{key}.notifications.sent",
        instance=f"slack.{key}.notification",
        skip_internal=False,
    )