@@ -23,12 +23,14 @@ use crate::feature_flags::{match_feature_flag, FeatureFlag, FeatureFlagsResponse
2323use crate :: local_evaluation:: { AsyncFlagPoller , FlagCache , LocalEvaluationConfig , LocalEvaluator } ;
2424use crate :: { Error , Event } ;
2525
26+ #[ cfg( feature = "capture-v1" ) ]
27+ use super :: common:: apply_capture_defaults;
2628use super :: common:: {
27- already_reported, build_dedup_key , extract_flag_details , flag_called_event ,
28- flag_event_dedup_cache, local_record, remote_record_from_detail, DetailedFlagsResponse ,
29- FlagEventDedupCache ,
29+ already_reported, apply_before_send_hooks , build_dedup_key , extract_flag_details ,
30+ flag_called_event , flag_event_dedup_cache, local_record, remote_record_from_detail,
31+ DetailedFlagsResponse , FlagEventDedupCache ,
3032} ;
31- use super :: ClientOptions ;
33+ use super :: { BeforeSendHook , ClientOptions } ;
3234
3335#[ cfg( not( feature = "capture-v1" ) ) ]
3436async fn check_response ( response : reqwest:: Response ) -> Result < ( ) , Error > {
@@ -64,6 +66,7 @@ struct AsyncFlagEventHost {
6466 http_client : HttpClient ,
6567 options : ClientOptions ,
6668 capture_url : String ,
69+ before_send : Vec < BeforeSendHook > ,
6770 dedup_cache : FlagEventDedupCache ,
6871 /// Tokio runtime handle captured at host construction (which always runs
6972 /// inside the runtime that hosts `evaluate_flags`). This lets snapshot
@@ -85,6 +88,7 @@ impl AsyncFlagEventHost {
8588 http_client,
8689 options : options. clone ( ) ,
8790 capture_url,
91+ before_send : options. before_send . clone ( ) ,
8892 dedup_cache : flag_event_dedup_cache ( ) ,
8993 runtime : tokio:: runtime:: Handle :: current ( ) ,
9094 }
@@ -146,6 +150,9 @@ impl AsyncFlagEventHost {
146150 #[ cfg( not( feature = "capture-v1" ) ) ]
147151 fn spawn_ship_v0 ( & self , mut event : Event ) {
148152 event. prepare_for_v0 ( ) ;
153+ let Some ( event) = apply_before_send_hooks ( & self . before_send , event) else {
154+ return ;
155+ } ;
149156 let inner_event = InnerEvent :: new ( event, self . options . api_key . clone ( ) ) ;
150157 let payload = match serde_json:: to_string ( & inner_event) {
151158 Ok ( p) => p,
@@ -284,6 +291,12 @@ impl Client {
284291
285292 #[ cfg( feature = "capture-v1" ) ]
286293 {
294+ let mut event = event;
295+ let defaults = self . options . capture_defaults ( ) ;
296+ apply_capture_defaults ( & mut event, & defaults) ;
297+ let Some ( event) = apply_before_send_hooks ( & self . options . before_send , event) else {
298+ return Ok ( ( ) ) ;
299+ } ;
287300 return self . capture_v1 ( vec ! [ event] , false ) . await . map ( |_| ( ) ) ;
288301 }
289302
@@ -318,6 +331,17 @@ impl Client {
318331
319332 #[ cfg( feature = "capture-v1" ) ]
320333 {
334+ let defaults = self . options . capture_defaults ( ) ;
335+ let events: Vec < _ > = events
336+ . into_iter ( )
337+ . filter_map ( |mut event| {
338+ apply_capture_defaults ( & mut event, & defaults) ;
339+ apply_before_send_hooks ( & self . options . before_send , event)
340+ } )
341+ . collect ( ) ;
342+ if events. is_empty ( ) {
343+ return Ok ( ( ) ) ;
344+ }
321345 return self
322346 . capture_v1 ( events, historical_migration)
323347 . await
@@ -332,6 +356,9 @@ impl Client {
332356 async fn capture_v0 ( & self , mut event : Event ) -> Result < ( ) , Error > {
333357 let defaults = self . options . capture_defaults ( ) ;
334358 super :: v0_capture:: prepare_event ( & mut event, & defaults) ;
359+ let Some ( event) = apply_before_send_hooks ( & self . options . before_send , event) else {
360+ return Ok ( ( ) ) ;
361+ } ;
335362 let payload =
336363 super :: v0_capture:: build_capture_payload ( event, self . options . api_key . clone ( ) ) ?;
337364 let url = self . options . endpoints ( ) . build_url ( Endpoint :: Capture ) ;
@@ -346,12 +373,16 @@ impl Client {
346373 historical_migration : bool ,
347374 ) -> Result < ( ) , Error > {
348375 let defaults = self . options . capture_defaults ( ) ;
349- let payload = super :: v0_capture:: build_batch_payload (
376+ let Some ( payload) = super :: v0_capture:: build_batch_payload (
350377 events,
351378 self . options . api_key . clone ( ) ,
352379 historical_migration,
353380 & defaults,
354- ) ?;
381+ & self . options . before_send ,
382+ ) ?
383+ else {
384+ return Ok ( ( ) ) ;
385+ } ;
355386 let url = self . options . endpoints ( ) . build_url ( Endpoint :: Batch ) ;
356387 let ( body, encoding) = super :: v0_capture:: encode_body ( & self . options , payload) ;
357388 self . send_v0_with_retry ( & url, body, encoding) . await
0 commit comments