@@ -12,6 +12,7 @@ import type {
1212 Event ,
1313 EventDropReason ,
1414 EventHint ,
15+ EventProcessor ,
1516 Integration ,
1617 IntegrationClass ,
1718 Outcome ,
@@ -43,6 +44,7 @@ import {
4344
4445import { getEnvelopeEndpointWithUrlEncodedAuth } from './api' ;
4546import { createEventEnvelope , createSessionEnvelope } from './envelope' ;
47+ import { notifyEventProcessors } from './eventProcessors' ;
4648import type { IntegrationIndex } from './integration' ;
4749import { setupIntegration , setupIntegrations } from './integration' ;
4850import type { Scope } from './scope' ;
@@ -107,6 +109,8 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
107109 // eslint-disable-next-line @typescript-eslint/ban-types
108110 private _hooks : Record < string , Function [ ] > ;
109111
112+ private _eventProcessors : EventProcessor [ ] ;
113+
110114 /**
111115 * Initializes this client instance.
112116 *
@@ -119,6 +123,7 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
119123 this . _numProcessing = 0 ;
120124 this . _outcomes = { } ;
121125 this . _hooks = { } ;
126+ this . _eventProcessors = [ ] ;
122127
123128 if ( options . dsn ) {
124129 this . _dsn = makeDsn ( options . dsn ) ;
@@ -280,6 +285,11 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
280285 } ) ;
281286 }
282287
288+ /** @inheritDoc */
289+ public addEventProcessor ( eventProcessor : EventProcessor ) : void {
290+ this . _eventProcessors . push ( eventProcessor ) ;
291+ }
292+
283293 /**
284294 * Sets up the integrations
285295 */
@@ -545,36 +555,41 @@ export abstract class BaseClient<O extends ClientOptions> implements Client<O> {
545555
546556 this . emit ( 'preprocessEvent' , event , hint ) ;
547557
548- return prepareEvent ( options , event , hint , scope ) . then ( evt => {
549- if ( evt === null ) {
550- return evt ;
551- }
558+ return prepareEvent ( options , event , hint , scope )
559+ . then ( evt => {
560+ // Process client-scoped event processors
561+ return notifyEventProcessors ( this . _eventProcessors , evt , hint ) ;
562+ } )
563+ . then ( evt => {
564+ if ( evt === null ) {
565+ return evt ;
566+ }
552567
553- // If a trace context is not set on the event, we use the propagationContext set on the event to
554- // generate a trace context. If the propagationContext does not have a dynamic sampling context, we
555- // also generate one for it.
556- const { propagationContext } = evt . sdkProcessingMetadata || { } ;
557- const trace = evt . contexts && evt . contexts . trace ;
558- if ( ! trace && propagationContext ) {
559- const { traceId : trace_id , spanId, parentSpanId, dsc } = propagationContext as PropagationContext ;
560- evt . contexts = {
561- trace : {
562- trace_id,
563- span_id : spanId ,
564- parent_span_id : parentSpanId ,
565- } ,
566- ...evt . contexts ,
567- } ;
568+ // If a trace context is not set on the event, we use the propagationContext set on the event to
569+ // generate a trace context. If the propagationContext does not have a dynamic sampling context, we
570+ // also generate one for it.
571+ const { propagationContext } = evt . sdkProcessingMetadata || { } ;
572+ const trace = evt . contexts && evt . contexts . trace ;
573+ if ( ! trace && propagationContext ) {
574+ const { traceId : trace_id , spanId, parentSpanId, dsc } = propagationContext as PropagationContext ;
575+ evt . contexts = {
576+ trace : {
577+ trace_id,
578+ span_id : spanId ,
579+ parent_span_id : parentSpanId ,
580+ } ,
581+ ...evt . contexts ,
582+ } ;
568583
569- const dynamicSamplingContext = dsc ? dsc : getDynamicSamplingContextFromClient ( trace_id , this , scope ) ;
584+ const dynamicSamplingContext = dsc ? dsc : getDynamicSamplingContextFromClient ( trace_id , this , scope ) ;
570585
571- evt . sdkProcessingMetadata = {
572- dynamicSamplingContext,
573- ...evt . sdkProcessingMetadata ,
574- } ;
575- }
576- return evt ;
577- } ) ;
586+ evt . sdkProcessingMetadata = {
587+ dynamicSamplingContext,
588+ ...evt . sdkProcessingMetadata ,
589+ } ;
590+ }
591+ return evt ;
592+ } ) ;
578593 }
579594
580595 /**
0 commit comments