Skip to content

RunTraceCollector

Namespace AgentPrism · Assembly AgentPrism.Core.dll

Listens to AgentPrism spans, buffers them per run, and writes them to ITraceStore when the run completes, based on the sampling decision.

public sealed class RunTraceCollector : IDisposable

objectRunTraceCollector

IDisposable

object.GetType(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Why scoped to a run’s lifetime? Spans are spread out over time and there is no general way to know a trace has ended; a timeout-based buffer produces both leaks and late writes. Here, the owner of the buffer is RunRecordingAgent: it opens the run, closes it, and drains the buffer in both cases (success or failure). A leak is therefore structurally impossible.

Why sample at the end? The spans of failed runs are the most valuable input for debugging, but whether a run will fail is not known at the start. The decision is made at the end; the cost of this is that spans are held in memory until then, bounded by AgentPrismObservabilityOptions.MaxSpansPerRun.

Does not take over the stream. Diagnostics.ActivityListener is a passive listener; the consumer’s own OpenTelemetry SDK keeps sending the same spans to its own exporter.

RunTraceCollector(ITraceStore, IOptions<AgentPrismOptions>, ILogger<RunTraceCollector>)

Section titled “ RunTraceCollector(ITraceStore, IOptions<AgentPrismOptions>, ILogger<RunTraceCollector>)”

Creates a new collector and starts listening.

public RunTraceCollector(ITraceStore store, IOptions<AgentPrismOptions> options, ILogger<RunTraceCollector> logger)

store ITraceStore

Span store.

options IOptions<AgentPrismOptions>

AgentPrism settings.

logger ILogger<RunTraceCollector>

Logger.

ArgumentNullException

One of the dependencies is null.

Whether the collector is persisting spans.

public bool IsCollecting { get; }

bool

Starts collecting spans for a run.

public bool BeginRun(string traceId, string rootSpanId)

traceId string

The W3C trace id of the run’s root span.

rootSpanId string

The span id of the activity this run opened. A trace id is inherited from the incoming request, so it alone does not identify one run.

bool

false when collection is disabled or this run is already being tracked.

CompleteRunAsync(string, string, Guid, string, RunStatus, CancellationToken)

Section titled “ CompleteRunAsync(string, string, Guid, string, RunStatus, CancellationToken)”

Ends collection and writes the spans if the sampling decision is positive. The buffer is released in every case.

public ValueTask<bool> CompleteRunAsync(string traceId, string rootSpanId, Guid runId, string tenantId, RunStatus status, CancellationToken cancellationToken = default)

traceId string

The W3C trace id of the root span.

rootSpanId string

The span id of the activity this run opened.

runId Guid

Run identifier.

tenantId string

Tenant identifier.

status RunStatus

The run’s final status.

cancellationToken CancellationToken

Cancellation token.

ValueTask<bool>

true when spans were written.

public void Dispose()