diff --git a/src/sentry/utils/sdk.py b/src/sentry/utils/sdk.py index 8bd776fe7039d3..3c83d4b03c1e9d 100644 --- a/src/sentry/utils/sdk.py +++ b/src/sentry/utils/sdk.py @@ -1,5 +1,6 @@ import copy import inspect +import logging import random import sentry_sdk @@ -18,6 +19,8 @@ from sentry.utils.db import DjangoAtomicIntegration from sentry.utils.rust import RustInfoIntegration +logger = logging.getLogger(__name__) + UNSAFE_FILES = ( "sentry/event_manager.py", "sentry/tasks/process_buffer.py", @@ -445,11 +448,38 @@ def _kwargs_into_scope(self, scope, extra=None, tags=None, fingerprint=None, req scope.fingerprint = fingerprint +def check_tag(tag_key: str, expected_value: str) -> bool: + """Detect a tag already set and being different than what we expect. + + This function checks if a tag has been already been set and if it differs + from what we want to set it to. + """ + with configure_scope() as scope: + if scope._tags and tag_key in scope._tags and scope._tags[tag_key] != expected_value: + extra = { + f"previous_{tag_key}": scope._tags[tag_key], + f"new_{tag_key}": expected_value, + } + logger.warning(f"Tag already set and different ({tag_key}).", extra=extra) + # This can be used to find errors that may have been mistagged + scope.set_tag("possible_mistag", True) + return True + + +# We have some events being tagged with organization.slug even when we don't have such info +def log_if_tags_already_set(org_id: int, org_slug: str) -> None: + """If the tags are already set and differ, report them as a Sentry error.""" + if check_tag("organization", org_id) or check_tag("organization.slug", org_slug): + logger.error("Intentional error for investigation. See WOR-2464.") + + def bind_organization_context(organization): helper = settings.SENTRY_ORGANIZATION_CONTEXT_HELPER # XXX(dcramer): this is duplicated in organizationContext.jsx on the frontend with sentry_sdk.configure_scope() as scope: + log_if_tags_already_set(organization.id, organization.slug) + scope.set_tag("organization", organization.id) scope.set_tag("organization.slug", organization.slug) scope.set_context("organization", {"id": organization.id, "slug": organization.slug})