Skip to content

Commit

Permalink
Fix redacted error test
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewlock committed Nov 13, 2024
1 parent 15d822a commit 40974de
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ public async Task Telemetry_SendsRedactedErrorLogs()

EnableAgentlessTelemetry(telemetry.Port, enableDependencies: true);
SetEnvironmentVariable(ConfigurationKeys.Telemetry.TelemetryLogsEnabled, "1");
// Create invalid sampling rules (invalid JSON) to trigger parsing error
SetEnvironmentVariable(ConfigurationKeys.CustomSamplingRules, "[{\"sample_rate\":0.1");
SetEnvironmentVariable("SEND_ERROR_LOG", "1");

int httpPort = TcpPortProvider.GetOpenPort();
Output.WriteLine($"Assigning port {httpPort} for the httpPort.");
Expand Down Expand Up @@ -339,7 +338,7 @@ public async Task Telemetry_SendsRedactedErrorLogs()
allLogs.Should()
.ContainSingle()
.Which.Message.Should()
.Be("Unable to parse the trace sampling rules.");
.Be("Sending an error log using hacky reflection");
}

[SkippableFact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -22,6 +24,12 @@ public static async Task Main(string[] args)
string port = args.FirstOrDefault(arg => arg.StartsWith("Port="))?.Split('=')[1] ?? "9000";
Console.WriteLine($"Port {port}");

// Need to send an error log, but no easy way to do that on purpose.
if (Environment.GetEnvironmentVariable("SEND_ERROR_LOG") == "1")
{
SendErrorLog();
}

using (var server = WebServer.Start(port, out var url))
{
server.RequestHandler = HandleHttpRequests;
Expand Down Expand Up @@ -84,5 +92,24 @@ private static void HandleHttpRequests(HttpListenerContext context)
activity?.Dispose();
}
}

private static void SendErrorLog([CallerLineNumber] int sourceLine = 0, [CallerFilePath] string sourceFile = "")
{
if (!SampleHelpers.IsProfilerAttached())
{
throw new Exception("Can't send error log unless profiler is attached");
}

// grab the log field from TracerSettings, as it's an easy way to get an instance
var settingsType = Type.GetType("Datadog.Trace.Configuration.TracerSettings, Datadog.Trace")!;
var settings = Activator.CreateInstance(settingsType);
var logField = settingsType.GetField("Log", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)!;
var logger = logField.GetValue(settings);

var loggerType = Type.GetType("Datadog.Trace.Logging.DatadogSerilogLogger, Datadog.Trace")!;
var errorMethod = loggerType.GetMethod("Error", [typeof(string), typeof(int), typeof(string)])!;

errorMethod.Invoke(logger, ["Sending an error log using hacky reflection", sourceLine, sourceFile]);
}
}
}

0 comments on commit 40974de

Please sign in to comment.