Interface SpanScope
- All Superinterfaces:
AutoCloseable
Span.start(). Closing the scope ends the underlying Span.
A SpanScope is the neutral, provider-agnostic handle to a span that is active on a context branch.
It is carried on a ProcessingContext under RESOURCE_KEY and read back with
fromContext(ProcessingContext) -- mirroring the RESOURCE_KEY / addToContext /
fromContext convention of Message and TrackingToken. Two
distinct carriers exist:
- Lifecycle-covering spans (a batch span, a per-command/query handler span) are recorded on the
root context by
Span.coverLifecycle(ProcessingContext), last-writer-wins, exactly likeMessage.RESOURCE_KEY's "current message". - Branch-scoped spans (a per-event handler span, a per-invocation method span) are carried on an
immutable branch via
addToContext(ProcessingContext, SpanScope): the branch, not the root, is what flows into the operation's own handling, so its children read back exactly this scope while it is open. Once it closes, the branch falls back to the parent scope inherited when it was created. The context tree is therefore the parenting stack, including for a branch retained by deferred lifecycle work.
- Since:
- 5.3.0
- Author:
- Mateusz Nowak
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Context.ResourceKey<SpanScope> Resource key under which the activeSpanScopeis carried on aProcessingContext-- either on the root context (written bySpan.coverLifecycle(ProcessingContext)) or on an immutable branch (viaaddToContext(ProcessingContext, SpanScope)) -- and read withfromContext(ProcessingContext). -
Method Summary
Modifier and TypeMethodDescriptionstatic ProcessingContextaddToContext(ProcessingContext context, SpanScope scope) voidclose()Closes this scope, ending the underlyingSpan.static @Nullable SpanScopefromContext(ProcessingContext context) booleanisClosed()Returns whether this scope has closed for context resolution.span()Returns theSpangoverned by this scope.default voidExecutes the given voidoperationwithin this scope.<T> TExecutes the givenoperationwithin this scope and returns its result.
-
Field Details
-
RESOURCE_KEY
Resource key under which the activeSpanScopeis carried on aProcessingContext-- either on the root context (written bySpan.coverLifecycle(ProcessingContext)) or on an immutable branch (viaaddToContext(ProcessingContext, SpanScope)) -- and read withfromContext(ProcessingContext).Question it answers: "Which scope is the parent for a span created with this exact context instance, and what is the active span for provider-agnostic access (for example
Span.addAttribute(String, String))?"Why one key is enough. Precise parent chaining no longer needs a provider-private stack: a branch-scoped span branches the context it hands to its own operation, so every span created with that branch -- however deeply nested, however many other operations start and finish elsewhere on the root in the meantime -- reads back exactly the branch's scope. A lifecycle-covering span instead writes the root directly, matching
Message.RESOURCE_KEY's last-writer-wins "current message" semantics, which is correct because such a span is the context's dominant operation for its entire lifetime.
-
-
Method Details
-
addToContext
Returns aProcessingContextcarrying the givenscopeas the active span scope underRESOURCE_KEY. Functional helper for callers composing a context; note it returns a (possibly new) context rather than mutating the given one --Span.coverLifecycle(ProcessingContext)instead mutates the live context so spans created next with that same instance can read the active scope.The parent carrier for the closed-scope fallback (see
fromContext(ProcessingContext)) is captured fromcontextat this moment. A lifecycle scope that later replaces the root's carrier (last-writer-wins, viaSpan.coverLifecycle(ProcessingContext)) does not re-target already-created branches: their fallback chain still ends at the scope inherited here.- Parameters:
context- the processing context to add the scope toscope- the active span scope to add- Returns:
- the processing context carrying
scopeunderRESOURCE_KEY
-
fromContext
- Parameters:
context- the processing context to read the active span scope from- Returns:
- the active span scope, or
nullwhen none is present
-
span
Span span()Returns theSpangoverned by this scope.- Returns:
- the span this scope governs
-
isClosed
boolean isClosed()Returns whether this scope has closed for context resolution. The result is monotonic and thread-safe: closing must transition it totruebefore ending the provider span, and once this method returnstrue, it must never returnfalseagain. A scope already observed as closed is skipped during parent resolution; a concurrent resolver that observes it immediately before closure may still select it as a legitimate in-flight parent.- Returns:
trueafter this scope has closed, otherwisefalse- Since:
- 5.3.0
-
close
void close()Closes this scope, ending the underlyingSpan. Subsequent calls are no-ops -- implementations MUST be idempotent, so a synchronous throw (which closes the scope explicitly) and a later stream-termination callback (which would otherwise close it again) can safely overlap.- Specified by:
closein interfaceAutoCloseable
-
within
Executes the givenoperationwithin this scope and returns its result. Any scope-bound state an implementation maintains is observable to code running inside the operation and detached again before this method returns. This is the provider extension point the structuredSpanoperations execute through. Implementations MUST be transparent: return the operation's value, let its exceptions propagate unchanged, and never end the span. An implementation without scope-bound state simply executes the operation unchanged.- Type Parameters:
T- the operation's result type- Parameters:
operation- the operation to execute within this scope- Returns:
- the value produced by
operation
-
within
Executes the given voidoperationwithin this scope. Convenience overload ofwithin(Supplier)for operations without a result; it routes throughwithin(Supplier), so the same transparency rules apply.- Parameters:
operation- the operation to execute within this scope
-