Class TraceScopeBuilder
TraceContext.newTrace(String) to create an instance.
For example,
try (ITraceScope scope = TraceContext.newTrace("operation")
.kind(SpanKind.SERVER) // MUST give
.parent(traceId, parentSpanId) // optional
.tracingMode(TracingMode.LOGGING) // optional, default is TracingMode.TRACING
.attach()) {
// Update tags if needed
ISpan span = scope.currentSpan();
span.tag("key", "value");
// Business logic here
}
- Author:
- frank.chen021@outlook.com
- Date:
- 8/5/25 6:00 pm
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionattach()Creates a trace scope and attaches it to the current thread.Creates a trace scope and attaches it to the current thread, temporarily replacing any existing context.kind()Sets the parent trace context for this trace scope.private static booleantraceId()tracingMode(TracingMode tracingMode) Sets the tracing mode for this trace scope.
-
Field Details
-
LOGGER
-
LOG_INTERVAL_MS
private static final long LOG_INTERVAL_MS- See Also:
-
lastLogTime
private static long lastLogTime -
operationName
-
traceId
-
parentSpanId
-
tracingMode
-
kind
-
-
Constructor Details
-
TraceScopeBuilder
TraceScopeBuilder(String operationName)
-
-
Method Details
-
shouldLog
private static boolean shouldLog() -
parent
Sets the parent trace context for this trace scope. Use this to continue an existing trace in another thread or context.If not set, a new trace(with auto generated trace id and empty parent span id) will be created.
- Parameters:
traceId- the trace ID of the parent traceparentSpanId- the span ID of the parent span- Returns:
- this builder for method chaining
-
kind
-
tracingMode
Sets the tracing mode for this trace scope.- Parameters:
tracingMode- the tracing mode to use- Returns:
- this builder for method chaining
-
attach
Creates a trace scope and attaches it to the current thread.The created trace scope has a root span with the operation name and kind specified in the builder. If the
kind(SpanKind)is not called, the default kind isSpanKind.INTERNAL.- Returns:
- an attached ITraceScope ready for use in try-with-resources. Callers MUST ensure to close the scope to avoid resource leaks.
- Throws:
SdkException- if there's already a trace context attached to the current thread.
-
attachOrReplaceCurrent
Creates a trace scope and attaches it to the current thread, temporarily replacing any existing context.If
parent(String, String)is not configured with a non-empty trace ID and parent span ID, this method returns a no-op scope.If a tracing context already exists on the current thread, it will be suspended while the returned scope is active, and restored when the returned scope is closed. The suspended context is not finished by this scope.
This method is intended for asynchronous task frameworks that need to restore the task's own trace context even when the executor thread already has a context propagated by instrumentation.
- Returns:
- an attached ITraceScope ready for use in try-with-resources. Callers MUST ensure to close the scope to avoid resource leaks and restore the previous context.
- Since:
- 1.2.4
-
operationName
-
traceId
-
parentSpanId
-
tracingMode
-
kind
-