|
1 | 1 | import copy |
2 | 2 | import inspect |
| 3 | +import logging |
3 | 4 | import random |
4 | 5 |
|
5 | 6 | import sentry_sdk |
|
18 | 19 | from sentry.utils.db import DjangoAtomicIntegration |
19 | 20 | from sentry.utils.rust import RustInfoIntegration |
20 | 21 |
|
| 22 | +logger = logging.getLogger(__name__) |
| 23 | + |
21 | 24 | UNSAFE_FILES = ( |
22 | 25 | "sentry/event_manager.py", |
23 | 26 | "sentry/tasks/process_buffer.py", |
@@ -445,11 +448,38 @@ def _kwargs_into_scope(self, scope, extra=None, tags=None, fingerprint=None, req |
445 | 448 | scope.fingerprint = fingerprint |
446 | 449 |
|
447 | 450 |
|
| 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 | + |
448 | 476 | def bind_organization_context(organization): |
449 | 477 | helper = settings.SENTRY_ORGANIZATION_CONTEXT_HELPER |
450 | 478 |
|
451 | 479 | # XXX(dcramer): this is duplicated in organizationContext.jsx on the frontend |
452 | 480 | with sentry_sdk.configure_scope() as scope: |
| 481 | + log_if_tags_already_set(organization.id, organization.slug) |
| 482 | + |
453 | 483 | scope.set_tag("organization", organization.id) |
454 | 484 | scope.set_tag("organization.slug", organization.slug) |
455 | 485 | scope.set_context("organization", {"id": organization.id, "slug": organization.slug}) |
|
0 commit comments