Skip to content

Commit bdf5b3e

Browse files
bmckerryuntitaker
andauthored
feat(metrics): add global tags to sentry_sdk tags as well (#102795)
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. --------- Co-authored-by: Markus Unterwaditzer <[email protected]>
1 parent 1251990 commit bdf5b3e

File tree

8 files changed

+36
-20
lines changed

8 files changed

+36
-20
lines changed

src/sentry/consumers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ def create_with_partitions(self, commit, partitions):
667667
# Update the min_partition global tag based on current partition assignment
668668
if partitions:
669669
min_partition = min(p.index for p in partitions)
670-
add_global_tags(min_partition=str(min_partition))
670+
add_global_tags(tags={"min_partition": str(min_partition)})
671671

672672
return self.inner.create_with_partitions(commit, partitions)
673673

src/sentry/metrics/middleware.py

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
from contextlib import contextmanager
33
from threading import local
44

5+
import sentry_sdk
56
from django.conf import settings
67

7-
from sentry.metrics.base import MetricsBackend, MutableTags, Tags, TagValue
8+
from sentry.metrics.base import MetricsBackend, MutableTags, Tags
89

910
_BAD_TAGS = frozenset(["event", "project", "group"])
1011
_NOT_BAD_TAGS = frozenset(
@@ -52,45 +53,57 @@ def _filter_tags(key: str, tags: MutableTags) -> MutableTags:
5253
_GLOBAL_TAGS: list[Tags] = []
5354

5455

55-
def _add_global_tags(_all_threads: bool = False, **tags: TagValue) -> list[Tags]:
56-
if _all_threads:
56+
def _add_global_tags(
57+
all_threads: bool = False, set_sentry_tags: bool = False, tags: Tags | None = None
58+
) -> list[Tags]:
59+
if all_threads:
5760
stack = _GLOBAL_TAGS
5861
else:
5962
if not hasattr(_THREAD_LOCAL_TAGS, "stack"):
6063
stack = _THREAD_LOCAL_TAGS.stack = []
6164
else:
6265
stack = _THREAD_LOCAL_TAGS.stack
6366

67+
if tags is None:
68+
tags = {}
6469
stack.append(tags)
70+
if set_sentry_tags:
71+
sentry_sdk.set_tags(tags)
6572
return stack
6673

6774

68-
def add_global_tags(_all_threads: bool = False, **tags: TagValue) -> None:
75+
def add_global_tags(
76+
all_threads: bool = False, set_sentry_tags: bool = False, tags: Tags | None = None
77+
) -> None:
6978
"""
7079
Set multiple metric tags onto the global or thread-local stack which then
7180
apply to all metrics.
81+
If `set_sentry_tags` is True, also sets the given tags in sentry_sdk.
7282
7383
When used in combination with the `global_tags` context manager,
74-
`add_global_tags` is reverted in any wrapping invocaation of `global_tags`.
84+
`add_global_tags` is reverted in any wrapping invocation of `global_tags`.
85+
However, tags set in the current sentry_sdk instance will remain set there.
7586
For example::
7687
77-
with global_tags(tag_a=123):
78-
add_global_tags(tag_b=123)
88+
with global_tags(tags={"tag_a": 123}):
89+
add_global_tags(tags={"tag_b": 123})
7990
8091
# tag_b is no longer visible
8192
"""
82-
_add_global_tags(_all_threads=_all_threads, **tags)
93+
_add_global_tags(all_threads=all_threads, set_sentry_tags=set_sentry_tags, tags=tags)
8394

8495

8596
@contextmanager
86-
def global_tags(_all_threads: bool = False, **tags: TagValue) -> Generator[None]:
97+
def global_tags(
98+
all_threads: bool = False, set_sentry_tags: bool = False, tags: Tags | None = None
99+
) -> Generator[None]:
87100
"""
88101
The context manager version of `add_global_tags` that reverts all tag
89102
changes upon exit.
90103
91104
See docstring of `add_global_tags` for how those two methods interact.
92105
"""
93-
stack = _add_global_tags(_all_threads=_all_threads, **tags)
106+
stack = _add_global_tags(all_threads=all_threads, set_sentry_tags=set_sentry_tags, tags=tags)
94107
old_len = len(stack) - 1
95108

96109
try:

src/sentry/runner/commands/run.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,12 @@ def basic_consumer(
580580
logging.getLogger("arroyo").setLevel(log_level.upper())
581581

582582
add_global_tags(
583-
kafka_topic=topic, consumer_group=options["group_id"], kafka_slice_id=kafka_slice_id
583+
set_sentry_tags=True,
584+
tags={
585+
"kafka_topic": topic,
586+
"consumer_group": options["group_id"],
587+
"kafka_slice_id": kafka_slice_id,
588+
},
584589
)
585590

586591
processor = get_stream_processor(

src/sentry/sentry_metrics/configuration.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,4 @@ def initialize_main_process_state(config: MetricsIngestConfiguration) -> None:
167167

168168
from sentry.utils.metrics import add_global_tags
169169

170-
global_tag_map = {"pipeline": config.internal_metrics_tag or ""}
171-
172-
add_global_tags(_all_threads=True, **global_tag_map)
170+
add_global_tags(all_threads=True, tags={"pipeline": config.internal_metrics_tag or ""})

src/sentry/tasks/store.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ def _do_save_event(
533533
event_data=data,
534534
)
535535

536-
with metrics.global_tags(event_type=event_type):
536+
with metrics.global_tags(tags={"event_type": event_type}):
537537
if event_id is None and data is not None:
538538
event_id = data["event_id"]
539539

src/sentry/utils/arroyo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def _initialize_arroyo_subprocess(initializer: Callable[[], None] | None, tags:
126126
from sentry.metrics.middleware import add_global_tags
127127

128128
# Inherit global tags from the parent process
129-
add_global_tags(_all_threads=True, **tags)
129+
add_global_tags(all_threads=True, tags=tags)
130130

131131

132132
def initialize_arroyo_main() -> None:

tests/sentry/metrics/test_middleware.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def test_filter_tags_prod() -> None:
3131
def test_global() -> None:
3232
assert get_current_global_tags() == {}
3333

34-
with global_tags(tag_a=123):
34+
with global_tags(tags={"tag_a": 123}):
3535
assert get_current_global_tags() == {"tag_a": 123}
36-
add_global_tags(tag_b=123)
36+
add_global_tags(tags={"tag_b": 123})
3737

3838
assert get_current_global_tags() == {"tag_a": 123, "tag_b": 123}
3939

tests/sentry/sentry_metrics/test_parallel_indexer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
def reset_global_metrics_state():
2929
# running a MetricsConsumerStrategyFactory has a side-effect of mutating
3030
# global metrics tags
31-
with global_tags(_all_threads=True):
31+
with global_tags(all_threads=True):
3232
yield
3333

3434

0 commit comments

Comments
 (0)