6
6
package io .opentelemetry .api .incubator .trace ;
7
7
8
8
import 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 ;
12
9
import io .opentelemetry .api .trace .Span ;
13
10
import 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 ;
19
11
import io .opentelemetry .context .propagation .ContextPropagators ;
20
- import java .time .Instant ;
21
12
import java .util .Map ;
22
- import java .util .concurrent .TimeUnit ;
23
13
import java .util .function .BiConsumer ;
24
14
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 {
109
17
110
18
/**
111
19
* 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) {
124
32
* @param propagators provide the propagators from {@link OpenTelemetry#getPropagators()}
125
33
* @param carrier the string map where to extract the span context from
126
34
*/
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 );
137
36
138
37
/**
139
38
* Runs the given {@link SpanCallable} inside of the span created by the given {@link
@@ -147,9 +46,7 @@ public Span startSpan() {
147
46
* @param <E> the type of the exception
148
47
* @return the result of the {@link SpanCallable}
149
48
*/
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 ;
153
50
154
51
/**
155
52
* 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)
165
62
* @param <E> the type of the exception
166
63
* @return the result of the {@link SpanCallable}
167
64
*/
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 ;
182
67
183
68
/**
184
69
* 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(
190
75
* @param runnable the {@link SpanRunnable} to run
191
76
* @param <E> the type of the exception
192
77
*/
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 ;
197
79
198
80
/**
199
81
* 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
206
88
* @param runnable the {@link SpanRunnable} to run
207
89
* @param <E> the type of the exception
208
90
*/
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 ;
230
93
}
0 commit comments