66package io .opentelemetry .api .incubator .trace ;
77
88import io .opentelemetry .api .OpenTelemetry ;
9- import io .opentelemetry .api .common .AttributeKey ;
10- import io .opentelemetry .api .common .Attributes ;
11- import io .opentelemetry .api .incubator .propagation .ExtendedContextPropagators ;
129import io .opentelemetry .api .trace .Span ;
1310import io .opentelemetry .api .trace .SpanBuilder ;
14- import io .opentelemetry .api .trace .SpanContext ;
15- import io .opentelemetry .api .trace .SpanKind ;
16- import io .opentelemetry .api .trace .StatusCode ;
17- import io .opentelemetry .context .Context ;
18- import io .opentelemetry .context .Scope ;
1911import io .opentelemetry .context .propagation .ContextPropagators ;
20- import java .time .Instant ;
2112import java .util .Map ;
22- import java .util .concurrent .TimeUnit ;
2313import java .util .function .BiConsumer ;
2414
25- public final class ExtendedSpanBuilder implements SpanBuilder {
26- private final SpanBuilder delegate ;
27-
28- ExtendedSpanBuilder (SpanBuilder delegate ) {
29- this .delegate = delegate ;
30- }
31-
32- @ Override
33- public ExtendedSpanBuilder setParent (Context context ) {
34- delegate .setParent (context );
35- return this ;
36- }
37-
38- @ Override
39- public ExtendedSpanBuilder setNoParent () {
40- delegate .setNoParent ();
41- return this ;
42- }
43-
44- @ Override
45- public ExtendedSpanBuilder addLink (SpanContext spanContext ) {
46- delegate .addLink (spanContext );
47- return this ;
48- }
49-
50- @ Override
51- public ExtendedSpanBuilder addLink (SpanContext spanContext , Attributes attributes ) {
52- delegate .addLink (spanContext , attributes );
53- return this ;
54- }
55-
56- @ Override
57- public ExtendedSpanBuilder setAttribute (String key , String value ) {
58- delegate .setAttribute (key , value );
59- return this ;
60- }
61-
62- @ Override
63- public ExtendedSpanBuilder setAttribute (String key , long value ) {
64- delegate .setAttribute (key , value );
65- return this ;
66- }
67-
68- @ Override
69- public ExtendedSpanBuilder setAttribute (String key , double value ) {
70- delegate .setAttribute (key , value );
71- return this ;
72- }
73-
74- @ Override
75- public ExtendedSpanBuilder setAttribute (String key , boolean value ) {
76- delegate .setAttribute (key , value );
77- return this ;
78- }
79-
80- @ Override
81- public <T > ExtendedSpanBuilder setAttribute (AttributeKey <T > key , T value ) {
82- delegate .setAttribute (key , value );
83- return this ;
84- }
85-
86- @ Override
87- public ExtendedSpanBuilder setAllAttributes (Attributes attributes ) {
88- delegate .setAllAttributes (attributes );
89- return this ;
90- }
91-
92- @ Override
93- public ExtendedSpanBuilder setSpanKind (SpanKind spanKind ) {
94- delegate .setSpanKind (spanKind );
95- return this ;
96- }
97-
98- @ Override
99- public ExtendedSpanBuilder setStartTimestamp (long startTimestamp , TimeUnit unit ) {
100- delegate .setStartTimestamp (startTimestamp , unit );
101- return this ;
102- }
103-
104- @ Override
105- public ExtendedSpanBuilder setStartTimestamp (Instant startTimestamp ) {
106- delegate .setStartTimestamp (startTimestamp );
107- return this ;
108- }
15+ /** Extended {@link SpanBuilder} with experimental APIs. */
16+ public interface ExtendedSpanBuilder extends SpanBuilder {
10917
11018 /**
11119 * Extract a span context from the given carrier and set it as parent of the span for {@link
@@ -124,16 +32,7 @@ public ExtendedSpanBuilder setStartTimestamp(Instant startTimestamp) {
12432 * @param propagators provide the propagators from {@link OpenTelemetry#getPropagators()}
12533 * @param carrier the string map where to extract the span context from
12634 */
127- public ExtendedSpanBuilder setParentFrom (
128- ContextPropagators propagators , Map <String , String > carrier ) {
129- setParent (ExtendedContextPropagators .extractTextMapPropagationContext (carrier , propagators ));
130- return this ;
131- }
132-
133- @ Override
134- public Span startSpan () {
135- return delegate .startSpan ();
136- }
35+ ExtendedSpanBuilder setParentFrom (ContextPropagators propagators , Map <String , String > carrier );
13736
13837 /**
13938 * Runs the given {@link SpanCallable} inside of the span created by the given {@link
@@ -147,9 +46,7 @@ public Span startSpan() {
14746 * @param <E> the type of the exception
14847 * @return the result of the {@link SpanCallable}
14948 */
150- public <T , E extends Throwable > T startAndCall (SpanCallable <T , E > spanCallable ) throws E {
151- return startAndCall (spanCallable , ExtendedSpanBuilder ::setSpanError );
152- }
49+ <T , E extends Throwable > T startAndCall (SpanCallable <T , E > spanCallable ) throws E ;
15350
15451 /**
15552 * Runs the given {@link SpanCallable} inside of the span created by the given {@link
@@ -165,20 +62,8 @@ public <T, E extends Throwable> T startAndCall(SpanCallable<T, E> spanCallable)
16562 * @param <E> the type of the exception
16663 * @return the result of the {@link SpanCallable}
16764 */
168- public <T , E extends Throwable > T startAndCall (
169- SpanCallable <T , E > spanCallable , BiConsumer <Span , Throwable > handleException ) throws E {
170- Span span = startSpan ();
171-
172- //noinspection unused
173- try (Scope unused = span .makeCurrent ()) {
174- return spanCallable .callInSpan ();
175- } catch (Throwable e ) {
176- handleException .accept (span , e );
177- throw e ;
178- } finally {
179- span .end ();
180- }
181- }
65+ <T , E extends Throwable > T startAndCall (
66+ SpanCallable <T , E > spanCallable , BiConsumer <Span , Throwable > handleException ) throws E ;
18267
18368 /**
18469 * Runs the given {@link SpanRunnable} inside of the span created by the given {@link
@@ -190,10 +75,7 @@ public <T, E extends Throwable> T startAndCall(
19075 * @param runnable the {@link SpanRunnable} to run
19176 * @param <E> the type of the exception
19277 */
193- @ SuppressWarnings ("NullAway" )
194- public <E extends Throwable > void startAndRun (SpanRunnable <E > runnable ) throws E {
195- startAndRun (runnable , ExtendedSpanBuilder ::setSpanError );
196- }
78+ <E extends Throwable > void startAndRun (SpanRunnable <E > runnable ) throws E ;
19779
19880 /**
19981 * Runs the given {@link SpanRunnable} inside of the span created by the given {@link
@@ -206,25 +88,6 @@ public <E extends Throwable> void startAndRun(SpanRunnable<E> runnable) throws E
20688 * @param runnable the {@link SpanRunnable} to run
20789 * @param <E> the type of the exception
20890 */
209- @ SuppressWarnings ("NullAway" )
210- public <E extends Throwable > void startAndRun (
211- SpanRunnable <E > runnable , BiConsumer <Span , Throwable > handleException ) throws E {
212- startAndCall (
213- () -> {
214- runnable .runInSpan ();
215- return null ;
216- },
217- handleException );
218- }
219-
220- /**
221- * Marks a span as error. This is the default exception handler.
222- *
223- * @param span the span
224- * @param exception the exception that caused the error
225- */
226- private static void setSpanError (Span span , Throwable exception ) {
227- span .setStatus (StatusCode .ERROR );
228- span .recordException (exception );
229- }
91+ <E extends Throwable > void startAndRun (
92+ SpanRunnable <E > runnable , BiConsumer <Span , Throwable > handleException ) throws E ;
23093}
0 commit comments