Span represents a unit of work or operation. Spans can be organized in a hierarchy and also linked to form a graph. Spans can have associated events, exceptions, attributes. Log records can be associated with spans. Span context can be propagated between execution threads and processes.
The below code shows how to create nested spans:
Tracer tracer = openTelemetry.getTracer("test-telemetry");
Span parentSpan = tracer
.spanBuilder("parent")
.startSpan();
try (Scope parentScope = parentSpan.makeCurrent()) {
Span childSpan = tracer
.spanBuilder("child")
.startSpan();
try (Scope childScope = childSpan.makeCurrent()) {
// Perform child span's work
} finally {
childSpan.end();
}
} finally {
parentSpan.end();
}