Interview question
Verify trace propagation across an HTTP dependency
Captures test activities to prove one incoming request and its outgoing HTTP dependency share trace context.
TL;DR
Captures test activities to prove one incoming request and its outgoing HTTP dependency share trace context.
Activity tracing, parent-child spans, W3C context, dependency boundaries, test exporters, and low-cardinality attributes.
Practice the problem like a real interview: restate, reason, implement, and test.
Configure an in-memory OpenTelemetry exporter in the test host. Call an endpoint that uses HttpClient against a stub server. Assert the server span and dependency span share a trace id and the dependency is a child. Do not assert generated span ids.
A slow dependency span can be found underneath the exact API request, carrying stable route and service attributes without user payloads.
I collect completed activities in memory and find spans by stable operation names and kinds. Trace-id equality plus parent-span equality proves propagation more directly than checking that two spans merely exist.
[Fact]
public async Task Pricing_dependency_is_child_of_api_request_span()
{
var exported = new InMemoryActivityExporter();
await using var factory = new ApiTestFactory(services =>
services.AddOpenTelemetry()
.WithTracing(builder => builder
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddProcessor(new SimpleActivityExportProcessor(exported))));
var client = factory.CreateAuthenticatedClient("user-1", "quotes.read");
var response = await client.GetAsync("/api/quotes/p-1");
response.EnsureSuccessStatusCode();
await exported.WaitForExportsAsync(expectedMinimum: 2);
var server = exported.Single(activity => activity.Kind == ActivityKind.Server);
var dependency = exported.Single(activity =>
activity.Kind == ActivityKind.Client && activity.DisplayName.Contains("pricing"));
Assert.Equal(server.TraceId, dependency.TraceId);
Assert.Equal(server.SpanId, dependency.ParentSpanId);
}
This validates instrumentation wiring without a real telemetry service. Exact span names vary by instrumentation version, so the production implementation should define stable custom operation attributes where ownership requires them.
Move to the linked follow-up, next path step, prerequisite, or deeper variant.
Practice the next layer of the same subject.