Skip to content

Commit 53f4ab4

Browse files
author
Kevin Gosse
authored
Tracer flare - Inspect the AGENT_CONFIG content to set the log level (#5802)
## Summary of changes When receiving the AGENT_CONFIG remote config, inspect the json to extract the log level, instead of relying only on the filename. ## Reason for change The filename is subject to change. ## Implementation details In theory we should remove entirely the old behavior (checking the file name) but the AGENT_CONFIG seems broken at the moment so I can't properly test it. To play it safe, I just merged the new and old logic. ## Test coverage Added a test case with a file name that doesn't contain the log level.
1 parent 41b10fb commit 53f4ab4

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

tracer/src/Datadog.Trace/Logging/TracerFlare/TracerFlareManager.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
using System.Collections.Generic;
1010
using System.Diagnostics.CodeAnalysis;
1111
using System.IO;
12-
using System.Linq;
1312
using System.Threading;
1413
using System.Threading.Tasks;
1514
using Datadog.Trace.Agent.DiscoveryService;
@@ -34,6 +33,8 @@ internal class TracerFlareManager : ITracerFlareManager
3433
private readonly IRcmSubscriptionManager _subscriptionManager;
3534
private readonly ITelemetryController _telemetryController;
3635
private readonly TracerFlareApi _flareApi;
36+
37+
private string? _debugEnabledConfigPath;
3738
private ISubscription? _subscription;
3839
private Timer? _resetTimer = null;
3940

@@ -86,6 +87,7 @@ private void ResetDebugging()
8687
// Restore the log level to its old value
8788
if (!_wasDebugLogEnabled)
8889
{
90+
_debugEnabledConfigPath = null;
8991
GlobalSettings.SetDebugEnabledInternal(false);
9092
}
9193
}
@@ -126,9 +128,10 @@ private async Task<ApplyDetails[]> HandleTracerFlareInitiated(List<RemoteConfigu
126128
var debugRequested = false;
127129
foreach (var remoteConfig in config)
128130
{
129-
if (IsEnableDebugConfig(remoteConfig.Path))
131+
if (IsEnableDebugConfig(remoteConfig))
130132
{
131133
debugRequested = true;
134+
_debugEnabledConfigPath = remoteConfig.Path.Path;
132135
break;
133136
}
134137
}
@@ -180,12 +183,16 @@ private void HandleTracerFlareResolved(List<RemoteConfigurationPath> config)
180183
try
181184
{
182185
var enableDebugDeleted = false;
183-
foreach (var removedConfig in config)
186+
187+
if (_debugEnabledConfigPath != null)
184188
{
185-
if (IsEnableDebugConfig(removedConfig))
189+
foreach (var removedConfig in config)
186190
{
187-
enableDebugDeleted = true;
188-
break;
191+
if (_debugEnabledConfigPath == removedConfig.Path)
192+
{
193+
enableDebugDeleted = true;
194+
break;
195+
}
189196
}
190197
}
191198

@@ -341,10 +348,32 @@ static bool TryGetArg(JObject argsObject, string name, string configPath, [NotNu
341348
}
342349
}
343350

344-
private static bool IsEnableDebugConfig(RemoteConfigurationPath remoteConfigPath)
351+
private static bool IsEnableDebugConfig(RemoteConfiguration remoteConfig)
345352
{
346-
return remoteConfigPath.Id.Equals("flare-log-level.debug", StringComparison.Ordinal)
347-
|| remoteConfigPath.Id.Equals("flare-log-level.trace", StringComparison.Ordinal);
353+
try
354+
{
355+
var remoteConfigPath = remoteConfig.Path;
356+
357+
if (remoteConfigPath.Id.Equals("flare-log-level.debug", StringComparison.Ordinal)
358+
|| remoteConfigPath.Id.Equals("flare-log-level.trace", StringComparison.Ordinal))
359+
{
360+
return true;
361+
}
362+
363+
var json = JObject.Parse(EncodingHelpers.Utf8NoBom.GetString(remoteConfig.Contents));
364+
365+
var logLevel = json["config"]?["log_level"]?.Value<string>();
366+
367+
return logLevel is not null
368+
&& (logLevel.Equals("debug", StringComparison.OrdinalIgnoreCase)
369+
|| logLevel.Equals("trace", StringComparison.OrdinalIgnoreCase));
370+
}
371+
catch (Exception ex)
372+
{
373+
Log.Error(ex, "Invalid configuration provided for tracer flare");
374+
}
375+
376+
return false;
348377
}
349378

350379
private static ApplyDetails[] AcknowledgeAll(List<RemoteConfiguration> config)

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/TracerFlareTests.cs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
using System;
77
using System.IO.Compression;
8-
using System.Linq;
98
using System.Threading.Tasks;
109
using Datadog.Trace.Configuration;
1110
using Datadog.Trace.Logging.TracerFlare;
@@ -34,9 +33,11 @@ public TracerFlareTests(ITestOutputHelper output)
3433
SetEnvironmentVariable(ConfigurationKeys.Rcm.PollInterval, "5");
3534
}
3635

37-
[SkippableFact]
36+
[SkippableTheory]
37+
[InlineData(true)]
38+
[InlineData(false)]
3839
[Trait("RunOnWindows", "True")]
39-
public async Task SendTracerFlare()
40+
public async Task SendTracerFlare(bool logLevelInFileName)
4041
{
4142
using var agent = EnvironmentHelper.GetMockAgent(useTelemetry: true);
4243
var processName = EnvironmentHelper.IsCoreClr() ? "dotnet" : "Samples.Console";
@@ -47,7 +48,7 @@ public async Task SendTracerFlare()
4748
{
4849
_ = await logEntryWatcher.WaitForLogEntry(DiagnosticLog);
4950

50-
await InitializeFlare(agent, logEntryWatcher);
51+
await InitializeFlare(agent, logEntryWatcher, logLevelInFileName);
5152
await TriggerFlareCollection(agent, logEntryWatcher);
5253
}
5354
finally
@@ -76,11 +77,23 @@ public async Task SendTracerFlare()
7677
.And.Contain(x => x.Name.StartsWith("dotnet-tracer-telemetry-"));
7778
}
7879

79-
private async Task InitializeFlare(MockTracerAgent agent, LogEntryWatcher logEntryWatcher)
80+
private async Task InitializeFlare(MockTracerAgent agent, LogEntryWatcher logEntryWatcher, bool logLevelInFileName)
8081
{
81-
var fileId = "flare-log-level.debug";
82+
string fileId;
83+
object payload;
8284

83-
var request = await agent.SetupRcmAndWait(Output, new[] { ((object)new { }, RcmProducts.TracerFlareInitiated, fileId) });
85+
if (logLevelInFileName)
86+
{
87+
fileId = "flare-log-level.debug";
88+
payload = new { };
89+
}
90+
else
91+
{
92+
fileId = Guid.NewGuid().ToString();
93+
payload = new { config = new { log_level = "debug" } };
94+
}
95+
96+
var request = await agent.SetupRcmAndWait(Output, new[] { (payload, RcmProducts.TracerFlareInitiated, fileId) });
8497

8598
request.Should().NotBeNull();
8699
request.Client.State.ConfigStates.Should().ContainSingle(f => f.Id == fileId)

0 commit comments

Comments
 (0)