Package org.bithon.agent.sdk.tracing
Class TraceScopeBuilder
java.lang.Object
org.bithon.agent.sdk.tracing.TraceScopeBuilder
Builder for creating trace scopes with fluent API.
Use
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.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.
-
operationName
-
traceId
-
parentSpanId
-
tracingMode
-
kind
-