Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure isEnabled = false won't start worker loop #2436

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading