Skip to content

Commit

Permalink
set up sns contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yiyuan-he committed Nov 27, 2024
1 parent 3ede02e commit a350133
Show file tree
Hide file tree
Showing 8 changed files with 493 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.assertj.core.api.ThrowingConsumer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand All @@ -46,7 +45,8 @@ public abstract class AwsSdkBaseTest extends ContractTestBase {
LocalStackContainer.Service.KINESIS,
LocalStackContainer.Service.SECRETSMANAGER,
LocalStackContainer.Service.IAM,
LocalStackContainer.Service.STEPFUNCTIONS)
LocalStackContainer.Service.STEPFUNCTIONS,
LocalStackContainer.Service.SNS)
.withEnv("DEFAULT_REGION", "us-west-2")
.withNetwork(network)
.withEnv("LOCALSTACK_HOST", "127.0.0.1")
Expand Down Expand Up @@ -110,6 +110,8 @@ protected String getApplicationWaitPattern() {

protected abstract String getStepFunctionsSpanNamePrefix();

protected abstract String getSnsSpanNamePrefix();

protected abstract String getS3RpcServiceName();

protected abstract String getDynamoDbRpcServiceName();
Expand All @@ -128,6 +130,8 @@ protected String getApplicationWaitPattern() {

protected abstract String getSecretsManagerRpcServiceName();

protected abstract String getSnsRpcServiceName();

protected abstract String getStepFunctionsRpcServiceName();

private String getS3ServiceName() {
Expand Down Expand Up @@ -166,7 +170,13 @@ private String getSecretsManagerServiceName() {
return "AWS::SecretsManager";
}

private String getStepFunctionsServiceName() { return "AWS::StepFunctions"; }
private String getStepFunctionsServiceName() {
return "AWS::StepFunctions";
}

protected String getSnsServiceName() {
return "AWS::SNS";
}

private String s3SpanName(String operation) {
return String.format("%s.%s", getS3SpanNamePrefix(), operation);
Expand Down Expand Up @@ -208,6 +218,10 @@ private String stepFunctionsSpanName(String operation) {
return String.format("%s.%s", getStepFunctionsSpanNamePrefix(), operation);
}

private String snsSpanName(String operation) {
return String.format("%s.%s", getSnsSpanNamePrefix(), operation);
}

protected ThrowingConsumer<KeyValue> assertAttribute(String key, String value) {
return (attribute) -> {
var actualKey = attribute.getKey();
Expand Down Expand Up @@ -3087,4 +3101,173 @@ protected void doTestStepFunctionsFault() throws Exception {
cloudformationIdentifier,
0.0);
}

protected void doTestSnsGetTopicAttributes() throws Exception {
appClient.get("/sns/gettopicattributes/test-topic").aggregate().join();
var traces = mockCollectorClient.getTraces();
var metrics =
mockCollectorClient.getMetrics(
Set.of(
AppSignalsConstants.ERROR_METRIC,
AppSignalsConstants.FAULT_METRIC,
AppSignalsConstants.LATENCY_METRIC));

var localService = getApplicationOtelServiceName();
var localOperation = "GET /sns/gettopicattributes/:topicId";
var type = "AWS::SNS::Topic";
var identifier = "test-topic";
var cloudformationIdentifier = "arn:aws:sns:us-west-2:000000000000:test-topic";

assertSpanClientAttributes(
traces,
snsSpanName("GetTopicAttributes"),
getSnsRpcServiceName(),
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
type,
identifier,
cloudformationIdentifier,
"localstack",
4566,
"http://localstack:4566",
200,
List.of(
assertAttribute(
SemanticConventionsConstants.AWS_TOPIC_ARN,
"arn:aws:sns:us-west-2:000000000000:test-topic")));
}

protected void doTestSnsError() throws Exception {
appClient.get("/sns/error").aggregate().join();
var traces = mockCollectorClient.getTraces();
var metrics =
mockCollectorClient.getMetrics(
Set.of(
AppSignalsConstants.ERROR_METRIC,
AppSignalsConstants.FAULT_METRIC,
AppSignalsConstants.LATENCY_METRIC));

var localService = getApplicationOtelServiceName();
var localOperation = "GET /sns/error";
assertSpanClientAttributes(
traces,
snsSpanName("GetTopicAttributes"),
getSnsRpcServiceName(),
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
"error.test",
8080,
"http://error.test:8080",
400,
List.of());

assertMetricClientAttributes(
metrics,
AppSignalsConstants.LATENCY_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
5000.0);

assertMetricClientAttributes(
metrics,
AppSignalsConstants.FAULT_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
0.0);

assertMetricClientAttributes(
metrics,
AppSignalsConstants.ERROR_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
1.0);
}

protected void doTestSnsFault() throws Exception {
appClient.get("/sns/fault").aggregate().join();
var traces = mockCollectorClient.getTraces();
var metrics =
mockCollectorClient.getMetrics(
Set.of(
AppSignalsConstants.ERROR_METRIC,
AppSignalsConstants.FAULT_METRIC,
AppSignalsConstants.LATENCY_METRIC));

var localService = getApplicationOtelServiceName();
var localOperation = "GET /sns/fault";
assertSpanClientAttributes(
traces,
snsSpanName("GetTopicAttributes"),
getSnsRpcServiceName(),
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
"fault.test",
8080,
"http://fault.test:8080",
500,
List.of());

assertMetricClientAttributes(
metrics,
AppSignalsConstants.LATENCY_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
5000.0);

assertMetricClientAttributes(
metrics,
AppSignalsConstants.FAULT_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
1.0);

assertMetricClientAttributes(
metrics,
AppSignalsConstants.ERROR_METRIC,
localService,
localOperation,
getSnsServiceName(),
"GetTopicAttributes",
null,
null,
null,
0.0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ protected String getStepFunctionsSpanNamePrefix() {
return "AWSStepFunctions";
}

@Override
protected String getSnsSpanNamePrefix() {
return "SNS";
}

protected String getS3RpcServiceName() {
return "Amazon S3";
}
Expand All @@ -110,6 +115,11 @@ protected String getStepFunctionsRpcServiceName() {
return "AWSStepFunctions";
}

@Override
protected String getSnsRpcServiceName() {
return "AmazonSNS";
}

protected String getKinesisRpcServiceName() {
return "AmazonKinesis";
}
Expand Down Expand Up @@ -315,4 +325,19 @@ void testStepFunctionsError() throws Exception {
void testStepFunctionsFault() throws Exception {
doTestStepFunctionsFault();
}

@Test
void testSnsGetTopicAttributes() throws Exception {
doTestSnsGetTopicAttributes();
}

@Test
void testSnsError() throws Exception {
doTestStepFunctionsError();
}

@Test
void testSnsFault() throws Exception {
doTestStepFunctionsFault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ protected String getStepFunctionsSpanNamePrefix() {
return "Sfn";
}

@Override
protected String getSnsSpanNamePrefix() {
return "Sns";
}

@Override
protected String getS3RpcServiceName() {
return "S3";
Expand Down Expand Up @@ -134,6 +139,11 @@ protected String getStepFunctionsRpcServiceName() {
return "Sfn";
}

@Override
protected String getSnsRpcServiceName() {
return "Sns";
}

@Test
void testS3CreateBucket() throws Exception {
doTestS3CreateBucket();
Expand Down Expand Up @@ -318,4 +328,19 @@ void testStepFunctionsError() throws Exception {
void testStepFunctionsFault() throws Exception {
doTestStepFunctionsFault();
}

@Test
void testSnsGetTopicAttributes() throws Exception {
doTestSnsGetTopicAttributes();
}

@Test
void testSnsError() throws Exception {
doTestStepFunctionsError();
}

@Test
void testSnsFault() throws Exception {
doTestStepFunctionsFault();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public class SemanticConventionsConstants {
public static final String AWS_SECRET_ARN = "aws.secretsmanager.secret.arn";
public static final String AWS_STATE_MACHINE_ARN = "aws.stepfunctions.state_machine.arn";
public static final String AWS_ACTIVITY_ARN = "aws.stepfunctions.activity.arn";
public static final String AWS_TOPIC_ARN = "aws.sns.topic.arn";

// kafka
public static final String MESSAGING_CLIENT_ID = "messaging.client_id";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
implementation("com.amazonaws:aws-java-sdk-secretsmanager")
implementation("com.amazonaws:aws-java-sdk-iam")
implementation("com.amazonaws:aws-java-sdk-stepfunctions")
implementation("com.amazonaws:aws-java-sdk-sns")
implementation("com.amazonaws:aws-java-sdk-bedrock")
implementation("com.amazonaws:aws-java-sdk-bedrockagent")
implementation("com.amazonaws:aws-java-sdk-bedrockruntime")
Expand Down
Loading

0 comments on commit a350133

Please sign in to comment.