@@ -58,6 +58,7 @@ internal class PayloadSenderV2 : BackendCommComponentBase, IPayloadSender, IPayl
5858
5959 private readonly ElasticVersion _brokenActivationMethodVersion ;
6060 private readonly string _cachedActivationMethod ;
61+ private readonly bool _isEnabled ;
6162
6263 public PayloadSenderV2 (
6364 IApmLogger logger ,
@@ -73,27 +74,30 @@ public PayloadSenderV2(
7374 )
7475 : base ( isEnabled , logger , ThisClassName , service , configuration , httpMessageHandler )
7576 {
76- if ( ! isEnabled )
77- return ;
78-
7977 _logger = logger ? . Scoped ( ThisClassName + ( dbgName == null ? "" : $ " (dbgName: `{ dbgName } ')") ) ;
78+ _isEnabled = isEnabled ;
79+ if ( ! _isEnabled )
80+ _logger ? . Debug ( ) ? . Log ( $ "{ nameof ( PayloadSenderV2 ) } is not enabled, work loop won't be started.") ;
81+
8082 _payloadItemSerializer = new PayloadItemSerializer ( ) ;
8183 _configuration = configuration ;
8284 _brokenActivationMethodVersion = new ElasticVersion ( 8 , 7 , 0 ) ;
83-
8485 _intakeV2EventsAbsoluteUrl = BackendCommUtils . ApmServerEndpoints . BuildIntakeV2EventsAbsoluteUrl ( configuration . ServerUrl ) ;
8586
8687 System = system ;
8788
8889 _cloudMetadataProviderCollection = new CloudMetadataProviderCollection ( configuration . CloudProvider , _logger , environmentVariables ) ;
8990 _apmServerInfo = apmServerInfo ?? new ApmServerInfo ( ) ;
9091 _serverInfoCallback = serverInfoCallback ;
92+
9193 var process = ProcessInformation . Create ( ) ;
9294 _metadata = new Metadata { Service = service , System = System , Process = process } ;
9395 foreach ( var globalLabelKeyValue in configuration . GlobalLabels )
9496 _metadata . Labels . Add ( globalLabelKeyValue . Key , globalLabelKeyValue . Value ) ;
9597 _cachedActivationMethod = _metadata . Service ? . Agent . ActivationMethod ;
96- ResetActivationMethodIfKnownBrokenApmServer ( ) ;
98+
99+ if ( _isEnabled )
100+ ResetActivationMethodIfKnownBrokenApmServer ( ) ;
97101
98102 if ( configuration . MaxQueueEventCount < configuration . MaxBatchEventCount )
99103 {
@@ -123,7 +127,9 @@ public PayloadSenderV2(
123127 _eventQueue = new BatchBlock < object > ( configuration . MaxBatchEventCount ) ;
124128
125129 SetUpFilters ( TransactionFilters , SpanFilters , ErrorFilters , apmServerInfo , logger ) ;
126- StartWorkLoop ( ) ;
130+
131+ if ( _isEnabled )
132+ StartWorkLoop ( ) ;
127133 }
128134
129135 internal static void SetUpFilters (
@@ -175,6 +181,9 @@ protected override void Dispose(bool disposing)
175181
176182 internal async Task < bool > EnqueueEventInternal ( object eventObj , string dbgEventKind )
177183 {
184+ if ( ! _isEnabled )
185+ return true ;
186+
178187 // Enforce _maxQueueEventCount manually instead of using BatchBlock's BoundedCapacity
179188 // because of the issue of Post returning false when TriggerBatch is in progress. For more details see
180189 // https://stackoverflow.com/questions/35626955/unexpected-behaviour-tpl-dataflow-batchblock-rejects-items-while-triggerbatch
@@ -217,8 +226,12 @@ internal async Task<bool> EnqueueEventInternal(object eventObj, string dbgEventK
217226 return true ;
218227 }
219228
220- internal void EnqueueEvent ( object eventObj , string dbgEventKind ) =>
229+ internal void EnqueueEvent ( object eventObj , string dbgEventKind )
230+ {
231+ if ( ! _isEnabled )
232+ return ;
221233 Task . Run ( async ( ) => await EnqueueEventInternal ( eventObj , dbgEventKind ) ) ;
234+ }
222235
223236 /// <summary>
224237 /// Runs on the background thread dedicated to sending data to APM Server. It's ok to block this thread.
0 commit comments