Skip to content

Commit 2855aa2

Browse files
committed
fix(webhooks): Reset some tags and context for JIRA & GHE
This is a follow up to #45139 for JIRA and Github enterprise. 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 This issues causes finding errors that are for the wrong organization when using `organization.slug`, thus, wasting time for engineers and customer support. This change only clears the invalid tag values to reduce the impact of this issue rather than fixing the root issue. Here's a [query](https://sentry.sentry.io/discover/results/?end=2023-03-04T04%3A59%3A59&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&start=2023-03-02T05%3A00%3A00&yAxis=count%28%29) showing transactions that should not have org values set. Fix [WOR-2464](https://getsentry.atlassian.net/browse/WOR-2464)
1 parent f706a95 commit 2855aa2

File tree

5 files changed

+29
-19
lines changed

5 files changed

+29
-19
lines changed

mypy.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ files = fixtures/mypy-stubs,
7979
src/sentry/integrations/slack/,
8080
src/sentry/integrations/message_builder.py,
8181
src/sentry/integrations/utils/atlassian_connect.py,
82+
src/sentry/integrations/utils/cleanup.py
8283
src/sentry/integrations/utils/code_mapping.py,
8384
src/sentry/integrations/utils/codecov.py,
8485
src/sentry/integrations/vsts/,

src/sentry/integrations/github/webhook.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
from django.views.decorators.csrf import csrf_exempt
1515
from django.views.generic import View
1616
from rest_framework.request import Request
17-
from sentry_sdk import configure_scope
1817

1918
from sentry import options
2019
from sentry.constants import ObjectStatus
20+
from sentry.integrations.utils.cleanup import clear_tags_and_context
2121
from sentry.models import (
2222
Commit,
2323
CommitAuthor,
@@ -37,23 +37,6 @@
3737
logger = logging.getLogger("sentry.webhooks")
3838

3939

40-
def clear_tags_and_context_if_already_set() -> None:
41-
"""Clear org tags and context since it should not be set"""
42-
reset_values = False
43-
with configure_scope() as scope:
44-
for tag in ["organization", "organization.slug"]:
45-
if tag in scope._tags:
46-
reset_values = True
47-
del scope._tags[tag]
48-
49-
if "organization" in scope._contexts:
50-
reset_values = True
51-
del scope._contexts["organization"]
52-
53-
if reset_values:
54-
logger.info("We've reset the context and tags.")
55-
56-
5740
class Webhook:
5841
provider = "github"
5942

@@ -474,7 +457,7 @@ def get_secret(self) -> str | None:
474457
raise NotImplementedError
475458

476459
def handle(self, request: Request) -> HttpResponse:
477-
clear_tags_and_context_if_already_set()
460+
clear_tags_and_context()
478461
secret = self.get_secret()
479462

480463
if secret is None:

src/sentry/integrations/github_enterprise/webhook.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
PullRequestEventWebhook,
1919
PushEventWebhook,
2020
)
21+
from sentry.integrations.utils.cleanup import clear_tags_and_context
2122
from sentry.models import Integration
2223
from sentry.utils import json
2324
from sentry.utils.sdk import configure_scope
@@ -112,6 +113,7 @@ def get_secret(self, event, host):
112113
return None
113114

114115
def handle(self, request: Request) -> Response:
116+
clear_tags_and_context()
115117
with configure_scope() as scope:
116118
meta = request.META
117119
try:

src/sentry/integrations/jira_server/webhooks.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from sentry.api.base import Endpoint, pending_silo_endpoint
99
from sentry.integrations.jira_server.utils import handle_assignee_change, handle_status_change
10+
from sentry.integrations.utils.cleanup import clear_tags_and_context
1011
from sentry.models import Integration
1112
from sentry.shared_integrations.exceptions import ApiError
1213
from sentry.utils import jwt
@@ -51,6 +52,7 @@ def dispatch(self, request: Request, *args, **kwargs) -> Response:
5152
return super().dispatch(request, *args, **kwargs)
5253

5354
def post(self, request: Request, token, *args, **kwargs) -> Response:
55+
clear_tags_and_context()
5456
extra = {}
5557
try:
5658
integration = get_integration_from_token(token)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import logging
2+
3+
from sentry_sdk import configure_scope
4+
5+
logger = logging.getLogger(__name__)
6+
7+
8+
def clear_tags_and_context() -> None:
9+
"""Clear certain tags and context since it should not be set."""
10+
reset_values = False
11+
with configure_scope() as scope:
12+
for tag in ["organization", "organization.slug"]:
13+
if tag in scope._tags:
14+
reset_values = True
15+
del scope._tags[tag]
16+
17+
if "organization" in scope._contexts:
18+
reset_values = True
19+
del scope._contexts["organization"]
20+
21+
if reset_values:
22+
logger.info("We've reset the context and tags.")

0 commit comments

Comments
 (0)