Skip to content

Commit 9cf6eed

Browse files
committed
add proxy wrapper to remove excess code
1 parent d2dc050 commit 9cf6eed

File tree

2 files changed

+37
-79
lines changed

2 files changed

+37
-79
lines changed

instrumentation/aws-sdk/aws-sdk-2.2/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AbstractAws2ClientCoreTest.java

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@
3131
import io.opentelemetry.testing.internal.armeria.common.MediaType;
3232
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension;
3333
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.RecordedRequest;
34+
import java.lang.reflect.Method;
35+
import java.lang.reflect.Proxy;
3436
import java.util.Arrays;
37+
import java.util.concurrent.CompletableFuture;
3538
import java.util.concurrent.ExecutionException;
3639
import java.util.concurrent.Future;
3740
import java.util.function.Function;
@@ -243,23 +246,30 @@ private static void assertDynamoDbRequest(SpanDataAssert span, String operation)
243246
equalTo(maybeStable(DB_OPERATION), operation));
244247
}
245248

249+
@SuppressWarnings("unchecked")
250+
protected static <T, U> T wrapClient(
251+
Class<T> syncClientClass, Class<U> asyncClientClass, U asyncClient) {
252+
return (T)
253+
Proxy.newProxyInstance(
254+
AbstractAws2ClientCoreTest.class.getClassLoader(),
255+
new Class<?>[] {syncClientClass},
256+
(proxy, method, args) -> {
257+
Method asyncMethod =
258+
asyncClientClass.getMethod(method.getName(), method.getParameterTypes());
259+
CompletableFuture<?> future =
260+
(CompletableFuture<?>) asyncMethod.invoke(asyncClient, args);
261+
return future.get();
262+
});
263+
}
264+
246265
private static Stream<Arguments> provideArguments() {
247266
return Stream.of(
248267
Arguments.of(
249268
"CreateTable",
250-
(Function<DynamoDbClient, Object>) c -> c.createTable(createTableRequest()),
251-
(Function<DynamoDbAsyncClient, Object>) c -> c.createTable(createTableRequest())),
269+
(Function<DynamoDbClient, Object>) c -> c.createTable(createTableRequest())),
252270
Arguments.of(
253271
"DeleteItem",
254272
(Function<DynamoDbClient, Object>)
255-
c ->
256-
c.deleteItem(
257-
DeleteItemRequest.builder()
258-
.tableName("sometable")
259-
.key(createTableRequestKey)
260-
.conditionExpression("property in (:one, :two)")
261-
.build()),
262-
(Function<DynamoDbAsyncClient, Object>)
263273
c ->
264274
c.deleteItem(
265275
DeleteItemRequest.builder()
@@ -270,20 +280,10 @@ private static Stream<Arguments> provideArguments() {
270280
Arguments.of(
271281
"DeleteTable",
272282
(Function<DynamoDbClient, Object>)
273-
c -> c.deleteTable(DeleteTableRequest.builder().tableName("sometable").build()),
274-
(Function<DynamoDbAsyncClient, Object>)
275283
c -> c.deleteTable(DeleteTableRequest.builder().tableName("sometable").build())),
276284
Arguments.of(
277285
"GetItem",
278286
(Function<DynamoDbClient, Object>)
279-
c ->
280-
c.getItem(
281-
GetItemRequest.builder()
282-
.tableName("sometable")
283-
.key(getItemRequestKey)
284-
.attributesToGet("propertyOne", "propertyTwo")
285-
.build()),
286-
(Function<DynamoDbAsyncClient, Object>)
287287
c ->
288288
c.getItem(
289289
GetItemRequest.builder()
@@ -294,14 +294,6 @@ private static Stream<Arguments> provideArguments() {
294294
Arguments.of(
295295
"PutItem",
296296
(Function<DynamoDbClient, Object>)
297-
c ->
298-
c.putItem(
299-
PutItemRequest.builder()
300-
.tableName("sometable")
301-
.item(putItemRequestKey)
302-
.conditionExpression("attributeOne <> :someVal")
303-
.build()),
304-
(Function<DynamoDbAsyncClient, Object>)
305297
c ->
306298
c.putItem(
307299
PutItemRequest.builder()
@@ -312,16 +304,6 @@ private static Stream<Arguments> provideArguments() {
312304
Arguments.of(
313305
"Query",
314306
(Function<DynamoDbClient, Object>)
315-
c ->
316-
c.query(
317-
QueryRequest.builder()
318-
.tableName("sometable")
319-
.select("ALL_ATTRIBUTES")
320-
.keyConditionExpression("attribute = :aValue")
321-
.filterExpression("anotherAttribute = :someVal")
322-
.limit(10)
323-
.build()),
324-
(Function<DynamoDbAsyncClient, Object>)
325307
c ->
326308
c.query(
327309
QueryRequest.builder()
@@ -334,20 +316,6 @@ private static Stream<Arguments> provideArguments() {
334316
Arguments.of(
335317
"UpdateItem",
336318
(Function<DynamoDbClient, Object>)
337-
c ->
338-
c.updateItem(
339-
UpdateItemRequest.builder()
340-
.tableName("sometable")
341-
.key(
342-
ImmutableMap.of(
343-
"keyOne",
344-
AttributeValue.builder().s("value").build(),
345-
"keyTwo",
346-
AttributeValue.builder().s("differentValue").build()))
347-
.conditionExpression("attributeOne <> :someVal")
348-
.updateExpression("set attributeOne = :updateValue")
349-
.build()),
350-
(Function<DynamoDbAsyncClient, Object>)
351319
c ->
352320
c.updateItem(
353321
UpdateItemRequest.builder()
@@ -384,9 +352,7 @@ void testSendDynamoDbRequestWithBuilderAndMockedResponse(
384352
@ParameterizedTest
385353
@MethodSource("provideArguments")
386354
void testSendDynamoDbAsyncRequestWithBuilderAndMockedResponse(
387-
String operation,
388-
Function<DynamoDbClient, Object> call,
389-
Function<DynamoDbAsyncClient, Object> asyncCall)
355+
String operation, Function<DynamoDbClient, Object> call)
390356
throws ExecutionException, InterruptedException {
391357
DynamoDbAsyncClientBuilder builder = DynamoDbAsyncClient.builder();
392358
configureSdkClient(builder);
@@ -397,7 +363,8 @@ void testSendDynamoDbAsyncRequestWithBuilderAndMockedResponse(
397363
.credentialsProvider(CREDENTIALS_PROVIDER)
398364
.build();
399365
server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, ""));
400-
Object response = asyncCall.apply(client);
366+
Object response =
367+
call.apply(wrapClient(DynamoDbClient.class, DynamoDbAsyncClient.class, client));
401368
validateOperationResponse(operation, response);
402369
}
403370
}

instrumentation/aws-sdk/aws-sdk-2.2/testing/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AbstractAws2ClientTest.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@
9292
public abstract class AbstractAws2ClientTest extends AbstractAws2ClientCoreTest {
9393
private static final String QUEUE_URL = "http://xxx/somequeue";
9494

95-
private static void assumeSupportedConfig(String operation) {
96-
Assumptions.assumeFalse(
97-
operation.equals("SendMessage") && isSqsAttributeInjectionEnabled(),
98-
"Cannot check Sqs.SendMessage here due to hard-coded MD5.");
99-
}
100-
10195
// Force localhost instead of relying on mock server because using ip is yet another corner case
10296
// of the virtual bucket changes introduced by aws sdk v2.18.0. When using IP, there is no way to
10397
// prefix the hostname with the bucket name as label.
@@ -115,6 +109,12 @@ private static void assumeSupportedConfig(String operation) {
115109
+ " <ResponseMetadata><RequestId>0ac9cda2-bbf4-11d3-f92b-31fa5e8dbc99</RequestId></ResponseMetadata>"
116110
+ "</DeleteOptionGroupResponse>";
117111

112+
private static void assumeSupportedConfig(String operation) {
113+
Assumptions.assumeFalse(
114+
operation.equals("SendMessage") && isSqsAttributeInjectionEnabled(),
115+
"Cannot check Sqs.SendMessage here due to hard-coded MD5.");
116+
}
117+
118118
@SuppressWarnings("deprecation") // uses deprecated semconv
119119
private void clientAssertions(
120120
String service, String operation, String method, Object response, String requestId) {
@@ -322,7 +322,7 @@ void testS3AsyncSendOperationRequestWithBuilder(
322322
}
323323

324324
@Test
325-
void testKinesisSendOperationRequestWithBuilder() throws Exception {
325+
void testKinesisSendOperationRequestWithBuilder() {
326326
KinesisClientBuilder builder = KinesisClient.builder();
327327
configureSdkClient(builder);
328328
KinesisClient client =
@@ -372,8 +372,6 @@ private static Stream<Arguments> provideSqsArguments() {
372372
return HttpResponse.of(headers, HttpData.of(StandardCharsets.UTF_8, content));
373373
},
374374
(Function<SqsClient, Object>)
375-
c -> c.createQueue(CreateQueueRequest.builder().queueName("somequeue").build()),
376-
(Function<SqsAsyncClient, Future<?>>)
377375
c -> c.createQueue(CreateQueueRequest.builder().queueName("somequeue").build())),
378376
Arguments.of(
379377
"SendMessage",
@@ -407,10 +405,6 @@ private static Stream<Arguments> provideSqsArguments() {
407405
return HttpResponse.of(headers, HttpData.of(StandardCharsets.UTF_8, content));
408406
},
409407
(Function<SqsClient, Object>)
410-
c ->
411-
c.sendMessage(
412-
SendMessageRequest.builder().queueUrl(QUEUE_URL).messageBody("").build()),
413-
(Function<SqsAsyncClient, Future<?>>)
414408
c ->
415409
c.sendMessage(
416410
SendMessageRequest.builder().queueUrl(QUEUE_URL).messageBody("").build())));
@@ -451,8 +445,7 @@ void testSqsAsyncSendOperationRequestWithBuilder(
451445
String operation,
452446
String requestId,
453447
Callable<HttpResponse> serverResponse,
454-
Function<SqsClient, Object> call,
455-
Function<SqsAsyncClient, Future<?>> asyncCall)
448+
Function<SqsClient, Object> call)
456449
throws Exception {
457450
assumeSupportedConfig(operation);
458451

@@ -466,8 +459,7 @@ void testSqsAsyncSendOperationRequestWithBuilder(
466459
.build();
467460

468461
server.enqueue(serverResponse.call());
469-
Future<?> response = asyncCall.apply(client);
470-
response.get();
462+
Object response = call.apply(wrapClient(SqsClient.class, SqsAsyncClient.class, client));
471463

472464
clientAssertions("Sqs", operation, "POST", response, requestId);
473465
}
@@ -524,8 +516,7 @@ void testSnsSendOperationRequestWithBuilder(Function<SnsClient, Object> call) {
524516
clientAssertions("Sns", "Publish", "POST", response, "d74b8436-ae13-5ab4-a9ff-ce54dfea72a0");
525517
}
526518

527-
@ParameterizedTest
528-
@MethodSource("provideSnsArguments")
519+
@Test
529520
void testSnsAsyncSendOperationRequestWithBuilder() {
530521
SnsAsyncClientBuilder builder = SnsAsyncClient.builder();
531522
configureSdkClient(builder);
@@ -553,7 +544,7 @@ void testSnsAsyncSendOperationRequestWithBuilder() {
553544
}
554545

555546
@Test
556-
void testEc2SendOperationRequestWithBuilder() throws Exception {
547+
void testEc2SendOperationRequestWithBuilder() {
557548
Ec2ClientBuilder builder = Ec2Client.builder();
558549
configureSdkClient(builder);
559550
Ec2Client client =
@@ -575,7 +566,7 @@ void testEc2SendOperationRequestWithBuilder() throws Exception {
575566
}
576567

577568
@Test
578-
void testEc2AsyncSendOperationRequestWithBuilder() throws Exception {
569+
void testEc2AsyncSendOperationRequestWithBuilder() {
579570
Ec2AsyncClientBuilder builder = Ec2AsyncClient.builder();
580571
configureSdkClient(builder);
581572
Ec2AsyncClient client =
@@ -593,7 +584,7 @@ void testEc2AsyncSendOperationRequestWithBuilder() throws Exception {
593584
}
594585

595586
@Test
596-
void testRdsSendOperationRequestWithBuilder() throws Exception {
587+
void testRdsSendOperationRequestWithBuilder() {
597588
RdsClientBuilder builder = RdsClient.builder();
598589
configureSdkClient(builder);
599590
RdsClient client =
@@ -636,7 +627,7 @@ void testRdsAsyncSendOperationRequestWithBuilder() {
636627
// spans because of https://github.com/aws/aws-sdk-java-v2/issues/1741. We should at least tweak
637628
// the instrumentation to add Events for retries instead.
638629
@Test
639-
void testTimeoutAndRetryErrorsAreNotCaptured() throws Exception {
630+
void testTimeoutAndRetryErrorsAreNotCaptured() {
640631
// One retry so two requests.
641632
server.enqueue(HttpResponse.delayed(HttpResponse.of(HttpStatus.OK), Duration.ofSeconds(5)));
642633
server.enqueue(HttpResponse.delayed(HttpResponse.of(HttpStatus.OK), Duration.ofSeconds(5)));

0 commit comments

Comments
 (0)