@@ -144,6 +144,7 @@ def __init__(
144144 exception_autocapture_integrations = None ,
145145 project_root = None ,
146146 privacy_mode = False ,
147+ before_send = None ,
147148 ):
148149 self .queue = queue .Queue (max_queue_size )
149150
@@ -199,6 +200,15 @@ def __init__(
199200 else :
200201 self .log .setLevel (logging .WARNING )
201202
203+ if before_send is not None :
204+ if callable (before_send ):
205+ self .before_send = before_send
206+ else :
207+ self .log .warning ("before_send is not callable, it will be ignored" )
208+ self .before_send = None
209+ else :
210+ self .before_send = None
211+
202212 if self .enable_exception_autocapture :
203213 self .exception_capture = ExceptionCapture (
204214 self , integrations = self .exception_autocapture_integrations
@@ -744,6 +754,18 @@ def _enqueue(self, msg, disable_geoip):
744754 msg ["distinct_id" ] = stringify_id (msg .get ("distinct_id" , None ))
745755
746756 msg = clean (msg )
757+
758+ if self .before_send :
759+ try :
760+ modified_msg = self .before_send (msg )
761+ if modified_msg is None :
762+ self .log .debug ("Event dropped by before_send callback" )
763+ return True , None
764+ msg = modified_msg
765+ except Exception as e :
766+ self .log .exception (f"Error in before_send callback: { e } " )
767+ # Continue with the original message if callback fails
768+
747769 self .log .debug ("queueing: %s" , msg )
748770
749771 # if send is False, return msg as if it was successfully queued
0 commit comments