From b9f00b27108569118e8331cba7791865b21bd22b Mon Sep 17 00:00:00 2001 From: Michael Matloka Date: Wed, 24 Feb 2021 23:14:02 +0100 Subject: [PATCH] Fix webhooks specifity (#3463) * Fix webhooks specifity * Make REST hook firing monitoring better reflect reality --- ee/models/hook.py | 4 +++- ee/tasks/webhooks_ee.py | 29 +++++++++++++---------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/ee/models/hook.py b/ee/models/hook.py index 8eb88839015..c6999cfa49a 100644 --- a/ee/models/hook.py +++ b/ee/models/hook.py @@ -1,6 +1,7 @@ -import json from typing import Optional +import statsd +from django.conf import settings from django.db import models from rest_hooks.models import AbstractHook @@ -26,6 +27,7 @@ def find_and_fire_hook( # action_performed is a resource_id-filterable hook hooks = hooks.filter(models.Q(resource_id=instance.pk)) for hook in hooks: + statsd.Counter("%s_posthog_cloud_hooks_rest_fired" % (settings.STATSD_PREFIX,)).increment() hook.deliver_hook(instance, payload_override) diff --git a/ee/tasks/webhooks_ee.py b/ee/tasks/webhooks_ee.py index 75baac5b37e..364bc0f906b 100644 --- a/ee/tasks/webhooks_ee.py +++ b/ee/tasks/webhooks_ee.py @@ -1,4 +1,3 @@ -import re from typing import Any, Dict, Sequence, cast import requests @@ -50,22 +49,20 @@ def post_event_to_webhook_ee(self: Task, event: Dict[str, Any], team_id: int, si # REST hooks if is_zapier_available: action.on_perform(ephemeral_postgres_event) - statsd.Counter("%s_posthog_cloud_hooks_rest_fired" % (settings.STATSD_PREFIX,)).increment() # webhooks - if not team.slack_incoming_webhook: - continue - message_text, message_markdown = get_formatted_message(action, ephemeral_postgres_event, site_url,) - if determine_webhook_type(team) == "slack": - message = { - "text": message_text, - "blocks": [{"type": "section", "text": {"type": "mrkdwn", "text": message_markdown},},], - } - else: - message = { - "text": message_markdown, - } - statsd.Counter("%s_posthog_cloud_hooks_web_fired" % (settings.STATSD_PREFIX,)).increment() - requests.post(team.slack_incoming_webhook, verify=False, json=message) + if team.slack_incoming_webhook and action.post_to_slack: + message_text, message_markdown = get_formatted_message(action, ephemeral_postgres_event, site_url) + if determine_webhook_type(team) == "slack": + message = { + "text": message_text, + "blocks": [{"type": "section", "text": {"type": "mrkdwn", "text": message_markdown}}], + } + else: + message = { + "text": message_markdown, + } + statsd.Counter("%s_posthog_cloud_hooks_web_fired" % (settings.STATSD_PREFIX)).increment() + requests.post(team.slack_incoming_webhook, verify=False, json=message) except: raise finally: