Skip to content

Commit

Permalink
Updated v1_11 tests to account for dynamodb
Browse files Browse the repository at this point in the history
  • Loading branch information
akats7 committed Nov 21, 2024
1 parent 9857868 commit 3c0acac
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
import com.amazonaws.auth.AnonymousAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.handlers.RequestHandler2;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.sdk.testing.assertj.AttributeAssertion;
import io.opentelemetry.testing.internal.armeria.testing.junit5.server.mock.MockWebServerExtension;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -75,7 +75,7 @@ public void assertRequestWithMockedResponse(
String service,
String operation,
String method,
Map<String, String> additionalAttributes)
List<AttributeKeyPair<?>> additionalAttributes)
throws Exception {

assertThat(response).isNotNull();
Expand Down Expand Up @@ -114,7 +114,13 @@ public void assertRequestWithMockedResponse(
}

additionalAttributes.forEach(
(k, v) -> attributes.add(equalTo(stringKey(k), v)));
(att) -> {
if (att.getType() == AttributeType.STRING) {
attributes.add(equalTo(att.getStringKey(), att.getStringVal()));
} else if (att.getType() == AttributeType.STRING_ARRAY) {
attributes.add(equalTo(att.getStringArrayKey(), att.getStringArrayVal()));
}
});

span.hasName(service + "." + operation)
.hasKind(operation.equals("SendMessage") ? PRODUCER : CLIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
import com.amazonaws.services.dynamodbv2.model.CreateTableRequest;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringArrayKeyPair;
import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;

public abstract class AbstractDynamoDbClientTest extends AbstractBaseAwsClientTest {

Expand All @@ -34,13 +39,21 @@ public void sendRequestWithMockedResponse() throws Exception {

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, ""));

List<String> tableList = Collections.singletonList("sometable");

List<AttributeKeyPair<?>> additionalAttributes = Arrays.asList(
createStringKeyPair("aws.table.name", "sometable"),
createStringKeyPair("db.system", "dynamodb"),
createStringArrayKeyPair("aws.dynamodb.table_names", tableList)
);

Object response = client.createTable(new CreateTableRequest("sometable", null));
assertRequestWithMockedResponse(
response,
client,
"DynamoDBv2",
"CreateTable",
"POST",
ImmutableMap.of("aws.table.name", "sometable"));
additionalAttributes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ public void sendRequestWithMockedResponse() throws Exception {

Object response = client.allocateAddress();
assertRequestWithMockedResponse(
response, client, "EC2", "AllocateAddress", "POST", Collections.emptyMap());
response, client, "EC2", "AllocateAddress", "POST", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
import com.amazonaws.services.kinesis.AmazonKinesis;
import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder;
import com.amazonaws.services.kinesis.model.DeleteStreamRequest;
import com.google.common.collect.ImmutableMap;

import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.util.Map;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;

public abstract class AbstractKinesisClientTest extends AbstractBaseAwsClientTest {

public abstract AmazonKinesisClientBuilder configureClient(AmazonKinesisClientBuilder client);
Expand All @@ -42,7 +45,9 @@ public void testSendRequestWithMockedResponse(

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, ""));

Map<String, String> additionalAttributes = ImmutableMap.of("aws.stream.name", "somestream");
List<AttributeKeyPair<?>> additionalAttributes = Arrays.asList(
createStringKeyPair("aws.stream.name", "somestream")
);
Object response = call.apply(client);
assertRequestWithMockedResponse(
response, client, "Kinesis", operation, "POST", additionalAttributes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ public void sendRequestWithMockedResponse() throws Exception {

Object response = client.deleteOptionGroup(new DeleteOptionGroupRequest());
assertRequestWithMockedResponse(
response, client, "RDS", "DeleteOptionGroup", "POST", Collections.emptyMap());
response, client, "RDS", "DeleteOptionGroup", "POST", Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.CLIENT;
import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;
import static io.opentelemetry.instrumentation.test.utils.PortUtils.UNUSABLE_PORT;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.ErrorAttributes.ERROR_TYPE;
Expand All @@ -27,14 +28,13 @@
import com.amazonaws.retry.PredefinedRetryPolicies;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.sdk.trace.data.StatusData;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.time.Duration;
import java.util.Map;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
Expand All @@ -60,7 +60,7 @@ public void testSendRequestWithMockedResponse(
String operation,
String method,
Function<AmazonS3, Object> call,
Map<String, String> additionalAttributes)
List<AttributeKeyPair<?>> additionalAttributes)
throws Exception {

AmazonS3 client =
Expand All @@ -82,12 +82,12 @@ private static Stream<Arguments> provideArguments() {
"CreateBucket",
"PUT",
(Function<AmazonS3, Object>) c -> c.createBucket("testbucket"),
ImmutableMap.of("aws.bucket.name", "testbucket")),
createStringKeyPair("aws.bucket.name", "testbucket")),
Arguments.of(
"GetObject",
"GET",
(Function<AmazonS3, Object>) c -> c.getObject("someBucket", "someKey"),
ImmutableMap.of("aws.bucket.name", "someBucket")));
createStringKeyPair("aws.bucket.name", "somebucket")));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@

package io.opentelemetry.instrumentation.awssdk.v1_11;

import static io.opentelemetry.instrumentation.awssdk.v1_11.AttributeKeyPair.createStringKeyPair;
import static io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME;

import com.amazonaws.services.sns.AmazonSNS;
import com.amazonaws.services.sns.AmazonSNSClientBuilder;
import com.amazonaws.services.sns.model.PublishRequest;
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.testing.internal.armeria.common.HttpResponse;
import io.opentelemetry.testing.internal.armeria.common.HttpStatus;
import io.opentelemetry.testing.internal.armeria.common.MediaType;
import java.util.Map;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -52,8 +53,8 @@ public void testSendRequestWithMockedResponse(Function<AmazonSNS, Object> call)

server.enqueue(HttpResponse.of(HttpStatus.OK, MediaType.PLAIN_TEXT_UTF_8, body));

Map<String, String> additionalAttributes =
ImmutableMap.of(MESSAGING_DESTINATION_NAME.toString(), "somearn");
List<AttributeKeyPair<?>> additionalAttributes = Arrays.asList(
createStringKeyPair(MESSAGING_DESTINATION_NAME.toString(), "somearn"));

Object response = call.apply(client);
assertRequestWithMockedResponse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.opentelemetry.instrumentation.awssdk.v1_11;

import groovyjarjarantlr4.v4.runtime.misc.Nullable;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributeType;
import java.util.List;

public class AttributeKeyPair<T> {

private final AttributeKey<T> key;
private final T value;

AttributeKeyPair(AttributeKey<T> key, T value) {
this.key = key;
this.value = value;
}

public static AttributeKeyPair<String> createStringKeyPair(String keyString, String val) {
return new AttributeKeyPair<>(AttributeKey.stringKey(keyString), val);
}

public static AttributeKeyPair<List<String>> createStringArrayKeyPair(String keyString, List<String> val) {
return new AttributeKeyPair<>(AttributeKey.stringArrayKey(keyString), val);
}

public AttributeType getType() {
return key.getType();
}

@SuppressWarnings("unchecked")
public AttributeKey<String> getStringKey() {
if (key.getType() != AttributeType.STRING){
return null;
}
return (AttributeKey<String>) key;
}

@SuppressWarnings("unchecked")
@Nullable
public AttributeKey<List<String>> getStringArrayKey() {
if (key.getType() != AttributeType.STRING_ARRAY) {
return null;
}
return (AttributeKey<List<String>>) key;
}

@SuppressWarnings("unchecked")
public String getStringVal(){
if (key.getType() != AttributeType.STRING){
return null;
}
return (String) value;
}

@SuppressWarnings("unchecked")
@Nullable
public List<String> getStringArrayVal(){
if (key.getType() != AttributeType.STRING_ARRAY){
return null;
}
return (List<String>)value;
}
}

0 comments on commit 3c0acac

Please sign in to comment.