Skip to content

Commit 76a4d84

Browse files
authored
chore: Investigating org slug already set to a different value (#45134)
We have events tagged with the following info even when it is not possible: - org slug - org id - org context (see screenshot) The only code that sets these three pieces of information is within the sdk.py module. This change will report a Sentry error (it won't abort the execution of the code). 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 these values set. There may be more transaction since I'm narrowing the query down to just `error.value:"Integration matching query does not exist."` This fix helps investigate: [WOR-2464](https://getsentry.atlassian.net/browse/WOR-2464) Screenshot showing org context being set: <img width="101" alt="image" src="https://user-images.githubusercontent.com/44410/221646367-9c07c4d5-5bd0-4bcd-89d0-1adf73f0468a.png">
1 parent 41ff6d0 commit 76a4d84

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/sentry/utils/sdk.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import inspect
3+
import logging
34
import random
45

56
import sentry_sdk
@@ -18,6 +19,8 @@
1819
from sentry.utils.db import DjangoAtomicIntegration
1920
from sentry.utils.rust import RustInfoIntegration
2021

22+
logger = logging.getLogger(__name__)
23+
2124
UNSAFE_FILES = (
2225
"sentry/event_manager.py",
2326
"sentry/tasks/process_buffer.py",
@@ -445,11 +448,38 @@ def _kwargs_into_scope(self, scope, extra=None, tags=None, fingerprint=None, req
445448
scope.fingerprint = fingerprint
446449

447450

451+
def check_tag(tag_key: str, expected_value: str) -> bool:
452+
"""Detect a tag already set and being different than what we expect.
453+
454+
This function checks if a tag has been already been set and if it differs
455+
from what we want to set it to.
456+
"""
457+
with configure_scope() as scope:
458+
if scope._tags and tag_key in scope._tags and scope._tags[tag_key] != expected_value:
459+
extra = {
460+
f"previous_{tag_key}": scope._tags[tag_key],
461+
f"new_{tag_key}": expected_value,
462+
}
463+
logger.warning(f"Tag already set and different ({tag_key}).", extra=extra)
464+
# This can be used to find errors that may have been mistagged
465+
scope.set_tag("possible_mistag", True)
466+
return True
467+
468+
469+
# We have some events being tagged with organization.slug even when we don't have such info
470+
def log_if_tags_already_set(org_id: int, org_slug: str) -> None:
471+
"""If the tags are already set and differ, report them as a Sentry error."""
472+
if check_tag("organization", org_id) or check_tag("organization.slug", org_slug):
473+
logger.error("Intentional error for investigation. See WOR-2464.")
474+
475+
448476
def bind_organization_context(organization):
449477
helper = settings.SENTRY_ORGANIZATION_CONTEXT_HELPER
450478

451479
# XXX(dcramer): this is duplicated in organizationContext.jsx on the frontend
452480
with sentry_sdk.configure_scope() as scope:
481+
log_if_tags_already_set(organization.id, organization.slug)
482+
453483
scope.set_tag("organization", organization.id)
454484
scope.set_tag("organization.slug", organization.slug)
455485
scope.set_context("organization", {"id": organization.id, "slug": organization.slug})

0 commit comments

Comments
 (0)