Skip to content

Commit aba9936

Browse files
grichacodex
andcommitted
ref(viewer-context): Centralize org binding hook
Update the viewer context organization id inside bind_organization_context instead of repeating the same call at each binding site. This keeps request-scoped enrichment behavior the same while making the canonical organization binding hook responsible for both SDK scope tags and ambient viewer context updates. Co-Authored-By: Codex <codex@openai.com>
1 parent 1adfd7a commit aba9936

File tree

6 files changed

+2
-12
lines changed

6 files changed

+2
-12
lines changed

src/sentry/api/bases/organization.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from sentry.utils.hashlib import hash_values
4040
from sentry.utils.numbers import format_grouped_length
4141
from sentry.utils.sdk import bind_organization_context, set_span_attribute
42-
from sentry.viewer_context import set_viewer_context_organization
4342

4443

4544
class NoProjects(Exception):
@@ -318,7 +317,6 @@ def convert_args(
318317

319318
# Used for API access logs
320319
request._request.organization = organization_context.organization
321-
set_viewer_context_organization(organization_context.organization.id)
322320

323321
return (args, kwargs)
324322

@@ -640,7 +638,6 @@ def convert_args(
640638
bind_organization_context(organization)
641639

642640
request._request.organization = organization
643-
set_viewer_context_organization(organization.id)
644641

645642
# Track the 'active' organization when the request came from
646643
# a cookie based agent (react app)

src/sentry/api/bases/project.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from sentry.models.project import Project
2121
from sentry.models.projectredirect import ProjectRedirect
2222
from sentry.utils.sdk import Scope, bind_organization_context
23-
from sentry.viewer_context import set_viewer_context_organization
2423

2524
from .organization import OrganizationPermission
2625

@@ -212,7 +211,6 @@ def convert_args(
212211
request._request.organization = (
213212
project.organization
214213
) # XXX: we should not be stuffing random attributes into HttpRequest
215-
set_viewer_context_organization(project.organization.id)
216214

217215
kwargs["project"] = project
218216
return (args, kwargs)

src/sentry/api/bases/team.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from sentry.api.exceptions import ResourceDoesNotExist
1010
from sentry.models.team import Team, TeamStatus
1111
from sentry.utils.sdk import bind_organization_context
12-
from sentry.viewer_context import set_viewer_context_organization
1312

1413
from .organization import OrganizationPermission
1514

@@ -68,7 +67,6 @@ def convert_args(
6867
bind_organization_context(team.organization)
6968

7069
request._request.organization = team.organization
71-
set_viewer_context_organization(team.organization.id)
7270

7371
kwargs["team"] = team
7472
return (args, kwargs)

src/sentry/issues/endpoints/bases/group.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from sentry.models.grouplink import GroupLink
2121
from sentry.models.organization import Organization
2222
from sentry.utils.sdk import bind_organization_context
23-
from sentry.viewer_context import set_viewer_context_organization
2423

2524
logger = logging.getLogger(__name__)
2625

@@ -78,7 +77,6 @@ def convert_args(
7877
bind_organization_context(organization)
7978

8079
request._request.organization = organization
81-
set_viewer_context_organization(organization.id)
8280
else:
8381
organization = None
8482

@@ -103,7 +101,6 @@ def convert_args(
103101
raise ResourceDoesNotExist
104102

105103
request._request.organization = group.project.organization
106-
set_viewer_context_organization(group.project.organization.id)
107104

108105
kwargs["group"] = group
109106

src/sentry/monitors/endpoints/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from sentry.models.project import Project
1515
from sentry.monitors.models import CheckInStatus, Monitor, MonitorCheckIn, MonitorEnvironment
1616
from sentry.utils.sdk import Scope, bind_organization_context
17-
from sentry.viewer_context import set_viewer_context_organization
1817

1918
DEPRECATED_INGEST_API_MESSAGE = "We have removed this deprecated API. Please migrate to using DSN instead: https://docs.sentry.io/product/crons/legacy-endpoint-migration/#am-i-using-legacy-endpoints"
2019

@@ -72,7 +71,6 @@ def convert_args(
7271
bind_organization_context(project.organization)
7372

7473
request._request.organization = project.organization
75-
set_viewer_context_organization(project.organization.id)
7674

7775
kwargs["organization"] = organization
7876
kwargs["project"] = project

src/sentry/utils/sdk.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from sentry.utils import metrics
3131
from sentry.utils.db import DjangoAtomicIntegration
3232
from sentry.utils.rust import RustInfoIntegration
33+
from sentry.viewer_context import set_viewer_context_organization
3334

3435
# Can't import models in utils because utils should be the bottom of the food chain
3536
if TYPE_CHECKING:
@@ -683,6 +684,7 @@ def bind_organization_context(organization: Organization | RpcOrganization) -> N
683684
helper = settings.SENTRY_ORGANIZATION_CONTEXT_HELPER
684685

685686
scope = sentry_sdk.get_isolation_scope()
687+
set_viewer_context_organization(organization.id)
686688

687689
# XXX(dcramer): this is duplicated in organizationContext.jsx on the frontend
688690
with sentry_sdk.start_span(op="other", name="bind_organization_context"):

0 commit comments

Comments
 (0)