Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f311ff2
feat: cross-process test log correlation via OTLP receiver (#4818)
thomhurst Apr 13, 2026
a32e2ad
fix: address code review findings on OTLP correlation PR
thomhurst Apr 13, 2026
13ee150
refactor: simplify after code review
thomhurst Apr 13, 2026
e3109a9
fix: gate CreateHttpClient override on EnableTelemetryCollection
thomhurst Apr 13, 2026
c3f804a
fix: address third-round review findings
thomhurst Apr 13, 2026
1f9ee36
docs: add telemetry correlation docs, enable by default
thomhurst Apr 13, 2026
8959f79
docs: note field ordering assumption in ParseResourceLogs
thomhurst Apr 13, 2026
b373da4
test: add thorough tests for OTLP log-to-test correlation
thomhurst Apr 13, 2026
ab164b9
fix: update public API snapshots for new InternalsVisibleTo entry
thomhurst Apr 14, 2026
4a9231d
feat: add OTLP integration tests and auto-inject telemetry env vars
thomhurst Apr 14, 2026
3aed4c2
refactor: simplify integration tests per code review
thomhurst Apr 14, 2026
dc20e0c
docs: address review feedback on shared handler and TraceId scope
thomhurst Apr 14, 2026
28d44bb
fix: generate unique TraceId per HTTP request for reliable correlation
thomhurst Apr 14, 2026
c40e230
feat: give each test its own TraceId via root activity with class link
thomhurst Apr 14, 2026
4359189
test: add engine activity tests, fix test body parent chain
thomhurst Apr 14, 2026
0587668
fix: restore parent-child activity hierarchy, move correlation to han…
thomhurst Apr 14, 2026
3e6575c
refactor: replace raw tag key strings with TUnitActivitySource constants
thomhurst Apr 14, 2026
3221c82
fix: replace fixed Task.Delay with polling in OtlpReceiverTests
thomhurst Apr 14, 2026
3ddbf74
fix: address review round 4 findings
thomhurst Apr 14, 2026
477a319
fix: remove mutually exclusive RunOnLinuxOnly/RunOnWindowsOnly attrs
thomhurst Apr 14, 2026
870a6c5
fix: add missing HangDump package to Aspire and ASP.NET test projects
thomhurst Apr 14, 2026
e911de2
fix: skip Aspire, ASP.NET, and RPC test modules on macOS
thomhurst Apr 14, 2026
3dd2b20
fix: enable Docker setup on macOS CI runners
thomhurst Apr 14, 2026
0874d0e
fix: skip Docker-dependent test modules on macOS
thomhurst Apr 14, 2026
87cfffa
fix: skip RPC tests globally until fixed (#5540)
thomhurst Apr 14, 2026
98fc40f
fix: restrict Docker-dependent CI modules to Linux, add missing ApiSe…
thomhurst Apr 14, 2026
08b62fb
fix: fix PublicAPI snapshot ordering and flaky severity test
thomhurst Apr 14, 2026
8c5e115
fix: move mock tests AOT publish into pipeline module
thomhurst Apr 14, 2026
068cdb0
Refactor code structure for improved readability and maintainability
thomhurst Apr 14, 2026
8034a5b
fix: share IntegrationTestFixture in WaitForHealthy tests
thomhurst Apr 14, 2026
f55d181
fix: restart Docker after setup to initialize iptables chains
thomhurst Apr 14, 2026
97178be
fix: remove docker/setup-docker-action
thomhurst Apr 14, 2026
4c3b872
fix: correct ASP.NET execution order assertions
thomhurst Apr 14, 2026
9145da5
fix: use CreateTablesAsync to avoid EnsureCreatedAsync race in parall…
thomhurst Apr 14, 2026
cad9f86
fix: use absolute path for AOT mock test publish output
thomhurst Apr 14, 2026
db8c9f9
merge: resolve conflict with main (NuGet.Protocol 7.3.1)
thomhurst Apr 14, 2026
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
Prev Previous commit
Next Next commit
refactor: simplify integration tests per code review
- Replace hardcoded "tunit.test.id" with TUnitActivitySource.TagTestId
- Remove unused OpenTelemetry.Resources import
- Add null guard on StartActivity instead of null-forgiving operator
- Extract "api-service" to const ServiceName (was repeated 8 times)
- Extract magic number 10 to const ConcurrentInstanceCount
- Fix TraceRegistry test silently passing when no activity exists
- Dispose HttpResponseMessage from all GetAsync calls
- Poll once instead of twice in DifferentLogLevels test
- Use Stopwatch instead of DateTime.UtcNow for monotonic timing
  • Loading branch information
thomhurst committed Apr 14, 2026
commit 3aed4c29ed7a5ae257088832b0f93f113f4e3a15
1 change: 0 additions & 1 deletion TUnit.Aspire.Tests.ApiService/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using OpenTelemetry.Logs;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);
Expand Down
82 changes: 45 additions & 37 deletions TUnit.Aspire.Tests/OtlpCorrelationIntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ namespace TUnit.Aspire.Tests;
[Category("Integration")]
public class OtlpCorrelationIntegrationTests(IntegrationTestFixture fixture)
{
private const string ServiceName = "api-service";
private const int ConcurrentInstanceCount = 10;

// Each test needs its own TraceId for correlation. The engine creates test
// activities as children of the class activity (shared TraceId), so we create
// a dedicated root activity per test to get a unique TraceId for HTTP requests.
Expand All @@ -32,8 +35,8 @@ public async Task Logs_FromApiService_AppearInTestOutput()
var marker = $"integration-marker-{Guid.NewGuid():N}";

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
var response = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
var client = fixture.CreateHttpClient(ServiceName);
using var response = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");

await Assert.That((int)response.StatusCode).IsEqualTo(200);

Expand All @@ -51,11 +54,11 @@ public async Task Logs_IncludeServiceNamePrefix()
var marker = $"svc-prefix-{Guid.NewGuid():N}";

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
var client = fixture.CreateHttpClient(ServiceName);
using var _ = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");

var output = await PollForOutput(marker);
await Assert.That(output).Contains("[api-service]");
await Assert.That(output).Contains($"[{ServiceName}]");
}

/// <summary>
Expand All @@ -68,8 +71,8 @@ public async Task Logs_IncludeSeverityLevel()
var marker = $"severity-{Guid.NewGuid():N}";

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
var client = fixture.CreateHttpClient(ServiceName);
using var _ = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");

var output = await PollForOutput(marker);

Expand All @@ -89,15 +92,14 @@ public async Task DifferentLogLevels_AreCorrectlyFormatted()
var errorMarker = $"error-level-{Guid.NewGuid():N}";

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
await client.GetAsync($"/log-level?message={Uri.EscapeDataString(warnMarker)}&level=Warning");
await client.GetAsync($"/log-level?message={Uri.EscapeDataString(errorMarker)}&level=Error");

var warnOutput = await PollForOutput(warnMarker);
await Assert.That(warnOutput).Contains(warnMarker);

var errorOutput = await PollForOutput(errorMarker);
await Assert.That(errorOutput).Contains(errorMarker);
var client = fixture.CreateHttpClient(ServiceName);
using var _ = await client.GetAsync($"/log-level?message={Uri.EscapeDataString(warnMarker)}&level=Warning");
using var __ = await client.GetAsync($"/log-level?message={Uri.EscapeDataString(errorMarker)}&level=Error");

// Poll once for the later marker — both will have arrived by then
var output = await PollForOutput(errorMarker);
await Assert.That(output).Contains(warnMarker);
await Assert.That(output).Contains(errorMarker);
}

/// <summary>
Expand All @@ -124,11 +126,11 @@ public async Task ConcurrentTests_EachSeeOnlyOwnLogs(int instanceId)
.ToList();

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
var client = fixture.CreateHttpClient(ServiceName);

foreach (var marker in markers)
{
await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
using var _ = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
}

var output = await PollForOutput(markers.Last());
Expand All @@ -140,7 +142,7 @@ public async Task ConcurrentTests_EachSeeOnlyOwnLogs(int instanceId)
}

// No markers from any other instance should appear in this test's output
for (var other = 0; other < 10; other++)
for (var other = 0; other < ConcurrentInstanceCount; other++)
{
if (other != instanceId)
{
Expand All @@ -162,12 +164,17 @@ public async Task BurstConcurrentRequests_AllCorrelateCorrectly()
.ToList();

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
var client = fixture.CreateHttpClient(ServiceName);

// Fire all requests concurrently
await Task.WhenAll(markers.Select(marker =>
// Fire all requests concurrently and dispose responses
var responses = await Task.WhenAll(markers.Select(marker =>
client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}")));

foreach (var response in responses)
{
response.Dispose();
}

// All logs from this burst must appear in this test's output
var output = await PollForOutput(markers.Last());

Expand All @@ -189,11 +196,11 @@ public async Task MultipleRequests_AllCorrelateToSameTest()
.ToList();

using var activity = StartCorrelatedActivity();
var client = fixture.CreateHttpClient("api-service");
var client = fixture.CreateHttpClient(ServiceName);

foreach (var marker in markers)
{
await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
using var _ = await client.GetAsync($"/log?message={Uri.EscapeDataString(marker)}");
}

// Wait for all logs to arrive
Expand All @@ -212,14 +219,12 @@ public async Task MultipleRequests_AllCorrelateToSameTest()
[Test]
public async Task TraceRegistry_ContainsCurrentTestTraceId()
{
// The engine registers the test's TraceId with TraceRegistry automatically.
var activity = Activity.Current;
if (activity is not null)
{
var traceId = activity.TraceId.ToString();
var isRegistered = TraceRegistry.IsRegistered(traceId);
await Assert.That(isRegistered).IsTrue();
}
var activity = Activity.Current
?? throw new InvalidOperationException("No Activity.Current — TUnit engine should create one per test.");

var traceId = activity.TraceId.ToString();
var isRegistered = TraceRegistry.IsRegistered(traceId);
await Assert.That(isRegistered).IsTrue();
}

/// <summary>
Expand All @@ -242,10 +247,12 @@ private Activity StartCorrelatedActivity()

var activity = TestActivitySource.StartActivity(
"integration-test-request",
ActivityKind.Client)!;
ActivityKind.Client)
?? throw new InvalidOperationException(
"StartActivity returned null — no ActivityListener is registered. " +
"Ensure the TUnit engine is creating test activities.");

// Copy the test baggage so the SUT receives tunit.test.id
activity.SetBaggage("tunit.test.id", testContext.Id);
activity.SetBaggage(TUnitActivitySource.TagTestId, testContext.Id);

// Register this new TraceId so the OTLP receiver can correlate logs back to this test
TraceRegistry.Register(
Expand All @@ -264,9 +271,10 @@ private Activity StartCorrelatedActivity()
private static async Task<string> PollForOutput(string expectedMarker, int timeoutSeconds = 30)
{
var testContext = TestContext.Current!;
var deadline = DateTime.UtcNow.AddSeconds(timeoutSeconds);
var sw = Stopwatch.StartNew();
var timeout = TimeSpan.FromSeconds(timeoutSeconds);

while (DateTime.UtcNow < deadline)
while (sw.Elapsed < timeout)
{
var output = testContext.GetStandardOutput();
if (output.Contains(expectedMarker))
Expand Down
Loading