diff --git a/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/InstrumentationTests.cs b/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/InstrumentationTests.cs index cb4c7d5a3cdd..51346d7b6e14 100644 --- a/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/InstrumentationTests.cs +++ b/tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/InstrumentationTests.cs @@ -10,6 +10,7 @@ using System.Runtime.CompilerServices; using System.Text; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; using Datadog.Trace.Configuration; using Datadog.Trace.TestHelpers; @@ -432,7 +433,7 @@ public async Task OnEolFrameworkInSsi_WhenForwarderPathExists_CallsForwarderWith "name": "library_entrypoint.abort.runtime" }] """; - AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "abort", ".NET Core 3.0 or lower", "incompatible_runtime"); + await AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "abort", ".NET Core 3.0 or lower", "incompatible_runtime"); } [SkippableFact] @@ -463,7 +464,7 @@ public async Task OnEolFrameworkInSsi_WhenOverriden_CallsForwarderWithExpectedTe "tags": ["injection_forced:true"] }] """; - AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Force instrumentation enabled, incompatible runtime, .NET Core 3.0 or lower", "success_forced"); + await AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Force instrumentation enabled, incompatible runtime, .NET Core 3.0 or lower", "success_forced"); } #endif @@ -502,7 +503,7 @@ public async Task OnPreviewFrameworkInSsi_CallsForwarderWithExpectedTelemetry() "tags": ["injection_forced:true"] }] """; - AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Force instrumentation enabled, incompatible runtime, .NET 10 or higher", "success_forced"); + await AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Force instrumentation enabled, incompatible runtime, .NET 10 or higher", "success_forced"); } [SkippableFact] @@ -535,7 +536,7 @@ public async Task OnPreviewFrameworkInSsi_WhenForwarderPathExists_CallsForwarder "name": "library_entrypoint.abort.runtime" }] """; - AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "abort", ".NET 10 or higher", "incompatible_runtime"); + await AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "abort", ".NET 10 or higher", "incompatible_runtime"); } [SkippableFact] @@ -592,7 +593,7 @@ public async Task OnSupportedFrameworkInSsi_CallsForwarderWithExpectedTelemetry( "tags": ["injection_forced:false"] }] """; - AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Successfully configured automatic instrumentation", "success"); + await AssertHasExpectedTelemetry(logFileName, processResult, pointsJson, "success", "Successfully configured automatic instrumentation", "success"); } [SkippableFact] @@ -795,10 +796,21 @@ private void AssertNativeLoaderLogContainsString(string logDir, string requiredL nativeLoaderLogFiles.Should().Contain(log => log.Contains(requiredLog)); } - private void AssertHasExpectedTelemetry(string echoLogFileName, ProcessResult processResult, string pointsJson, string injectResult, string injectResultReason, string injectResultClass) + private async Task AssertHasExpectedTelemetry(string echoLogFileName, ProcessResult processResult, string pointsJson, string injectResult, string injectResultReason, string injectResultClass) { using var s = new AssertionScope(); - File.Exists(echoLogFileName).Should().BeTrue(); + + // Wait for the telemetry echo file to be written (with timeout) + // The telemetry forwarder may write the file asynchronously after process exit + var fileWaitTimeout = TimeSpan.FromSeconds(10); + var fileWaitStart = DateTime.UtcNow; + while (!File.Exists(echoLogFileName) && (DateTime.UtcNow - fileWaitStart) < fileWaitTimeout) + { + await Task.Delay(100); + } + + File.Exists(echoLogFileName).Should().BeTrue($"Telemetry echo file should exist at {echoLogFileName} within {fileWaitTimeout.TotalSeconds} seconds"); + var echoLogContent = File.ReadAllText(echoLogFileName); s.AddReportable(echoLogFileName, echoLogContent);