Skip to content

Conversation

@bmckerry
Copy link
Member

@bmckerry bmckerry commented Nov 5, 2025

This PR adds the option to also configure tags set in the metrics backend via add_global_tags to also be set in the sentry_sdk.

@bmckerry bmckerry requested a review from untitaker November 5, 2025 17:56
@sentry
Copy link

sentry bot commented Nov 5, 2025

🔍 Existing Issues For Review

Your pull request is modifying functions with the following pre-existing issues:

📄 File: src/sentry/runner/commands/run.py

Function Unhandled Issue
basic_consumer AssertionError sentry.utils.kafka in run_processo...
Event Count: 4

@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Nov 5, 2025

def _add_global_tags(_all_threads: bool = False, **tags: TagValue) -> list[Tags]:
def _add_global_tags(
_all_threads: bool = False, set_sentry_tags: bool = False, **tags: TagValue
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
_all_threads: bool = False, set_sentry_tags: bool = False, **tags: TagValue
_all_threads: bool = False, _set_sentry_tags: bool = False, **tags: TagValue

there is a convention to name all parameters with underscores so they don't collide with actual tags

@bmckerry bmckerry force-pushed the ben/global-tags-sdk branch from 16aa284 to dc65c6d Compare November 5, 2025 18:17
@codecov
Copy link

codecov bot commented Nov 5, 2025

Codecov Report

❌ Patch coverage is 62.50000% with 6 lines in your changes missing coverage. Please review.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
src/sentry/metrics/middleware.py 75.00% 3 Missing ⚠️
src/sentry/sentry_metrics/configuration.py 0.00% 1 Missing ⚠️
src/sentry/tasks/store.py 0.00% 1 Missing ⚠️
src/sentry/utils/arroyo.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #102795      +/-   ##
===========================================
- Coverage   80.58%    79.78%   -0.81%     
===========================================
  Files        8938      8962      +24     
  Lines      391188    405154   +13966     
  Branches    24884     24884              
===========================================
+ Hits       315234    323244    +8010     
- Misses      75584     81540    +5956     
  Partials      370       370              

See docstring of `add_global_tags` for how those two methods interact.
"""
stack = _add_global_tags(_all_threads=_all_threads, **tags)
stack = _add_global_tags(all_threads=all_threads, set_sentry_tags=set_sentry_tags, tags=tags)
Copy link
Contributor

Choose a reason for hiding this comment

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

Bug: Sentry Tags Escape Global Context Scope

The global_tags context manager doesn't revert sentry_sdk tags when set_sentry_tags=True is used. When exiting the context, only the metrics tag stack is cleaned up (line 110), but tags set via sentry_sdk.set_tags() (line 69) remain permanently set. This creates inconsistent behavior where metric tags are scoped but sentry tags leak beyond the context, violating the documented promise to "revert all tag changes upon exit."

Fix in Cursor Fix in Web

Copy link
Member

Choose a reason for hiding this comment

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

can't be bothered to figure out how to revert sentry tags. it's difficult to do cross-thread and APIs keep getting deprecated

@untitaker untitaker merged commit bdf5b3e into master Nov 13, 2025
66 checks passed
@untitaker untitaker deleted the ben/global-tags-sdk branch November 13, 2025 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants