@@ -22,6 +22,9 @@ def __init__(
2222 self .session_id : Optional [str ] = None
2323 self .distinct_id : Optional [str ] = None
2424 self .tags : Dict [str , Any ] = {}
25+ self .capture_exception_code_variables : Optional [bool ] = None
26+ self .code_variables_mask_patterns : Optional [list ] = None
27+ self .code_variables_ignore_patterns : Optional [list ] = None
2528
2629 def set_session_id (self , session_id : str ):
2730 self .session_id = session_id
@@ -32,6 +35,15 @@ def set_distinct_id(self, distinct_id: str):
3235 def add_tag (self , key : str , value : Any ):
3336 self .tags [key ] = value
3437
38+ def set_capture_exception_code_variables (self , enabled : bool ):
39+ self .capture_exception_code_variables = enabled
40+
41+ def set_code_variables_mask_patterns (self , mask_patterns : list ):
42+ self .code_variables_mask_patterns = mask_patterns
43+
44+ def set_code_variables_ignore_patterns (self , ignore_patterns : list ):
45+ self .code_variables_ignore_patterns = ignore_patterns
46+
3547 def get_parent (self ):
3648 return self .parent
3749
@@ -59,6 +71,27 @@ def collect_tags(self) -> Dict[str, Any]:
5971 tags .update (new_tags )
6072 return tags
6173
74+ def get_capture_exception_code_variables (self ) -> Optional [bool ]:
75+ if self .capture_exception_code_variables is not None :
76+ return self .capture_exception_code_variables
77+ if self .parent is not None and not self .fresh :
78+ return self .parent .get_capture_exception_code_variables ()
79+ return None
80+
81+ def get_code_variables_mask_patterns (self ) -> Optional [list ]:
82+ if self .code_variables_mask_patterns is not None :
83+ return self .code_variables_mask_patterns
84+ if self .parent is not None and not self .fresh :
85+ return self .parent .get_code_variables_mask_patterns ()
86+ return None
87+
88+ def get_code_variables_ignore_patterns (self ) -> Optional [list ]:
89+ if self .code_variables_ignore_patterns is not None :
90+ return self .code_variables_ignore_patterns
91+ if self .parent is not None and not self .fresh :
92+ return self .parent .get_code_variables_ignore_patterns ()
93+ return None
94+
6295
6396_context_stack : contextvars .ContextVar [Optional [ContextScope ]] = contextvars .ContextVar (
6497 "posthog_context_stack" , default = None
@@ -243,6 +276,54 @@ def get_context_distinct_id() -> Optional[str]:
243276 return None
244277
245278
279+ def set_capture_exception_code_variables_context (enabled : bool ) -> None :
280+ """
281+ Set whether code variables are captured for the current context.
282+ """
283+ current_context = _get_current_context ()
284+ if current_context :
285+ current_context .set_capture_exception_code_variables (enabled )
286+
287+
288+ def set_code_variables_mask_patterns_context (mask_patterns : list ) -> None :
289+ """
290+ Variable names matching these patterns will be masked with *** when capturing code variables.
291+ """
292+ current_context = _get_current_context ()
293+ if current_context :
294+ current_context .set_code_variables_mask_patterns (mask_patterns )
295+
296+
297+ def set_code_variables_ignore_patterns_context (ignore_patterns : list ) -> None :
298+ """
299+ Variable names matching these patterns will be ignored completely when capturing code variables.
300+ """
301+ current_context = _get_current_context ()
302+ if current_context :
303+ current_context .set_code_variables_ignore_patterns (ignore_patterns )
304+
305+
306+ def get_capture_exception_code_variables_context () -> Optional [bool ]:
307+ current_context = _get_current_context ()
308+ if current_context :
309+ return current_context .get_capture_exception_code_variables ()
310+ return None
311+
312+
313+ def get_code_variables_mask_patterns_context () -> Optional [list ]:
314+ current_context = _get_current_context ()
315+ if current_context :
316+ return current_context .get_code_variables_mask_patterns ()
317+ return None
318+
319+
320+ def get_code_variables_ignore_patterns_context () -> Optional [list ]:
321+ current_context = _get_current_context ()
322+ if current_context :
323+ return current_context .get_code_variables_ignore_patterns ()
324+ return None
325+
326+
246327F = TypeVar ("F" , bound = Callable [..., Any ])
247328
248329
0 commit comments