SpanBuilder is used to construct Span instances which define arbitrary scopes of
code that are sampled for distributed tracing as a single atomic unit.
This is a simple example where all the work is being done within a single scope and a single thread and the Context is automatically propagated:
class MyClass {
private final Tracer tracer;
MyClass(OpenTelemetry openTelemetry) {
tracer = openTelemetry.getTracer("com.anyco.rpc");
}
void doWork() {
// Create a Span as a child of the current Span.
Span span = tracer.spanBuilder("MyChildSpan").startSpan();
try (Scope ss = span.makeCurrent()) {
span.addEvent("my event");
doSomeWork(); // Here the new span is in the current Context, so it can be used
// implicitly anywhere down the stack.
} finally {
span.end();
}
}
}
There might be cases where you do not perform all the work inside one static scope and the Context is automatically propagated:
class MyRpcServerInterceptorListener implements RpcServerInterceptor.Listener {
private final Tracer tracer;
private Span mySpan;
public MyRpcInterceptor(OpenTelemetry openTelemetry) {
tracer = openTelemetry.getTracer("com.example.rpc");
}
public void onRequest(String rpcName, Metadata metadata) {
// Create a Span as a child of the remote Span.
mySpan = tracer.spanBuilder(rpcName)
.setParent(extractContextFromMetadata(metadata)).startSpan();
}
public void onExecuteHandler(ServerCallHandler serverCallHandler) {
try (Scope ws = mySpan.makeCurrent()) {
Span.current().addEvent("Start rpc execution.");
serverCallHandler.run(); // Here the new span is in the current Context, so it can be
// used implicitly anywhere down the stack.
}
}
// Called when the RPC is canceled and guaranteed onComplete will not be called.
public void onCancel() {
// IMPORTANT: DO NOT forget to ended the Span here as the work is done.
mySpan.setStatus(StatusCode.ERROR);
mySpan.end();
}
// Called when the RPC is done and guaranteed onCancel will not be called.
public void onComplete(RpcStatus rpcStatus) {
// IMPORTANT: DO NOT forget to ended the Span here as the work is done.
mySpan.setStatus(rpcStatusToCanonicalTraceStatus(status);
mySpan.end();
}
}
This is a simple example where all the work is being done within a single scope and the Context is manually propagated:
class MyClass {
private final Tracer tracer;
MyClass(OpenTelemetry openTelemetry) {
tracer = openTelemetry.getTracer("com.example.rpc");
}
void doWork(Span parent) {
Span childSpan = tracer.spanBuilder("MyChildSpan")
.setParent(Context.current().with(parent))
.startSpan();
childSpan.addEvent("my event");
try {
doSomeWork(childSpan); // Manually propagate the new span down the stack.
} finally {
// To make sure we end the span even in case of an exception.
childSpan.end(); // Manually end the span.
}
}
}
see SpanBuilder#startSpan for usage examples.
| Modifier and Type | Method and Description |
|---|---|
| public SpanBuilder | Returns: this.the context of the linked spanContext)Span.Adds a link to the newly created |
| public SpanBuilder | Returns: this.the context of the linked spanContext, Attributes Span.the attributes of the attributes)Link.Adds a link to the newly created |
| public default SpanBuilder | Returns: this.the attributes attributes)Sets attributes to the |
| public SpanBuilder | Returns: this.the key for this attribute. key, String the value for this attribute. value)Sets an attribute to the newly created |
| public SpanBuilder | Returns: this.the key for this attribute. key, long the value for this attribute. value)Sets an attribute to the newly created |
| public SpanBuilder | Returns: this.the key for this attribute. key, double the value for this attribute. value)Sets an attribute to the newly created |
| public SpanBuilder | Returns: this.the key for this attribute. key, boolean the value for this attribute. value)Sets an attribute to the newly created |
| public <T> SpanBuilder | Returns: this.the key for this attribute. key, T the value for this attribute. value)Sets an attribute to the newly created |
| public default SpanBuilder | Returns: this.the key for this attribute. key, int the value for this attribute. value)Sets an attribute to the newly created |
| public SpanBuilder | |
| public SpanBuilder | |
| public SpanBuilder | Returns: this.the kind of the newly created spanKind)Span.Sets the |
| public SpanBuilder | Returns: this.the explicit start timestamp from the epoch of the newly created startTimestamp, TimeUnit Span.the unit of the timestamp. unit)Sets an explicit start timestamp for the newly created |
| public default SpanBuilder | Returns: this.the explicit start timestamp from the epoch of the newly created startTimestamp)Span.Sets an explicit start timestamp for the newly created |
| public Span |
| addLink | back to summary |
|---|---|
| public SpanBuilder addLink(SpanContext spanContext) Adds a link to the newly created Links are used to link Implementations may ignore calls with an invalid span context.
| |
| addLink | back to summary |
|---|---|
| public SpanBuilder addLink(SpanContext spanContext, Attributes attributes) Adds a link to the newly created Links are used to link Implementations may ignore calls with an invalid span context.
| |
| setAllAttributes | back to summary |
|---|---|
| public default SpanBuilder setAllAttributes(Attributes attributes) Sets attributes to the
| |
| setAttribute | back to summary |
|---|---|
| public SpanBuilder setAttribute(String key, String value) Sets an attribute to the newly created If a null or empty String Note It is strongly recommended to use
| |
| setAttribute | back to summary |
|---|---|
| public SpanBuilder setAttribute(String key, long value) Sets an attribute to the newly created Note It is strongly recommended to use
| |
| setAttribute | back to summary |
|---|---|
| public SpanBuilder setAttribute(String key, double value) Sets an attribute to the newly created Note It is strongly recommended to use
| |
| setAttribute | back to summary |
|---|---|
| public SpanBuilder setAttribute(String key, boolean value) Sets an attribute to the newly created Note It is strongly recommended to use
| |
| setAttribute | back to summary |
|---|---|
| public <T> SpanBuilder setAttribute(AttributeKey<T> key, T value) Sets an attribute to the newly created Note the behavior of null values is undefined, and hence strongly discouraged.
| |
| setAttribute | back to summary |
|---|---|
| public default SpanBuilder setAttribute(AttributeKey<Long> key, int value) Sets an attribute to the newly created
| |
| setNoParent | back to summary |
|---|---|
| public SpanBuilder setNoParent() Sets the option to become a root Observe that any previously set parent will be discarded.
| |
| setParent | back to summary |
|---|---|
| public SpanBuilder setParent(Context context) Sets the parent to use from the specified If no If called multiple times, only the last specified value will be used. Observe that the state
defined by a previous call to
| |
| setSpanKind | back to summary |
|---|---|
| public SpanBuilder setSpanKind(SpanKind spanKind) Sets the
| |
| setStartTimestamp | back to summary |
|---|---|
| public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) Sets an explicit start timestamp for the newly created LIRInstruction.Use this method to specify an explicit start timestamp. If not called, the
implementation will use the timestamp value at Important this is NOT equivalent with System.nanoTime().
| |
| setStartTimestamp | back to summary |
|---|---|
| public default SpanBuilder setStartTimestamp(Instant startTimestamp) Sets an explicit start timestamp for the newly created Use this method to specify an explicit start timestamp. If not called, the implementation
will use the timestamp value at Important this is NOT equivalent with System.nanoTime().
| |
| startSpan | back to summary |
|---|---|
| public Span startSpan() Starts a new Users must manually call Does not install the newly created Important Note This method can be called only once per Example of usage:
| |