From 7edc1f7cfb2072c2f7714c74f2e077bfe294e77c Mon Sep 17 00:00:00 2001 From: Armen Zambrano G Date: Mon, 27 Feb 2023 13:17:03 -0500 Subject: [PATCH] fix(github_webhooks): Reset org tags and context It seems that we're tagging events with information about organizations even when it's impossible to have any org info. We have events tagged with the following info even when it is not possible: - org slug - org id - org context The only code that sets these three pieces of information is within the sdk.py module (see #45134 for details). This issues causes finding errors that are for the wrong organization when using `organization.slug`, thus, wasting time for engineers and customer support. It is unclear if the root cause comes from a bug in the SDK or the way we tag things in our code base. This change only clears the invalid tag values to reduce the impact of this issue rather than fixing the root issue. A sample event can be seen [here](https://sentry.sentry.io/discover/sentry:99c8b9c9d2c241cc90e52c307faa45eb/?field=organization.slug&field=user.display&field=timestamp&name=Integration.DoesNotExist%3A+Integration+matching+query+does+not+exist.&project=1&query=issue%3ASENTRY-W7T&sort=-timestamp&statsPeriod=90d&yAxis=count%28%29) (private link) Here's a [query](https://sentry.sentry.io/discover/results/?field=transaction&field=count_unique%28organization.slug%29&field=count%28%29&name=Integration.DoesNotExist%3A+Integration+matching+query+does+not+exist.&project=1&query=%21organization.slug%3A%22%22+error.value%3A%22Integration+matching+query+does+not+exist.%22&sort=-transaction&statsPeriod=14d&yAxis=count%28%29) showing some transactions that should not have org values set. There may be more transaction since I'm narrowing the query down to just `error.value:"Integration matching query does not exist."` Improves [WOR-2464](https://getsentry.atlassian.net/browse/WOR-2464) --- src/sentry/integrations/github/webhook.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/sentry/integrations/github/webhook.py b/src/sentry/integrations/github/webhook.py index 7bee3f156b988e..f05c8e179eafb9 100644 --- a/src/sentry/integrations/github/webhook.py +++ b/src/sentry/integrations/github/webhook.py @@ -14,6 +14,7 @@ from django.views.decorators.csrf import csrf_exempt from django.views.generic import View from rest_framework.request import Request +from sentry_sdk import configure_scope from sentry import options from sentry.constants import ObjectStatus @@ -36,6 +37,23 @@ logger = logging.getLogger("sentry.webhooks") +def clear_tags_and_context_if_already_set() -> None: + """Clear org tags and context since it should not be set""" + reset_values = False + with configure_scope() as scope: + for tag in ["organization", "organization.slug"]: + if tag in scope._tags: + reset_values = True + del scope._tags[tag] + + if "organization" in scope._contexts: + reset_values = True + del scope._contexts["organization"] + + if reset_values: + logger.info("We've reset the context and tags.") + + class Webhook: provider = "github" @@ -456,6 +474,7 @@ def get_secret(self) -> str | None: raise NotImplementedError def handle(self, request: Request) -> HttpResponse: + clear_tags_and_context_if_already_set() secret = self.get_secret() if secret is None: