Class SpanCapture
- Namespace
- TracingAssertions
- Assembly
- TracingAssertions.dll
Disposable, per-test capture of Activity spans emitted by one or more ActivitySources. Starts a raw ActivityListener sampling at AllDataAndRecorded and collects every stopped activity from the listened source(s). Disposing detaches the listener.
public sealed class SpanCapture : IDisposable
- Inheritance
-
SpanCapture
- Implements
- Inherited Members
- Extension Methods
Remarks
No OpenTelemetry SDK or exporter pipeline is involved: capture is a BCL
ActivityListener over an ActivitySource matched by name, so the type
is AOT-safe and carries no NuGet runtime dependency (System.Diagnostics.DiagnosticSource
is in the shared framework).
Create one per test with a using statement for isolation; the listener stops collecting and detaches when disposed. The query helpers (FindByOperationName(string), FindByOperationNameAndTag(string, string, object), ChildrenOf(Activity)) read the captured set without advancing any clock.
Properties
Captured
The spans captured so far, in completion (activity-stopped) order. Each access returns a snapshot.
public IReadOnlyList<Activity> Captured { get; }
Property Value
Methods
ChildrenOf(Activity)
Returns the captured spans that are direct children of parent: a
span whose ParentSpanId equals parent's
SpanId and that shares its TraceId.
public IReadOnlyList<Activity> ChildrenOf(Activity parent)
Parameters
parentActivityThe parent span.
Returns
- IReadOnlyList<Activity>
The direct children, in completion order (possibly empty).
Exceptions
- ArgumentNullException
parentis null.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
FindByOperationName(string)
Returns the first captured span whose OperationName equals
operationName (ordinal), or null if none matches.
public Activity? FindByOperationName(string operationName)
Parameters
operationNamestringThe operation name to look for.
Returns
Exceptions
- ArgumentNullException
operationNameis null.
FindByOperationNameAndTag(string, string, object)
Returns the first captured span whose OperationName equals
operationName and that carries a tag tagKey whose value
matches tagValue, or null if none matches. Tag values are
compared by their invariant ToString() form, matching how spans carry
heterogeneously-typed tag values.
public Activity? FindByOperationNameAndTag(string operationName, string tagKey, object tagValue)
Parameters
operationNamestringThe operation name to look for.
tagKeystringThe tag key to inspect.
tagValueobjectThe expected tag value (compared by invariant
ToString).
Returns
Exceptions
- ArgumentNullException
operationName,tagKey, ortagValueis null.
ForSource(string)
Starts capturing spans from the single ActivitySource whose
Name equals sourceName (ordinal), sampling at
AllDataAndRecorded.
public static SpanCapture ForSource(string sourceName)
Parameters
sourceNamestringThe activity-source name to listen to.
Returns
- SpanCapture
A disposable capture; dispose it (ideally via using) to detach the listener at the end of the test.
Exceptions
- ArgumentNullException
sourceNameis null.
ForSource(string, ActivitySamplingResult)
Starts capturing spans from the single ActivitySource whose
Name equals sourceName (ordinal), at the given
sampling result.
public static SpanCapture ForSource(string sourceName, ActivitySamplingResult sampling)
Parameters
sourceNamestringThe activity-source name to listen to.
samplingActivitySamplingResultThe sampling result the listener returns for every activity.
Returns
- SpanCapture
A disposable capture; dispose it (ideally via using) to detach the listener at the end of the test.
Remarks
The default ForSource(string) forces AllDataAndRecorded, so code that branches on IsAllDataRequested always takes the recorded path under test. Pass a lower result (for example PropagationData) to exercise the not-fully-sampled branch. Note that an activity's effective sampling is the maximum requested by any active listener, so this lower result only takes effect when the capture is the sole listener; a co-existing recording listener (for example an OpenTelemetry SDK pipeline) still raises the activity to fully recorded.
Exceptions
- ArgumentNullException
sourceNameis null.
ForSources(ActivitySamplingResult, params string[])
Starts capturing spans from any ActivitySource whose
Name is one of sourceNames (ordinal), at the
given sampling result (see ForSource(string, ActivitySamplingResult)
for why this matters).
public static SpanCapture ForSources(ActivitySamplingResult sampling, params string[] sourceNames)
Parameters
samplingActivitySamplingResultThe sampling result the listener returns for every activity.
sourceNamesstring[]The activity-source names to listen to. An empty array captures nothing.
Returns
- SpanCapture
A disposable capture; dispose it (ideally via using) to detach the listener at the end of the test.
Exceptions
- ArgumentNullException
sourceNamesis null.
ForSources(params string[])
Starts capturing spans from any ActivitySource whose
Name is one of sourceNames (ordinal), sampling
at AllDataAndRecorded.
public static SpanCapture ForSources(params string[] sourceNames)
Parameters
sourceNamesstring[]The activity-source names to listen to. An empty array captures nothing.
Returns
- SpanCapture
A disposable capture; dispose it (ideally via using) to detach the listener at the end of the test.
Exceptions
- ArgumentNullException
sourceNamesis null.