Skip to content

Commit

Permalink
Reenable tracer flare and handle debug request (#5040)
Browse files Browse the repository at this point in the history
* Revert "Temporarily disable the tracer flare functionality"

This reverts commit 728e72d.

* Check for `flare-log-level.debug` in the remote config
  • Loading branch information
andrewlock authored Jan 15, 2024
1 parent 3b94302 commit 7db6320
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 39 deletions.
91 changes: 55 additions & 36 deletions tracer/src/Datadog.Trace/Logging/TracerFlare/TracerFlareManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ internal class TracerFlareManager : ITracerFlareManager
private readonly IDiscoveryService _discoveryService;
private readonly IRcmSubscriptionManager _subscriptionManager;
private readonly TracerFlareApi _flareApi;
private readonly bool _enableFlare;
private ISubscription? _subscription;
private Timer? _resetTimer = null;

Expand All @@ -40,24 +39,17 @@ internal class TracerFlareManager : ITracerFlareManager
public TracerFlareManager(
IDiscoveryService discoveryService,
IRcmSubscriptionManager subscriptionManager,
TracerFlareApi flareApi,
bool enableFlare = true)
TracerFlareApi flareApi)
{
_subscriptionManager = subscriptionManager;
_flareApi = flareApi;
_discoveryService = discoveryService;
_enableFlare = enableFlare;
}

public bool? CanSendTracerFlare { get; private set; } = null;

public void Start()
{
if (!_enableFlare)
{
return;
}

if (Interlocked.Exchange(ref _subscription, new Subscription(RcmProductReceived, RcmProducts.TracerFlareInitiated, RcmProducts.TracerFlareRequested)) == null)
{
_discoveryService.SubscribeToChanges(HandleConfigUpdate);
Expand All @@ -68,11 +60,6 @@ public void Start()

public void Dispose()
{
if (!_enableFlare)
{
return;
}

if (_resetTimer is not null)
{
// If we have a timer, we should reset debugging now
Expand Down Expand Up @@ -131,22 +118,35 @@ private ApplyDetails[] HandleTracerFlareInitiated(List<RemoteConfiguration> conf
{
try
{
// This product means "prepare for sending a tracer flare."
// We may consider doing more than just enabling debug mode in the future
_wasDebugLogEnabled = GlobalSettings.Instance.DebugEnabledInternal;
GlobalSettings.SetDebugEnabledInternal(true);

// The timer is a fallback, in case we never receive a "send flare" product
var timer = new Timer(
_ => ResetDebugging(),
state: null,
dueTime: TimeSpan.FromMinutes(RevertGlobalDebugMinutes),
period: Timeout.InfiniteTimeSpan);

var previous = Interlocked.Exchange(ref _resetTimer, timer);
previous?.Dispose();
var debugRequested = false;
foreach (var remoteConfig in config)
{
if (IsEnableDebugConfig(remoteConfig.Path))
{
debugRequested = true;
break;
}
}

Log.Debug(TracerFlareInitializationLog);
if (debugRequested)
{
// This product means "prepare for sending a tracer flare."
// We may consider doing more than just enabling debug mode in the future
_wasDebugLogEnabled = GlobalSettings.Instance.DebugEnabledInternal;
GlobalSettings.SetDebugEnabledInternal(true);

// The timer is a fallback, in case we never receive a "send flare" product
var timer = new Timer(
_ => ResetDebugging(),
state: null,
dueTime: TimeSpan.FromMinutes(RevertGlobalDebugMinutes),
period: Timeout.InfiniteTimeSpan);

var previous = Interlocked.Exchange(ref _resetTimer, timer);
previous?.Dispose();

Log.Debug(TracerFlareInitializationLog);
}

return AcknowledgeAll(config);
}
Expand All @@ -162,12 +162,25 @@ private ApplyDetails[] HandleTracerFlareResolved(List<RemoteConfigurationPath> c
{
try
{
// This product means "tracer flare is over, revert log levels"
ResetDebugging();
var timer = Interlocked.Exchange(ref _resetTimer, null);
timer?.Dispose();
var enableDebugDeleted = false;
foreach (var removedConfig in config)
{
if (IsEnableDebugConfig(removedConfig))
{
enableDebugDeleted = true;
break;
}
}

Log.Information(TracerFlareCompleteLog);
if (enableDebugDeleted)
{
// This product means "tracer flare is over, revert log levels"
ResetDebugging();
var timer = Interlocked.Exchange(ref _resetTimer, null);
timer?.Dispose();

Log.Information(TracerFlareCompleteLog);
}

// TODO: I don't know if we need to "accept" removed config?
var result = new ApplyDetails[config.Count];
Expand Down Expand Up @@ -329,7 +342,13 @@ static bool TryGetArg(JObject argsObject, string name, string configPath, [NotNu
}
}

private ApplyDetails[] AcknowledgeAll(List<RemoteConfiguration> config)
private static bool IsEnableDebugConfig(RemoteConfigurationPath remoteConfigPath)
{
return remoteConfigPath.Id.Equals("flare-log-level.debug", StringComparison.Ordinal)
|| remoteConfigPath.Id.Equals("flare-log-level.trace", StringComparison.Ordinal);
}

private static ApplyDetails[] AcknowledgeAll(List<RemoteConfiguration> config)
{
var result = new ApplyDetails[config.Count];
for (var i = 0; i < config.Count; i++)
Expand All @@ -340,7 +359,7 @@ private ApplyDetails[] AcknowledgeAll(List<RemoteConfiguration> config)
return result;
}

private ApplyDetails[] ErrorAll(List<RemoteConfiguration> config, Exception ex)
private static ApplyDetails[] ErrorAll(List<RemoteConfiguration> config, Exception ex)
{
var result = new ApplyDetails[config.Count];
for (var i = 0; i < config.Count; i++)
Expand Down
2 changes: 1 addition & 1 deletion tracer/src/Datadog.Trace/TracerManagerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ internal TracerManager CreateTracerManager(
}

dynamicConfigurationManager ??= new DynamicConfigurationManager(RcmSubscriptionManager.Instance);
tracerFlareManager ??= new TracerFlareManager(discoveryService, RcmSubscriptionManager.Instance, TracerFlareApi.Create(settings.ExporterInternal), enableFlare: false);
tracerFlareManager ??= new TracerFlareManager(discoveryService, RcmSubscriptionManager.Instance, TracerFlareApi.Create(settings.ExporterInternal));

return CreateTracerManagerFrom(
settings,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public TracerFlareTests(ITestOutputHelper output)
SetEnvironmentVariable(ConfigurationKeys.Rcm.PollInterval, "5");
}

[SkippableFact(Skip = "Temporarily disabled for release")]
[SkippableFact]
[Trait("RunOnWindows", "True")]
public async Task SendTracerFlare()
{
Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task SendTracerFlare()

private async Task InitializeFlare(MockTracerAgent agent, LogEntryWatcher logEntryWatcher)
{
var fileId = Guid.NewGuid().ToString();
var fileId = "flare-log-level.debug";

var request = await agent.SetupRcmAndWait(Output, new[] { ((object)new { }, RcmProducts.TracerFlareInitiated, fileId) });

Expand Down

0 comments on commit 7db6320

Please sign in to comment.