Skip to content

AmbientRunAttributionScope

Namespace AgentPrism · Assembly AgentPrism.Abstractions.dll

The ambient pass-through for attributing a run that does not run inside an HTTP request to a user and a set of labels.

public static class AmbientRunAttributionScope

objectAmbientRunAttributionScope

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

This is the exact counterpart of AmbientTenantScope. An IRunAttributionContext implementation is a singleton and normally resolves the user from the current request; a queued job, a scheduled run or a direct.NET API call has no request to resolve from. The class carries the value through the same Threading.AsyncLocal pattern IHttpContextAccessor uses, and the built-in DefaultRunAttributionContext reads it.

An Threading.AsyncLocal write made inside an async method does not flow back to its caller. Open the scope in the body of the method that actually starts the run, and keep it alive across the whole run — on a streaming path that means the scope must still be open before every MoveNextAsync, not only before the first one.

Both members of a scope are held in a SINGLE slot, so AmbientRunAttributionScope.Begin replaces the user and the labels together and can never be observed half-applied.

Gets the ambient labels, or null when no scope is open.

public static IReadOnlyDictionary<string, string>? CurrentLabels { get; }

IReadOnlyDictionary<string, string>?

Gets the ambient user identity, or null when no scope is open.

public static string? CurrentUserId { get; }

string?

Gets whether a scope is open right now.

public static bool IsActive { get; }

bool

Begin(string?, IReadOnlyDictionary<string, string>?)

Section titled “ Begin(string?, IReadOnlyDictionary<string, string>?)”

Sets the ambient attribution for the duration of the scope.

public static IDisposable Begin(string? userId, IReadOnlyDictionary<string, string>? labels)

userId string?

The user identity, or null to attribute the run to no user. Whitespace is treated as null.

labels IReadOnlyDictionary<string, string>?

The labels, or null when there are none.

IDisposable

An object that restores the previous value when IDisposable.Dispose is called. Nested use is safe.

ArgumentException

userId or labels breaks a limit on RunLabels. The scope is REJECTED rather than trimmed: silently trimming a label set turns a measurement into a false claim.