22
22
import io .opentelemetry .instrumentation .api .instrumenter .http .HttpClientAttributesExtractor ;
23
23
import io .opentelemetry .instrumentation .api .instrumenter .messaging .MessageOperation ;
24
24
import io .opentelemetry .instrumentation .api .instrumenter .messaging .MessagingAttributesExtractor ;
25
+ import io .opentelemetry .instrumentation .api .instrumenter .messaging .MessagingAttributesGetter ;
25
26
import io .opentelemetry .instrumentation .api .instrumenter .messaging .MessagingSpanNameExtractor ;
26
27
import io .opentelemetry .instrumentation .api .instrumenter .rpc .RpcClientAttributesExtractor ;
27
28
import java .util .ArrayList ;
28
29
import java .util .Arrays ;
29
30
import java .util .List ;
31
+ import java .util .function .Function ;
30
32
import javax .annotation .Nullable ;
31
33
32
34
final class AwsSdkInstrumenterFactory {
@@ -47,48 +49,73 @@ final class AwsSdkInstrumenterFactory {
47
49
httpAttributesExtractor , rpcAttributesExtractor , experimentalAttributesExtractor );
48
50
private static final AwsSdkSpanNameExtractor spanName = new AwsSdkSpanNameExtractor ();
49
51
50
- static Instrumenter <Request <?>, Response <?>> requestInstrumenter (
51
- OpenTelemetry openTelemetry , boolean captureExperimentalSpanAttributes ) {
52
+ private final OpenTelemetry openTelemetry ;
53
+ private final List <String > capturedHeaders ;
54
+ private final boolean captureExperimentalSpanAttributes ;
55
+ private final boolean messagingReceiveInstrumentationEnabled ;
52
56
57
+ AwsSdkInstrumenterFactory (
58
+ OpenTelemetry openTelemetry ,
59
+ List <String > capturedHeaders ,
60
+ boolean captureExperimentalSpanAttributes ,
61
+ boolean messagingReceiveInstrumentationEnabled ) {
62
+ this .openTelemetry = openTelemetry ;
63
+ this .capturedHeaders = capturedHeaders ;
64
+ this .captureExperimentalSpanAttributes = captureExperimentalSpanAttributes ;
65
+ this .messagingReceiveInstrumentationEnabled = messagingReceiveInstrumentationEnabled ;
66
+ }
67
+
68
+ Instrumenter <Request <?>, Response <?>> requestInstrumenter () {
53
69
return createInstrumenter (
54
70
openTelemetry ,
55
- captureExperimentalSpanAttributes ,
56
71
spanName ,
57
72
SpanKindExtractor .alwaysClient (),
73
+ attributesExtractors (),
58
74
emptyList (),
59
75
true );
60
76
}
61
77
62
- static Instrumenter <Request <?>, Response <?>> consumerReceiveInstrumenter (
63
- OpenTelemetry openTelemetry ,
64
- boolean captureExperimentalSpanAttributes ,
65
- boolean messagingReceiveInstrumentationEnabled ) {
66
- return sqsInstrumenter (
78
+ private List <AttributesExtractor <Request <?>, Response <?>>> attributesExtractors () {
79
+ return captureExperimentalSpanAttributes
80
+ ? extendedAttributesExtractors
81
+ : defaultAttributesExtractors ;
82
+ }
83
+
84
+ private <REQUEST , RESPONSE > AttributesExtractor <REQUEST , RESPONSE > messagingAttributesExtractor (
85
+ MessagingAttributesGetter <REQUEST , RESPONSE > getter , MessageOperation operation ) {
86
+ return MessagingAttributesExtractor .builder (getter , operation )
87
+ .setCapturedHeaders (capturedHeaders )
88
+ .build ();
89
+ }
90
+
91
+ Instrumenter <SqsReceiveRequest , Response <?>> consumerReceiveInstrumenter () {
92
+ MessageOperation operation = MessageOperation .RECEIVE ;
93
+ SqsReceiveRequestAttributesGetter getter = SqsReceiveRequestAttributesGetter .INSTANCE ;
94
+ AttributesExtractor <SqsReceiveRequest , Response <?>> messagingAttributeExtractor =
95
+ messagingAttributesExtractor (getter , operation );
96
+
97
+ return createInstrumenter (
67
98
openTelemetry ,
68
- MessageOperation .RECEIVE ,
69
- captureExperimentalSpanAttributes ,
99
+ MessagingSpanNameExtractor .create (getter , operation ),
100
+ SpanKindExtractor .alwaysConsumer (),
101
+ toSqsRequestExtractors (attributesExtractors (), Function .identity ()),
102
+ singletonList (messagingAttributeExtractor ),
70
103
messagingReceiveInstrumentationEnabled );
71
104
}
72
105
73
- static Instrumenter <SqsProcessRequest , Void > consumerProcessInstrumenter (
74
- OpenTelemetry openTelemetry ,
75
- boolean captureExperimentalSpanAttributes ,
76
- boolean messagingReceiveInstrumentationEnabled ) {
106
+ Instrumenter <SqsProcessRequest , Void > consumerProcessInstrumenter () {
77
107
MessageOperation operation = MessageOperation .PROCESS ;
78
108
SqsProcessRequestAttributesGetter getter = SqsProcessRequestAttributesGetter .INSTANCE ;
109
+ AttributesExtractor <SqsProcessRequest , Void > messagingAttributeExtractor =
110
+ messagingAttributesExtractor (getter , operation );
79
111
80
112
InstrumenterBuilder <SqsProcessRequest , Void > builder =
81
113
Instrumenter .<SqsProcessRequest , Void >builder (
82
114
openTelemetry ,
83
115
INSTRUMENTATION_NAME ,
84
116
MessagingSpanNameExtractor .create (getter , operation ))
85
- .addAttributesExtractors (
86
- toProcessRequestExtractors (
87
- captureExperimentalSpanAttributes
88
- ? extendedAttributesExtractors
89
- : defaultAttributesExtractors ))
90
- .addAttributesExtractor (
91
- MessagingAttributesExtractor .builder (getter , operation ).build ());
117
+ .addAttributesExtractors (toSqsRequestExtractors (attributesExtractors (), unused -> null ))
118
+ .addAttributesExtractor (messagingAttributeExtractor );
92
119
93
120
if (messagingReceiveInstrumentationEnabled ) {
94
121
builder .addSpanLinksExtractor (
@@ -101,77 +128,68 @@ static Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter(
101
128
return builder .buildInstrumenter (SpanKindExtractor .alwaysConsumer ());
102
129
}
103
130
104
- private static List <AttributesExtractor <SqsProcessRequest , Void >> toProcessRequestExtractors (
105
- List <AttributesExtractor <Request <?>, Response <?>>> extractors ) {
106
- List <AttributesExtractor <SqsProcessRequest , Void >> result = new ArrayList <>();
131
+ private static <RESPONSE >
132
+ List <AttributesExtractor <AbstractSqsRequest , RESPONSE >> toSqsRequestExtractors (
133
+ List <AttributesExtractor <Request <?>, Response <?>>> extractors ,
134
+ Function <RESPONSE , Response <?>> responseConverter ) {
135
+ List <AttributesExtractor <AbstractSqsRequest , RESPONSE >> result = new ArrayList <>();
107
136
for (AttributesExtractor <Request <?>, Response <?>> extractor : extractors ) {
108
137
result .add (
109
- new AttributesExtractor <SqsProcessRequest , Void >() {
138
+ new AttributesExtractor <AbstractSqsRequest , RESPONSE >() {
110
139
@ Override
111
140
public void onStart (
112
141
AttributesBuilder attributes ,
113
142
Context parentContext ,
114
- SqsProcessRequest sqsProcessRequest ) {
115
- extractor .onStart (attributes , parentContext , sqsProcessRequest .getRequest ());
143
+ AbstractSqsRequest sqsRequest ) {
144
+ extractor .onStart (attributes , parentContext , sqsRequest .getRequest ());
116
145
}
117
146
118
147
@ Override
119
148
public void onEnd (
120
149
AttributesBuilder attributes ,
121
150
Context context ,
122
- SqsProcessRequest sqsProcessRequest ,
123
- @ Nullable Void unused ,
151
+ AbstractSqsRequest sqsRequest ,
152
+ @ Nullable RESPONSE response ,
124
153
@ Nullable Throwable error ) {
125
- extractor .onEnd (attributes , context , sqsProcessRequest .getRequest (), null , error );
154
+ extractor .onEnd (
155
+ attributes ,
156
+ context ,
157
+ sqsRequest .getRequest (),
158
+ responseConverter .apply (response ),
159
+ error );
126
160
}
127
161
});
128
162
}
129
163
return result ;
130
164
}
131
165
132
- static Instrumenter <Request <?>, Response <?>> producerInstrumenter (
133
- OpenTelemetry openTelemetry , boolean captureExperimentalSpanAttributes ) {
134
- return sqsInstrumenter (
135
- openTelemetry , MessageOperation .PUBLISH , captureExperimentalSpanAttributes , true );
136
- }
137
-
138
- private static Instrumenter <Request <?>, Response <?>> sqsInstrumenter (
139
- OpenTelemetry openTelemetry ,
140
- MessageOperation operation ,
141
- boolean captureExperimentalSpanAttributes ,
142
- boolean enabled ) {
166
+ Instrumenter <Request <?>, Response <?>> producerInstrumenter () {
167
+ MessageOperation operation = MessageOperation .PUBLISH ;
143
168
SqsAttributesGetter getter = SqsAttributesGetter .INSTANCE ;
144
169
AttributesExtractor <Request <?>, Response <?>> messagingAttributeExtractor =
145
- MessagingAttributesExtractor . builder (getter , operation ). build ( );
170
+ messagingAttributesExtractor (getter , operation );
146
171
147
172
return createInstrumenter (
148
173
openTelemetry ,
149
- captureExperimentalSpanAttributes ,
150
174
MessagingSpanNameExtractor .create (getter , operation ),
151
- operation == MessageOperation .PUBLISH
152
- ? SpanKindExtractor .alwaysProducer ()
153
- : SpanKindExtractor .alwaysConsumer (),
175
+ SpanKindExtractor .alwaysProducer (),
176
+ attributesExtractors (),
154
177
singletonList (messagingAttributeExtractor ),
155
- enabled );
178
+ true );
156
179
}
157
180
158
- private static Instrumenter < Request <?>, Response <?> > createInstrumenter (
181
+ private static < REQUEST , RESPONSE > Instrumenter < REQUEST , RESPONSE > createInstrumenter (
159
182
OpenTelemetry openTelemetry ,
160
- boolean captureExperimentalSpanAttributes ,
161
- SpanNameExtractor < Request <?>> spanNameExtractor ,
162
- SpanKindExtractor < Request <? >> spanKindExtractor ,
163
- List <AttributesExtractor <Request <?>, Response <?> >> additionalAttributeExtractors ,
183
+ SpanNameExtractor < REQUEST > spanNameExtractor ,
184
+ SpanKindExtractor < REQUEST > spanKindExtractor ,
185
+ List <? extends AttributesExtractor <? super REQUEST , ? super RESPONSE >> attributeExtractors ,
186
+ List <AttributesExtractor <REQUEST , RESPONSE >> additionalAttributeExtractors ,
164
187
boolean enabled ) {
165
- return Instrumenter .<Request <?>, Response <?> >builder (
188
+ return Instrumenter .<REQUEST , RESPONSE >builder (
166
189
openTelemetry , INSTRUMENTATION_NAME , spanNameExtractor )
167
- .addAttributesExtractors (
168
- captureExperimentalSpanAttributes
169
- ? extendedAttributesExtractors
170
- : defaultAttributesExtractors )
190
+ .addAttributesExtractors (attributeExtractors )
171
191
.addAttributesExtractors (additionalAttributeExtractors )
172
192
.setEnabled (enabled )
173
193
.buildInstrumenter (spanKindExtractor );
174
194
}
175
-
176
- private AwsSdkInstrumenterFactory () {}
177
195
}
0 commit comments