Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/sentry/utils/sdk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy
import inspect
import logging
import random

import sentry_sdk
Expand All @@ -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",
Expand Down Expand Up @@ -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:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main difference with #45039 is that we also check for the tags being different ( 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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I believe would help most.

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})
Expand Down