1
1
package io .opentelemetry .contrib .messaging .wrappers ;
2
2
3
3
import io .opentelemetry .api .OpenTelemetry ;
4
+ import io .opentelemetry .api .common .Attributes ;
5
+ import io .opentelemetry .api .common .AttributesBuilder ;
4
6
import io .opentelemetry .api .trace .Span ;
5
7
import io .opentelemetry .api .trace .SpanBuilder ;
6
8
import io .opentelemetry .api .trace .Tracer ;
9
11
import io .opentelemetry .context .propagation .TextMapGetter ;
10
12
import io .opentelemetry .context .propagation .TextMapPropagator ;
11
13
import io .opentelemetry .contrib .messaging .wrappers .semconv .MessagingProcessRequest ;
12
- import io .opentelemetry .contrib . messaging . wrappers . semconv . MessagingProcessResponse ;
14
+ import io .opentelemetry .instrumentation . api . instrumenter . AttributesExtractor ;
13
15
16
+ import javax .annotation .Nullable ;
14
17
import java .util .List ;
15
- import java .util .concurrent .Callable ;
16
- import java .util .logging .Level ;
17
- import java .util .logging .Logger ;
18
18
19
- public class MessagingProcessWrapper <REQUEST extends MessagingProcessRequest , RESPONSE extends MessagingProcessResponse <?>> {
20
-
21
- private static final Logger LOG = Logger .getLogger (MessagingProcessWrapper .class .getName ());
19
+ public class MessagingProcessWrapper <REQUEST extends MessagingProcessRequest > {
22
20
23
21
private static final String INSTRUMENTATION_SCOPE = "messaging-process-wrapper" ;
24
22
@@ -32,94 +30,57 @@ public class MessagingProcessWrapper<REQUEST extends MessagingProcessRequest, RE
32
30
33
31
private final TextMapGetter <REQUEST > textMapGetter ;
34
32
35
- private final List <MessagingSpanCustomizer <REQUEST , RESPONSE >> spanCustomizers ;
33
+ // no attributes need to be extracted from responses in process operations
34
+ private final List <AttributesExtractor <REQUEST , Void >> attributesExtractors ;
36
35
37
- public Runnable wrap (REQUEST request , Runnable runnable ) {
38
- return () -> {
39
- Span span = handleStart (request );
40
- Scope scope = span .makeCurrent ();
36
+ public static <REQUEST extends MessagingProcessRequest > DefaultMessagingProcessWrapperBuilder <REQUEST > defaultBuilder () {
37
+ return new DefaultMessagingProcessWrapperBuilder <>();
38
+ }
41
39
42
- try {
43
- runnable .run ();
44
- } catch (Throwable t ) {
45
- handleEnd (span , request , null , t );
46
- scope .close ();
47
- throw t ;
48
- }
40
+ public <E extends Throwable > void doProcess (REQUEST request , ThrowingRunnable <E > runnable ) throws E {
41
+ Span span = handleStart (request );
49
42
50
- handleEnd (span , request , null , null );
51
- scope .close ();
52
- };
53
- }
43
+ try (Scope scope = span .makeCurrent ()) {
44
+ runnable .run ();
45
+ } catch (Throwable t ) {
46
+ handleEnd (span , request , t );
47
+ throw t ;
48
+ }
54
49
55
- public <R > Callable <R > wrap (REQUEST request , Callable <R > callable ) {
56
- return () -> {
57
- Span span = handleStart (request );
58
- Scope scope = span .makeCurrent ();
59
- RESPONSE response = null ;
60
-
61
- R result = null ;
62
- try {
63
- result = callable .call ();
64
- if (result instanceof MessagingProcessResponse ) {
65
- response = (RESPONSE ) result ;
66
- }
67
- } catch (Throwable t ) {
68
- handleEnd (span , request , response , t );
69
- scope .close ();
70
- throw t ;
71
- }
72
-
73
- handleEnd (span , request , response , null );
74
- scope .close ();
75
- return result ;
76
- };
50
+ handleEnd (span , request , null );
77
51
}
78
52
79
- public <R > R doProcess (REQUEST request , Callable < R > process ) throws Exception {
53
+ public <R , E extends Throwable > R doProcess (REQUEST request , ThrowingSupplier < R , E > supplier ) throws E {
80
54
Span span = handleStart (request );
81
- Scope scope = span .makeCurrent ();
82
- RESPONSE response = null ;
83
55
84
56
R result = null ;
85
- try {
86
- result = process .call ();
87
- if (result instanceof MessagingProcessResponse ) {
88
- response = (RESPONSE ) result ;
89
- }
57
+ try (Scope scope = span .makeCurrent ()) {
58
+ result = supplier .get ();
90
59
} catch (Throwable t ) {
91
- handleEnd (span , request , response , t );
92
- scope .close ();
60
+ handleEnd (span , request , t );
93
61
throw t ;
94
62
}
95
63
96
- // noop response by default
97
- handleEnd (span , request , response , null );
98
- scope .close ();
64
+ handleEnd (span , request , null );
99
65
return result ;
100
66
}
101
67
102
68
protected Span handleStart (REQUEST request ) {
103
69
Context context = this .textMapPropagator .extract (Context .current (), request , this .textMapGetter );
104
70
SpanBuilder spanBuilder = this .tracer .spanBuilder (getDefaultSpanName (request .getDestination ()));
105
71
spanBuilder .setParent (context );
106
- for (MessagingSpanCustomizer <REQUEST , RESPONSE > customizer : spanCustomizers ) {
107
- try {
108
- context = customizer .onStart (spanBuilder , context , request );
109
- } catch (Exception e ) {
110
- LOG .log (Level .WARNING , "Exception occurred while customizing span on start." , e );
111
- }
72
+
73
+ AttributesBuilder builder = Attributes .builder ();
74
+ for (AttributesExtractor <REQUEST , Void > extractor : this .attributesExtractors ) {
75
+ extractor .onStart (builder , context , request );
112
76
}
113
- return spanBuilder .startSpan ();
77
+ return spanBuilder .setAllAttributes ( builder . build ()). startSpan ();
114
78
}
115
79
116
- protected void handleEnd (Span span , REQUEST request , RESPONSE response , Throwable t ) {
117
- for (MessagingSpanCustomizer <REQUEST , RESPONSE > customizer : spanCustomizers ) {
118
- try {
119
- customizer .onEnd (span , Context .current (), request , response , t );
120
- } catch (Exception e ) {
121
- LOG .log (Level .WARNING , "Exception occurred while customizing span on end." , e );
122
- }
80
+ protected void handleEnd (Span span , REQUEST request , Throwable t ) {
81
+ AttributesBuilder builder = Attributes .builder ();
82
+ for (AttributesExtractor <REQUEST , Void > extractor : this .attributesExtractors ) {
83
+ extractor .onEnd (builder , Context .current (), request , null , t );
123
84
}
124
85
span .end ();
125
86
}
@@ -132,11 +93,11 @@ protected String getDefaultSpanName(String destination) {
132
93
}
133
94
134
95
protected MessagingProcessWrapper (OpenTelemetry openTelemetry ,
135
- TextMapGetter <REQUEST > textMapGetter ,
136
- List <MessagingSpanCustomizer <REQUEST , RESPONSE >> spanCustomizers ) {
96
+ @ Nullable TextMapGetter <REQUEST > textMapGetter ,
97
+ List <AttributesExtractor <REQUEST , Void >> attributesExtractors ) {
137
98
this .textMapPropagator = openTelemetry .getPropagators ().getTextMapPropagator ();
138
99
this .tracer = openTelemetry .getTracer (INSTRUMENTATION_SCOPE + "-" + INSTRUMENTATION_VERSION );
139
100
this .textMapGetter = textMapGetter ;
140
- this .spanCustomizers = spanCustomizers ;
101
+ this .attributesExtractors = attributesExtractors ;
141
102
}
142
103
}
0 commit comments