7
7
from collections import Counter
8
8
from collections .abc import Sequence
9
9
from functools import cached_property
10
- from typing import Any , Literal , NotRequired , TypedDict
10
+ from typing import Any , Literal
11
11
12
12
import msgpack
13
13
import sentry_sdk
16
16
from sentry_ophio .enhancers import Component as RustFrame
17
17
from sentry_ophio .enhancers import Enhancements as RustEnhancements
18
18
19
- from sentry import projectoptions
20
19
from sentry .grouping .component import FrameGroupingComponent , StacktraceGroupingComponent
21
20
from sentry .stacktraces .functions import set_in_app
22
21
from sentry .utils .safe import get_path , set_path
23
22
24
23
from .exceptions import InvalidEnhancerConfig
25
24
from .matchers import create_match_frame
26
25
from .parser import parse_enhancements
27
- from .rules import EnhancementRule , EnhancementRuleDict
26
+ from .rules import EnhancementRule
28
27
29
28
logger = logging .getLogger (__name__ )
30
29
@@ -134,13 +133,6 @@ def keep_profiling_rules(config: str) -> str:
134
133
return "\n " .join (filtered_rules )
135
134
136
135
137
- class EnhancementsDict (TypedDict ):
138
- id : str | None
139
- bases : list [str ]
140
- latest : bool
141
- rules : NotRequired [list [EnhancementRuleDict ]]
142
-
143
-
144
136
class Enhancements :
145
137
# NOTE: You must add a version to ``VERSIONS`` any time attributes are added
146
138
# to this class, s.t. no enhancements lacking these attributes are loaded
@@ -178,9 +170,10 @@ def apply_category_and_updated_in_app_to_frames(
178
170
"""
179
171
# TODO: Fix this type to list[MatchFrame] once it's fixed in ophio
180
172
match_frames : list [Any ] = [create_match_frame (frame , platform ) for frame in frames ]
173
+ rust_exception_data = make_rust_exception_data (exception_data )
181
174
182
175
category_and_in_app_results = self .rust_enhancements .apply_modifications_to_frames (
183
- match_frames , make_rust_exception_data ( exception_data )
176
+ match_frames , rust_exception_data
184
177
)
185
178
186
179
for frame , (category , in_app ) in zip (frames , category_and_in_app_results ):
@@ -209,13 +202,14 @@ def assemble_stacktrace_component(
209
202
match_frames : list [Any ] = [create_match_frame (frame , platform ) for frame in frames ]
210
203
211
204
rust_frames = [RustFrame (contributes = c .contributes ) for c in frame_components ]
205
+ rust_exception_data = make_rust_exception_data (exception_data )
212
206
213
207
# Modify the rust frames by applying +group/-group rules and getting hints for both those
214
208
# changes and the `in_app` changes applied by earlier in the ingestion process by
215
209
# `apply_category_and_updated_in_app_to_frames`. Also, get `hint` and `contributes` values
216
210
# for the overall stacktrace (returned in `rust_results`).
217
211
rust_stacktrace_results = self .rust_enhancements .assemble_stacktrace_component (
218
- match_frames , make_rust_exception_data ( exception_data ) , rust_frames
212
+ match_frames , rust_exception_data , rust_frames
219
213
)
220
214
221
215
# Tally the number of each type of frame in the stacktrace. Later on, this will allow us to
@@ -292,19 +286,6 @@ def assemble_stacktrace_component(
292
286
293
287
return stacktrace_component
294
288
295
- def as_dict (self , with_rules : bool = False ) -> EnhancementsDict :
296
- rv : EnhancementsDict = {
297
- "id" : self .id ,
298
- "bases" : self .bases ,
299
- "latest" : projectoptions .lookup_well_known_key (
300
- "sentry:grouping_enhancements_base"
301
- ).get_default (epoch = projectoptions .LATEST_EPOCH )
302
- == self .id ,
303
- }
304
- if with_rules :
305
- rv ["rules" ] = [x .as_dict () for x in self .rules ]
306
- return rv
307
-
308
289
def _to_config_structure (self ) -> list [Any ]:
309
290
# TODO: Can we switch this to a tuple so we can type it more exactly?
310
291
return [
@@ -318,7 +299,9 @@ def base64_string(self) -> str:
318
299
"""A base64 string representation of the enhancements object"""
319
300
pickled = msgpack .dumps (self ._to_config_structure ())
320
301
compressed_pickle = zstandard .compress (pickled )
321
- return base64 .urlsafe_b64encode (compressed_pickle ).decode ("ascii" ).strip ("=" )
302
+ base64_bytes = base64 .urlsafe_b64encode (compressed_pickle ).strip (b"=" )
303
+ base64_str = base64_bytes .decode ("ascii" )
304
+ return base64_str
322
305
323
306
@classmethod
324
307
def _from_config_structure (
@@ -339,9 +322,12 @@ def _from_config_structure(
339
322
@classmethod
340
323
def from_base64_string (cls , base64_string : str | bytes ) -> Enhancements :
341
324
"""Convert a base64 string into an `Enhancements` object"""
342
- if isinstance (base64_string , str ):
343
- base64_string = base64_string .encode ("ascii" , "ignore" )
344
- padded_bytes = base64_string + b"=" * (4 - (len (base64_string ) % 4 ))
325
+ bytes_str = (
326
+ base64_string .encode ("ascii" , "ignore" )
327
+ if isinstance (base64_string , str )
328
+ else base64_string
329
+ )
330
+ padded_bytes = bytes_str + b"=" * (4 - (len (bytes_str ) % 4 ))
345
331
try :
346
332
compressed_pickle = base64 .urlsafe_b64decode (padded_bytes )
347
333
@@ -351,8 +337,9 @@ def from_base64_string(cls, base64_string: str | bytes) -> Enhancements:
351
337
pickled = zlib .decompress (compressed_pickle )
352
338
353
339
rust_enhancements = get_rust_enhancements ("config_structure" , pickled )
340
+ config_structure = msgpack .loads (pickled , raw = False )
354
341
355
- return cls ._from_config_structure (msgpack . loads ( pickled , raw = False ) , rust_enhancements )
342
+ return cls ._from_config_structure (config_structure , rust_enhancements )
356
343
except (LookupError , AttributeError , TypeError , ValueError ) as e :
357
344
raise ValueError ("invalid stack trace rule config: %s" % e )
358
345
0 commit comments