Skip to content

Commit e8ed1d8

Browse files
committed
use reflection instead of bumping aws sdk version, fix remaining tests
1 parent 5912c1d commit e8ed1d8

File tree

4 files changed

+66
-20
lines changed

4 files changed

+66
-20
lines changed

instrumentation/aws-sdk/aws-sdk-1.11/javaagent/build.gradle.kts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ muzzle {
1313
pass {
1414
group.set("com.amazonaws")
1515
module.set("aws-java-sdk-core")
16-
versions.set("[1.11.33,)")
16+
versions.set("[1.10.33,)")
1717
assertInverse.set(true)
1818

1919
excludeInstrumentationName("aws-sdk-1.11-sqs")
@@ -22,15 +22,15 @@ muzzle {
2222
fail {
2323
group.set("com.amazonaws")
2424
module.set("aws-java-sdk-core")
25-
versions.set("[1.11.33,)")
25+
versions.set("[1.10.33,)")
2626

2727
excludeInstrumentationName("aws-sdk-1.11-core")
2828
}
2929

3030
pass {
3131
group.set("com.amazonaws")
3232
module.set("aws-java-sdk-sqs")
33-
versions.set("[1.11.33,)")
33+
versions.set("[1.10.33,)")
3434
}
3535
}
3636

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
6060
attributes {
6161
"aws.agent" "java-aws-sdk"
6262
"aws.endpoint" String
63-
"rpc.method" "CreateQueue"
6463
"aws.queue.name" queueName
64+
"aws.request_id" String
65+
"rpc.method" "CreateQueue"
6566
"rpc.system" "aws-api"
6667
"rpc.service" "AmazonSQS"
6768
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -82,10 +83,10 @@ class S3TracingTest extends AgentInstrumentationSpecification {
8283
attributes {
8384
"aws.agent" "java-aws-sdk"
8485
"aws.endpoint" String
86+
"aws.bucket.name" bucketName
8587
"rpc.method" "CreateBucket"
8688
"rpc.system" "aws-api"
8789
"rpc.service" "Amazon S3"
88-
"aws.bucket.name" bucketName
8990
"$HttpAttributes.HTTP_REQUEST_METHOD" "PUT"
9091
"$HttpAttributes.HTTP_RESPONSE_STATUS_CODE" 200
9192
"$UrlAttributes.URL_FULL" { it.startsWith("http://") }
@@ -104,8 +105,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
104105
attributes {
105106
"aws.agent" "java-aws-sdk"
106107
"aws.endpoint" String
107-
"rpc.method" "GetQueueAttributes"
108108
"aws.queue.url" queueUrl
109+
"aws.request_id" String
110+
"rpc.method" "GetQueueAttributes"
109111
"rpc.system" "aws-api"
110112
"rpc.service" "AmazonSQS"
111113
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -126,8 +128,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
126128
attributes {
127129
"aws.agent" "java-aws-sdk"
128130
"aws.endpoint" String
129-
"rpc.method" "SetQueueAttributes"
130131
"aws.queue.url" queueUrl
132+
"aws.request_id" String
133+
"rpc.method" "SetQueueAttributes"
131134
"rpc.system" "aws-api"
132135
"rpc.service" "AmazonSQS"
133136
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -188,8 +191,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
188191
attributes {
189192
"aws.agent" "java-aws-sdk"
190193
"aws.endpoint" String
191-
"rpc.method" "ReceiveMessage"
192194
"aws.queue.url" queueUrl
195+
"aws.request_id" String
196+
"rpc.method" "ReceiveMessage"
193197
"rpc.system" "aws-api"
194198
"rpc.service" "AmazonSQS"
195199
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -282,8 +286,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
282286
attributes {
283287
"aws.agent" "java-aws-sdk"
284288
"aws.endpoint" String
285-
"rpc.method" "PurgeQueue"
286289
"aws.queue.url" queueUrl
290+
"aws.request_id" String
291+
"rpc.method" "PurgeQueue"
287292
"rpc.system" "aws-api"
288293
"rpc.service" "AmazonSQS"
289294
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -336,8 +341,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
336341
attributes {
337342
"aws.agent" "java-aws-sdk"
338343
"aws.endpoint" String
339-
"rpc.method" "CreateQueue"
340344
"aws.queue.name" queueName
345+
"aws.request_id" String
346+
"rpc.method" "CreateQueue"
341347
"rpc.system" "aws-api"
342348
"rpc.service" "AmazonSQS"
343349
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -357,8 +363,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
357363
attributes {
358364
"aws.agent" "java-aws-sdk"
359365
"aws.endpoint" String
360-
"rpc.method" "GetQueueAttributes"
361366
"aws.queue.url" queueUrl
367+
"aws.request_id" String
368+
"rpc.method" "GetQueueAttributes"
362369
"rpc.system" "aws-api"
363370
"rpc.service" "AmazonSQS"
364371
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -399,6 +406,7 @@ class S3TracingTest extends AgentInstrumentationSpecification {
399406
attributes {
400407
"aws.agent" "java-aws-sdk"
401408
"aws.endpoint" String
409+
"aws.request_id" String
402410
"rpc.method" "CreateTopic"
403411
"rpc.system" "aws-api"
404412
"rpc.service" "AmazonSNS"
@@ -419,6 +427,7 @@ class S3TracingTest extends AgentInstrumentationSpecification {
419427
attributes {
420428
"aws.agent" "java-aws-sdk"
421429
"aws.endpoint" String
430+
"aws.request_id" String
422431
"rpc.method" "Subscribe"
423432
"rpc.system" "aws-api"
424433
"rpc.service" "AmazonSNS"
@@ -440,8 +449,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
440449
attributes {
441450
"aws.agent" "java-aws-sdk"
442451
"aws.endpoint" String
443-
"rpc.method" "SetQueueAttributes"
444452
"aws.queue.url" queueUrl
453+
"aws.request_id" String
454+
"rpc.method" "SetQueueAttributes"
445455
"rpc.system" "aws-api"
446456
"rpc.service" "AmazonSQS"
447457
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -461,6 +471,7 @@ class S3TracingTest extends AgentInstrumentationSpecification {
461471
attributes {
462472
"aws.agent" "java-aws-sdk"
463473
"aws.endpoint" String
474+
"aws.request_id" String
464475
"rpc.method" "SetTopicAttributes"
465476
"rpc.system" "aws-api"
466477
"rpc.service" "AmazonSNS"
@@ -524,8 +535,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
524535
attributes {
525536
"aws.agent" "java-aws-sdk"
526537
"aws.endpoint" String
527-
"rpc.method" "ReceiveMessage"
528538
"aws.queue.url" queueUrl
539+
"aws.request_id" String
540+
"rpc.method" "ReceiveMessage"
529541
"rpc.system" "aws-api"
530542
"rpc.service" "AmazonSQS"
531543
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -618,8 +630,9 @@ class S3TracingTest extends AgentInstrumentationSpecification {
618630
attributes {
619631
"aws.agent" "java-aws-sdk"
620632
"aws.endpoint" String
621-
"rpc.method" "PurgeQueue"
622633
"aws.queue.url" queueUrl
634+
"aws.request_id" String
635+
"rpc.method" "PurgeQueue"
623636
"rpc.system" "aws-api"
624637
"rpc.service" "AmazonSQS"
625638
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
5252
attributes {
5353
"aws.agent" "java-aws-sdk"
5454
"aws.endpoint" String
55-
"rpc.method" "CreateQueue"
5655
"aws.queue.name" queueName
56+
"aws.request_id" String
57+
"rpc.method" "CreateQueue"
5758
"rpc.system" "aws-api"
5859
"rpc.service" "AmazonSQS"
5960
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -74,8 +75,9 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
7475
attributes {
7576
"aws.agent" "java-aws-sdk"
7677
"aws.endpoint" String
77-
"rpc.method" "GetQueueAttributes"
7878
"aws.queue.url" queueUrl
79+
"aws.request_id" String
80+
"rpc.method" "GetQueueAttributes"
7981
"rpc.system" "aws-api"
8082
"rpc.service" "AmazonSQS"
8183
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -96,8 +98,9 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
9698
attributes {
9799
"aws.agent" "java-aws-sdk"
98100
"aws.endpoint" String
99-
"rpc.method" "SetQueueAttributes"
100101
"aws.queue.url" queueUrl
102+
"aws.request_id" String
103+
"rpc.method" "SetQueueAttributes"
101104
"rpc.system" "aws-api"
102105
"rpc.service" "AmazonSQS"
103106
"$HttpAttributes.HTTP_REQUEST_METHOD" "POST"
@@ -118,6 +121,7 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
118121
attributes {
119122
"aws.agent" "java-aws-sdk"
120123
"aws.endpoint" String
124+
"aws.request_id" String
121125
"rpc.method" "CreateTopic"
122126
"rpc.system" "aws-api"
123127
"rpc.service" "AmazonSNS"
@@ -139,6 +143,7 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
139143
attributes {
140144
"aws.agent" "java-aws-sdk"
141145
"aws.endpoint" String
146+
"aws.request_id" String
142147
"rpc.method" "Subscribe"
143148
"rpc.system" "aws-api"
144149
"rpc.service" "AmazonSNS"
@@ -160,6 +165,7 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
160165
attributes {
161166
"aws.agent" "java-aws-sdk"
162167
"aws.endpoint" String
168+
"aws.request_id" String
163169
"rpc.method" "Publish"
164170
"rpc.system" "aws-api"
165171
"rpc.service" "AmazonSNS"
@@ -180,6 +186,7 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
180186
"aws.agent" "java-aws-sdk"
181187
"aws.endpoint" String
182188
"aws.queue.url" queueUrl
189+
"aws.request_id" String
183190
"rpc.system" "aws-api"
184191
"rpc.service" "AmazonSQS"
185192
"rpc.method" "ReceiveMessage"

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,30 @@
1717
import com.amazonaws.AmazonWebServiceResult;
1818
import com.amazonaws.Request;
1919
import com.amazonaws.Response;
20+
import com.amazonaws.ResponseMetadata;
2021
import io.opentelemetry.api.common.AttributeKey;
2122
import io.opentelemetry.api.common.AttributesBuilder;
2223
import io.opentelemetry.context.Context;
2324
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
25+
import io.opentelemetry.javaagent.tooling.muzzle.NoMuzzle;
2426
import java.util.function.Function;
2527
import javax.annotation.Nullable;
2628

2729
class AwsSdkExperimentalAttributesExtractor
2830
implements AttributesExtractor<Request<?>, Response<?>> {
2931
private static final String COMPONENT_NAME = "java-aws-sdk";
32+
private static final boolean CAN_GET_RESPONSE_METADATA = canGetResponseMetadata();
33+
34+
// AmazonWebServiceResult is only available in v1.11.33 and later
35+
private static boolean canGetResponseMetadata() {
36+
try {
37+
Class<?> clazz = Class.forName("com.amazonaws.AmazonWebServiceResult");
38+
clazz.getMethod("getSdkResponseMetadata");
39+
return true;
40+
} catch (ClassNotFoundException | NoSuchMethodException exception) {
41+
return false;
42+
}
43+
}
3044

3145
@Override
3246
public void onStart(AttributesBuilder attributes, Context parentContext, Request<?> request) {
@@ -59,12 +73,24 @@ public void onEnd(
5973
Request<?> request,
6074
@Nullable Response<?> response,
6175
@Nullable Throwable error) {
62-
if (response != null && response.getAwsResponse() instanceof AmazonWebServiceResult) {
63-
AmazonWebServiceResult<?> awsResp = (AmazonWebServiceResult<?>) response.getAwsResponse();
64-
String requestId = awsResp.getSdkResponseMetadata().getRequestId();
76+
ResponseMetadata responseMetadata = getResponseMetadata(response);
77+
78+
if (responseMetadata != null) {
79+
String requestId = responseMetadata.getRequestId();
6580
if (requestId != null) {
6681
attributes.put(AWS_REQUEST_ID, requestId);
6782
}
6883
}
6984
}
85+
86+
@NoMuzzle
87+
private static ResponseMetadata getResponseMetadata(Response<?> response) {
88+
if (CAN_GET_RESPONSE_METADATA
89+
&& response != null
90+
&& response.getAwsResponse() instanceof AmazonWebServiceResult) {
91+
AmazonWebServiceResult<?> awsResp = (AmazonWebServiceResult<?>) response.getAwsResponse();
92+
return awsResp.getSdkResponseMetadata();
93+
}
94+
return null;
95+
}
7096
}

0 commit comments

Comments
 (0)