Skip to content

Commit

Permalink
Ensure isEnabled = false won't start worker loop (#2436)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mpdreamz committed Sep 11, 2024
1 parent becb5df commit 369b4a8
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/Elastic.Apm/Report/PayloadSenderV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal class PayloadSenderV2 : BackendCommComponentBase, IPayloadSender, IPayl

private readonly ElasticVersion _brokenActivationMethodVersion;
private readonly string _cachedActivationMethod;
private readonly bool _isEnabled;

public PayloadSenderV2(
IApmLogger logger,
Expand All @@ -73,27 +74,30 @@ public PayloadSenderV2(
)
: base(isEnabled, logger, ThisClassName, service, configuration, httpMessageHandler)
{
if (!isEnabled)
return;

_logger = logger?.Scoped(ThisClassName + (dbgName == null ? "" : $" (dbgName: `{dbgName}')"));
_isEnabled = isEnabled;
if (!_isEnabled)
_logger?.Debug()?.Log($"{nameof(PayloadSenderV2)} is not enabled, work loop won't be started.");

_payloadItemSerializer = new PayloadItemSerializer();
_configuration = configuration;
_brokenActivationMethodVersion = new ElasticVersion(8, 7, 0);

_intakeV2EventsAbsoluteUrl = BackendCommUtils.ApmServerEndpoints.BuildIntakeV2EventsAbsoluteUrl(configuration.ServerUrl);

System = system;

_cloudMetadataProviderCollection = new CloudMetadataProviderCollection(configuration.CloudProvider, _logger, environmentVariables);
_apmServerInfo = apmServerInfo ?? new ApmServerInfo();
_serverInfoCallback = serverInfoCallback;

var process = ProcessInformation.Create();
_metadata = new Metadata { Service = service, System = System, Process = process };
foreach (var globalLabelKeyValue in configuration.GlobalLabels)
_metadata.Labels.Add(globalLabelKeyValue.Key, globalLabelKeyValue.Value);
_cachedActivationMethod = _metadata.Service?.Agent.ActivationMethod;
ResetActivationMethodIfKnownBrokenApmServer();

if (_isEnabled)
ResetActivationMethodIfKnownBrokenApmServer();

if (configuration.MaxQueueEventCount < configuration.MaxBatchEventCount)
{
Expand Down Expand Up @@ -123,7 +127,9 @@ public PayloadSenderV2(
_eventQueue = new BatchBlock<object>(configuration.MaxBatchEventCount);

SetUpFilters(TransactionFilters, SpanFilters, ErrorFilters, apmServerInfo, logger);
StartWorkLoop();

if (_isEnabled)
StartWorkLoop();
}

internal static void SetUpFilters(
Expand Down Expand Up @@ -175,6 +181,9 @@ protected override void Dispose(bool disposing)

internal async Task<bool> EnqueueEventInternal(object eventObj, string dbgEventKind)
{
if (!_isEnabled)
return true;

// Enforce _maxQueueEventCount manually instead of using BatchBlock's BoundedCapacity
// because of the issue of Post returning false when TriggerBatch is in progress. For more details see
// https://stackoverflow.com/questions/35626955/unexpected-behaviour-tpl-dataflow-batchblock-rejects-items-while-triggerbatch
Expand Down Expand Up @@ -217,8 +226,12 @@ internal async Task<bool> EnqueueEventInternal(object eventObj, string dbgEventK
return true;
}

internal void EnqueueEvent(object eventObj, string dbgEventKind) =>
internal void EnqueueEvent(object eventObj, string dbgEventKind)
{
if (!_isEnabled)
return;
Task.Run(async () => await EnqueueEventInternal(eventObj, dbgEventKind));
}

/// <summary>
/// Runs on the background thread dedicated to sending data to APM Server. It's ok to block this thread.
Expand Down

0 comments on commit 369b4a8

Please sign in to comment.