Skip to content

Commit 37ca15b

Browse files
authored
Add http client response attributes to aws sqs process spans (#10074)
1 parent 30ddf6a commit 37ca15b

File tree

21 files changed

+105
-101
lines changed

21 files changed

+105
-101
lines changed

instrumentation/aws-sdk/aws-sdk-1.11/javaagent/src/test/groovy/S3TracingTest.groovy

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes
76
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
87
import io.opentelemetry.semconv.SemanticAttributes
98
import spock.lang.Shared
@@ -190,14 +189,15 @@ class S3TracingTest extends AgentInstrumentationSpecification {
190189
"rpc.system" "aws-api"
191190
"rpc.service" "AmazonSQS"
192191
"$SemanticAttributes.HTTP_REQUEST_METHOD" "POST"
192+
"$SemanticAttributes.HTTP_RESPONSE_STATUS_CODE" 200
193193
"$SemanticAttributes.URL_FULL" { it.startsWith("http://") }
194194
"$SemanticAttributes.SERVER_ADDRESS" String
195195
"$SemanticAttributes.SERVER_PORT" { it == null || Number }
196196
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
197197
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "s3ToSqsTestQueue"
198198
"$SemanticAttributes.MESSAGING_OPERATION" "process"
199199
"$SemanticAttributes.MESSAGING_MESSAGE_ID" String
200-
"$HttpAttributes.ERROR_TYPE" "_OTHER"
200+
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
201201
}
202202
}
203203
span(2) {
@@ -523,14 +523,15 @@ class S3TracingTest extends AgentInstrumentationSpecification {
523523
"rpc.system" "aws-api"
524524
"rpc.service" "AmazonSQS"
525525
"$SemanticAttributes.HTTP_REQUEST_METHOD" "POST"
526+
"$SemanticAttributes.HTTP_RESPONSE_STATUS_CODE" 200
526527
"$SemanticAttributes.URL_FULL" { it.startsWith("http://") }
527528
"$SemanticAttributes.SERVER_ADDRESS" String
528529
"$SemanticAttributes.SERVER_PORT" { it == null || Number }
529530
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
530531
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "s3ToSnsToSqsTestQueue"
531532
"$SemanticAttributes.MESSAGING_OPERATION" "process"
532533
"$SemanticAttributes.MESSAGING_MESSAGE_ID" String
533-
"$HttpAttributes.ERROR_TYPE" "_OTHER"
534+
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
534535
}
535536
}
536537
span(1) {

instrumentation/aws-sdk/aws-sdk-1.11/javaagent/src/test/groovy/SnsTracingTest.groovy

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes
76
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
87
import io.opentelemetry.semconv.SemanticAttributes
98
import spock.lang.Shared
@@ -179,14 +178,15 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
179178
"rpc.service" "AmazonSQS"
180179
"rpc.method" "ReceiveMessage"
181180
"$SemanticAttributes.HTTP_REQUEST_METHOD" "POST"
181+
"$SemanticAttributes.HTTP_RESPONSE_STATUS_CODE" 200
182182
"$SemanticAttributes.URL_FULL" String
183183
"$SemanticAttributes.SERVER_ADDRESS" String
184184
"$SemanticAttributes.SERVER_PORT" { it == null || Number }
185185
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
186186
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "snsToSqsTestQueue"
187187
"$SemanticAttributes.MESSAGING_OPERATION" "process"
188188
"$SemanticAttributes.MESSAGING_MESSAGE_ID" String
189-
"$HttpAttributes.ERROR_TYPE" "_OTHER"
189+
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
190190
}
191191
}
192192
span(2) {

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkInstrumenterFactory.java

+12-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import java.util.ArrayList;
2929
import java.util.Arrays;
3030
import java.util.List;
31-
import java.util.function.Function;
3231
import javax.annotation.Nullable;
3332

3433
final class AwsSdkInstrumenterFactory {
@@ -98,23 +97,23 @@ Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter() {
9897
openTelemetry,
9998
MessagingSpanNameExtractor.create(getter, operation),
10099
SpanKindExtractor.alwaysConsumer(),
101-
toSqsRequestExtractors(attributesExtractors(), Function.identity()),
100+
toSqsRequestExtractors(attributesExtractors()),
102101
singletonList(messagingAttributeExtractor),
103102
messagingReceiveInstrumentationEnabled);
104103
}
105104

106-
Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter() {
105+
Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter() {
107106
MessageOperation operation = MessageOperation.PROCESS;
108107
SqsProcessRequestAttributesGetter getter = SqsProcessRequestAttributesGetter.INSTANCE;
109-
AttributesExtractor<SqsProcessRequest, Void> messagingAttributeExtractor =
108+
AttributesExtractor<SqsProcessRequest, Response<?>> messagingAttributeExtractor =
110109
messagingAttributesExtractor(getter, operation);
111110

112-
InstrumenterBuilder<SqsProcessRequest, Void> builder =
113-
Instrumenter.<SqsProcessRequest, Void>builder(
111+
InstrumenterBuilder<SqsProcessRequest, Response<?>> builder =
112+
Instrumenter.<SqsProcessRequest, Response<?>>builder(
114113
openTelemetry,
115114
INSTRUMENTATION_NAME,
116115
MessagingSpanNameExtractor.create(getter, operation))
117-
.addAttributesExtractors(toSqsRequestExtractors(attributesExtractors(), unused -> null))
116+
.addAttributesExtractors(toSqsRequestExtractors(attributesExtractors()))
118117
.addAttributesExtractor(messagingAttributeExtractor);
119118

120119
if (messagingReceiveInstrumentationEnabled) {
@@ -128,14 +127,12 @@ Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter() {
128127
return builder.buildInstrumenter(SpanKindExtractor.alwaysConsumer());
129128
}
130129

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<>();
130+
private static List<AttributesExtractor<AbstractSqsRequest, Response<?>>> toSqsRequestExtractors(
131+
List<AttributesExtractor<Request<?>, Response<?>>> extractors) {
132+
List<AttributesExtractor<AbstractSqsRequest, Response<?>>> result = new ArrayList<>();
136133
for (AttributesExtractor<Request<?>, Response<?>> extractor : extractors) {
137134
result.add(
138-
new AttributesExtractor<AbstractSqsRequest, RESPONSE>() {
135+
new AttributesExtractor<AbstractSqsRequest, Response<?>>() {
139136
@Override
140137
public void onStart(
141138
AttributesBuilder attributes,
@@ -149,14 +146,9 @@ public void onEnd(
149146
AttributesBuilder attributes,
150147
Context context,
151148
AbstractSqsRequest sqsRequest,
152-
@Nullable RESPONSE response,
149+
@Nullable Response<?> response,
153150
@Nullable Throwable error) {
154-
extractor.onEnd(
155-
attributes,
156-
context,
157-
sqsRequest.getRequest(),
158-
responseConverter.apply(response),
159-
error);
151+
extractor.onEnd(attributes, context, sqsRequest.getRequest(), response, error);
160152
}
161153
});
162154
}

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/AwsSdkTelemetry.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static AwsSdkTelemetryBuilder builder(OpenTelemetry openTelemetry) {
4747

4848
private final Instrumenter<Request<?>, Response<?>> requestInstrumenter;
4949
private final Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter;
50-
private final Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter;
50+
private final Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter;
5151
private final Instrumenter<Request<?>, Response<?>> producerInstrumenter;
5252

5353
AwsSdkTelemetry(

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/SqsImpl.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ private static void afterConsumerResponse(
5757

5858
Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter =
5959
requestHandler.getConsumerReceiveInstrumenter();
60-
Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter =
60+
Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter =
6161
requestHandler.getConsumerProcessInstrumenter();
6262

6363
Context receiveContext = null;
@@ -75,7 +75,8 @@ private static void afterConsumerResponse(
7575
timer.now());
7676
}
7777

78-
addTracing(receiveMessageResult, request, consumerProcessInstrumenter, receiveContext);
78+
addTracing(
79+
receiveMessageResult, request, response, consumerProcessInstrumenter, receiveContext);
7980
}
8081

8182
private static final Field messagesField = getMessagesField();
@@ -93,7 +94,8 @@ private static Field getMessagesField() {
9394
private static void addTracing(
9495
ReceiveMessageResult receiveMessageResult,
9596
Request<?> request,
96-
Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter,
97+
Response<?> response,
98+
Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter,
9799
Context receiveContext) {
98100
if (messagesField == null) {
99101
return;
@@ -107,6 +109,7 @@ private static void addTracing(
107109
receiveMessageResult.getMessages(),
108110
consumerProcessInstrumenter,
109111
request,
112+
response,
110113
receiveContext));
111114
} catch (IllegalAccessException ignored) {
112115
// should not happen, we call setAccessible on the field

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/SqsProcessRequestAttributesGetter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55

66
package io.opentelemetry.instrumentation.awssdk.v1_11;
77

8+
import com.amazonaws.Response;
89
import io.opentelemetry.instrumentation.api.incubator.semconv.messaging.MessagingAttributesGetter;
910
import java.util.Collections;
1011
import java.util.List;
1112
import javax.annotation.Nullable;
1213

1314
enum SqsProcessRequestAttributesGetter
14-
implements MessagingAttributesGetter<SqsProcessRequest, Void> {
15+
implements MessagingAttributesGetter<SqsProcessRequest, Response<?>> {
1516
INSTANCE;
1617

1718
@Override
@@ -52,7 +53,7 @@ public Long getMessagePayloadCompressedSize(SqsProcessRequest request) {
5253

5354
@Override
5455
@Nullable
55-
public String getMessageId(SqsProcessRequest request, @Nullable Void response) {
56+
public String getMessageId(SqsProcessRequest request, @Nullable Response<?> response) {
5657
return request.getMessage().getMessageId();
5758
}
5859

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingIterator.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.instrumentation.awssdk.v1_11;
77

88
import com.amazonaws.Request;
9+
import com.amazonaws.Response;
910
import com.amazonaws.services.sqs.model.Message;
1011
import io.opentelemetry.context.Context;
1112
import io.opentelemetry.context.Scope;
@@ -16,8 +17,9 @@
1617
class TracingIterator implements Iterator<Message> {
1718

1819
private final Iterator<Message> delegateIterator;
19-
private final Instrumenter<SqsProcessRequest, Void> instrumenter;
20+
private final Instrumenter<SqsProcessRequest, Response<?>> instrumenter;
2021
private final Request<?> request;
22+
private final Response<?> response;
2123
private final Context receiveContext;
2224

2325
/*
@@ -30,21 +32,24 @@ class TracingIterator implements Iterator<Message> {
3032

3133
private TracingIterator(
3234
Iterator<Message> delegateIterator,
33-
Instrumenter<SqsProcessRequest, Void> instrumenter,
35+
Instrumenter<SqsProcessRequest, Response<?>> instrumenter,
3436
Request<?> request,
37+
Response<?> response,
3538
Context receiveContext) {
3639
this.delegateIterator = delegateIterator;
3740
this.instrumenter = instrumenter;
3841
this.request = request;
42+
this.response = response;
3943
this.receiveContext = receiveContext;
4044
}
4145

4246
public static Iterator<Message> wrap(
4347
Iterator<Message> delegateIterator,
44-
Instrumenter<SqsProcessRequest, Void> instrumenter,
48+
Instrumenter<SqsProcessRequest, Response<?>> instrumenter,
4549
Request<?> request,
50+
Response<?> response,
4651
Context receiveContext) {
47-
return new TracingIterator(delegateIterator, instrumenter, request, receiveContext);
52+
return new TracingIterator(delegateIterator, instrumenter, request, response, receiveContext);
4853
}
4954

5055
@Override
@@ -80,7 +85,7 @@ public Message next() {
8085
private void closeScopeAndEndSpan() {
8186
if (currentScope != null) {
8287
currentScope.close();
83-
instrumenter.end(currentContext, currentRequest, null, null);
88+
instrumenter.end(currentContext, currentRequest, response, null);
8489
currentScope = null;
8590
currentRequest = null;
8691
currentContext = null;

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingList.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.opentelemetry.instrumentation.awssdk.v1_11;
77

88
import com.amazonaws.Request;
9+
import com.amazonaws.Response;
910
import com.amazonaws.internal.SdkInternalList;
1011
import com.amazonaws.services.sqs.AmazonSQSClient;
1112
import com.amazonaws.services.sqs.model.Message;
@@ -18,28 +19,32 @@
1819
class TracingList extends SdkInternalList<Message> {
1920
private static final long serialVersionUID = 1L;
2021

21-
private final transient Instrumenter<SqsProcessRequest, Void> instrumenter;
22+
private final transient Instrumenter<SqsProcessRequest, Response<?>> instrumenter;
2223
private final transient Request<?> request;
24+
private final transient Response<?> response;
2325
private final transient Context receiveContext;
2426
private boolean firstIterator = true;
2527

2628
private TracingList(
2729
List<Message> list,
28-
Instrumenter<SqsProcessRequest, Void> instrumenter,
30+
Instrumenter<SqsProcessRequest, Response<?>> instrumenter,
2931
Request<?> request,
32+
Response<?> response,
3033
Context receiveContext) {
3134
super(list);
3235
this.instrumenter = instrumenter;
3336
this.request = request;
37+
this.response = response;
3438
this.receiveContext = receiveContext;
3539
}
3640

3741
public static SdkInternalList<Message> wrap(
3842
List<Message> list,
39-
Instrumenter<SqsProcessRequest, Void> instrumenter,
43+
Instrumenter<SqsProcessRequest, Response<?>> instrumenter,
4044
Request<?> request,
45+
Response<?> response,
4146
Context receiveContext) {
42-
return new TracingList(list, instrumenter, request, receiveContext);
47+
return new TracingList(list, instrumenter, request, response, receiveContext);
4348
}
4449

4550
@Override
@@ -49,7 +54,7 @@ public Iterator<Message> iterator() {
4954
// However, this is not thread-safe, but usually the first (hopefully only) traversal of
5055
// List is performed in the same thread that called receiveMessage()
5156
if (firstIterator && !inAwsClient()) {
52-
it = TracingIterator.wrap(super.iterator(), instrumenter, request, receiveContext);
57+
it = TracingIterator.wrap(super.iterator(), instrumenter, request, response, receiveContext);
5358
firstIterator = false;
5459
} else {
5560
it = super.iterator();

instrumentation/aws-sdk/aws-sdk-1.11/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v1_11/TracingRequestHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ final class TracingRequestHandler extends RequestHandler2 {
3434

3535
private final Instrumenter<Request<?>, Response<?>> requestInstrumenter;
3636
private final Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter;
37-
private final Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter;
37+
private final Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter;
3838
private final Instrumenter<Request<?>, Response<?>> producerInstrumenter;
3939

4040
TracingRequestHandler(
4141
Instrumenter<Request<?>, Response<?>> requestInstrumenter,
4242
Instrumenter<SqsReceiveRequest, Response<?>> consumerReceiveInstrumenter,
43-
Instrumenter<SqsProcessRequest, Void> consumerProcessInstrumenter,
43+
Instrumenter<SqsProcessRequest, Response<?>> consumerProcessInstrumenter,
4444
Instrumenter<Request<?>, Response<?>> producerInstrumenter) {
4545
this.requestInstrumenter = requestInstrumenter;
4646
this.consumerReceiveInstrumenter = consumerReceiveInstrumenter;
@@ -103,7 +103,7 @@ Instrumenter<SqsReceiveRequest, Response<?>> getConsumerReceiveInstrumenter() {
103103
return consumerReceiveInstrumenter;
104104
}
105105

106-
Instrumenter<SqsProcessRequest, Void> getConsumerProcessInstrumenter() {
106+
Instrumenter<SqsProcessRequest, Response<?>> getConsumerProcessInstrumenter() {
107107
return consumerProcessInstrumenter;
108108
}
109109

instrumentation/aws-sdk/aws-sdk-1.11/testing/src/main/groovy/io/opentelemetry/instrumentation/awssdk/v1_11/AbstractSqsSuppressReceiveSpansTest.groovy

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.amazonaws.services.sqs.AmazonSQSAsyncClient
1212
import com.amazonaws.services.sqs.AmazonSQSAsyncClientBuilder
1313
import com.amazonaws.services.sqs.model.ReceiveMessageRequest
1414
import com.amazonaws.services.sqs.model.SendMessageRequest
15-
import io.opentelemetry.instrumentation.api.semconv.http.internal.HttpAttributes
1615
import io.opentelemetry.instrumentation.test.InstrumentationSpecification
1716
import io.opentelemetry.instrumentation.test.utils.PortUtils
1817
import io.opentelemetry.semconv.SemanticAttributes
@@ -121,14 +120,15 @@ abstract class AbstractSqsSuppressReceiveSpansTest extends InstrumentationSpecif
121120
"rpc.system" "aws-api"
122121
"rpc.service" "AmazonSQS"
123122
"$SemanticAttributes.HTTP_REQUEST_METHOD" "POST"
123+
"$SemanticAttributes.HTTP_RESPONSE_STATUS_CODE" 200
124124
"$SemanticAttributes.URL_FULL" "http://localhost:$sqsPort"
125125
"$SemanticAttributes.SERVER_ADDRESS" "localhost"
126126
"$SemanticAttributes.SERVER_PORT" sqsPort
127127
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
128128
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "testSdkSqs"
129129
"$SemanticAttributes.MESSAGING_OPERATION" "process"
130130
"$SemanticAttributes.MESSAGING_MESSAGE_ID" String
131-
"$HttpAttributes.ERROR_TYPE" "_OTHER"
131+
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
132132
}
133133
}
134134
span(2) {
@@ -213,14 +213,15 @@ abstract class AbstractSqsSuppressReceiveSpansTest extends InstrumentationSpecif
213213
"rpc.system" "aws-api"
214214
"rpc.service" "AmazonSQS"
215215
"$SemanticAttributes.HTTP_REQUEST_METHOD" "POST"
216+
"$SemanticAttributes.HTTP_RESPONSE_STATUS_CODE" 200
216217
"$SemanticAttributes.URL_FULL" "http://localhost:$sqsPort"
217218
"$SemanticAttributes.SERVER_ADDRESS" "localhost"
218219
"$SemanticAttributes.SERVER_PORT" sqsPort
219220
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
220221
"$SemanticAttributes.MESSAGING_DESTINATION_NAME" "testSdkSqs"
221222
"$SemanticAttributes.MESSAGING_OPERATION" "process"
222223
"$SemanticAttributes.MESSAGING_MESSAGE_ID" String
223-
"$HttpAttributes.ERROR_TYPE" "_OTHER"
224+
"$SemanticAttributes.NETWORK_PROTOCOL_VERSION" "1.1"
224225
}
225226
}
226227
span(2) {

0 commit comments

Comments
 (0)