Skip to content

Commit 369b4a8

Browse files
authored
Ensure isEnabled = false won't start worker loop (#2436)
Inside PayloadSenderV2, we also now early exit when events get queued. Removed early exit from constructor, allow readonly fields to be initialized so that when we start adding nullable annotations not all of them have to be nullable
1 parent becb5df commit 369b4a8

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

src/Elastic.Apm/Report/PayloadSenderV2.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)