|
2 | 2 | from contextlib import contextmanager |
3 | 3 | from threading import local |
4 | 4 |
|
| 5 | +import sentry_sdk |
5 | 6 | from django.conf import settings |
6 | 7 |
|
7 | | -from sentry.metrics.base import MetricsBackend, MutableTags, Tags, TagValue |
| 8 | +from sentry.metrics.base import MetricsBackend, MutableTags, Tags |
8 | 9 |
|
9 | 10 | _BAD_TAGS = frozenset(["event", "project", "group"]) |
10 | 11 | _NOT_BAD_TAGS = frozenset( |
@@ -52,45 +53,57 @@ def _filter_tags(key: str, tags: MutableTags) -> MutableTags: |
52 | 53 | _GLOBAL_TAGS: list[Tags] = [] |
53 | 54 |
|
54 | 55 |
|
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: |
57 | 60 | stack = _GLOBAL_TAGS |
58 | 61 | else: |
59 | 62 | if not hasattr(_THREAD_LOCAL_TAGS, "stack"): |
60 | 63 | stack = _THREAD_LOCAL_TAGS.stack = [] |
61 | 64 | else: |
62 | 65 | stack = _THREAD_LOCAL_TAGS.stack |
63 | 66 |
|
| 67 | + if tags is None: |
| 68 | + tags = {} |
64 | 69 | stack.append(tags) |
| 70 | + if set_sentry_tags: |
| 71 | + sentry_sdk.set_tags(tags) |
65 | 72 | return stack |
66 | 73 |
|
67 | 74 |
|
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: |
69 | 78 | """ |
70 | 79 | Set multiple metric tags onto the global or thread-local stack which then |
71 | 80 | apply to all metrics. |
| 81 | + If `set_sentry_tags` is True, also sets the given tags in sentry_sdk. |
72 | 82 |
|
73 | 83 | 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. |
75 | 86 | For example:: |
76 | 87 |
|
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}) |
79 | 90 |
|
80 | 91 | # tag_b is no longer visible |
81 | 92 | """ |
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) |
83 | 94 |
|
84 | 95 |
|
85 | 96 | @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]: |
87 | 100 | """ |
88 | 101 | The context manager version of `add_global_tags` that reverts all tag |
89 | 102 | changes upon exit. |
90 | 103 |
|
91 | 104 | See docstring of `add_global_tags` for how those two methods interact. |
92 | 105 | """ |
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) |
94 | 107 | old_len = len(stack) - 1 |
95 | 108 |
|
96 | 109 | try: |
|
0 commit comments