Skip to content

Commit

Permalink
make endpoint override configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick committed Jun 20, 2024
1 parent 67e6039 commit 9238afc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import software.amazon.awssdk.services.cloudwatch.model.Metric
import software.amazon.awssdk.services.cloudwatch.model.MetricDataQuery
import software.amazon.awssdk.services.cloudwatch.model.MetricStat
import software.amazon.awssdk.services.cloudwatch.model.Statistic
import java.net.URI
import java.time.Duration
import java.time.Instant
import java.util.concurrent.TimeUnit
Expand All @@ -44,8 +43,7 @@ class EndToEndIT {
"hivemq-aws-cloudwatch-extension",
"extension-config.xml"
)
withEnv("AWS_ENDPOINT_OVERRIDE", URI("http://localstack:4566").toString())
withEnv("AWS_REGION_OVERRIDE", localStack.region)
withEnv("AWS_REGION", localStack.region)
withEnv("AWS_ACCESS_KEY_ID", localStack.accessKey)
withEnv("AWS_SECRET_ACCESS_KEY", localStack.secretKey)
withLogConsumer { println("HIVEMQ: " + it.utf8StringWithoutLineEnding) }
Expand Down
1 change: 1 addition & 0 deletions src/integrationTest/resources/extension-config.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cloudwatch-extension-configuration>
<aws-endpoint-override>http://localstack:4566</aws-endpoint-override>
<report-interval>1</report-interval>
<zero-value-submission>true</zero-value-submission>
<report-raw-count-value>true</report-raw-count-value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import software.amazon.awssdk.core.client.config.ClientAsyncConfiguration;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient;
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClientBuilder;

import java.net.URI;
import java.time.Duration;
Expand Down Expand Up @@ -65,25 +65,23 @@ void startCloudWatchReporter(
} else {
final Duration apiTimeout = cloudWatchConfig.getApiTimeout().map(Duration::ofMillis).orElse(null);

final String endpointOverride = System.getenv("AWS_ENDPOINT_OVERRIDE");
final URI endpoint = URI.create(endpointOverride);
System.out.println("ENDPOINT IS " + endpoint);

final String awsRegion = System.getenv("AWS_REGION_OVERRIDE");
System.out.println("AWS_REGION IS " + awsRegion);

final CloudWatchAsyncClient cloudWatchAsync = CloudWatchAsyncClient.builder()
.endpointOverride(endpoint)
.region(Region.of(awsRegion))
.credentialsProvider(DefaultCredentialsProvider.create())
.asyncConfiguration(ClientAsyncConfiguration.builder()
.advancedOption(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR, executorService)
.build())
.overrideConfiguration(ClientOverrideConfiguration.builder()
.apiCallTimeout(apiTimeout)
.apiCallAttemptTimeout(apiTimeout)
.build())
.build();
final CloudWatchAsyncClientBuilder cloudWatchAsyncClientBuilder = CloudWatchAsyncClient.builder();
if (configuration.getConfig().getAwsEndpointOverride() != null) {
cloudWatchAsyncClientBuilder.endpointOverride(URI.create(configuration.getConfig()
.getAwsEndpointOverride()));
}

final CloudWatchAsyncClient cloudWatchAsync =
cloudWatchAsyncClientBuilder.credentialsProvider(DefaultCredentialsProvider.create())
.asyncConfiguration(ClientAsyncConfiguration.builder()
.advancedOption(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR,
executorService)
.build())
.overrideConfiguration(ClientOverrideConfiguration.builder()
.apiCallTimeout(apiTimeout)
.apiCallAttemptTimeout(apiTimeout)
.build())
.build();

final CloudWatchReporter.Builder builder =
CloudWatchReporter.forRegistry(metricRegistry, cloudWatchAsync, METRIC_NAMESPACE);
Expand All @@ -94,8 +92,7 @@ void startCloudWatchReporter(
if (configuration.getConfig().getReportRawCountValue()) {
builder.withReportRawCountValue();
}
cloudWatchReporter = builder
.withZeroValuesSubmission()
cloudWatchReporter = builder.withZeroValuesSubmission()
.withReportRawCountValue()
.filter(new ConfiguredMetricsFilter(configuration.getEnabledMetrics()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class Config {
@XmlElement(name = "report-raw-count-value", defaultValue = "false")
private final boolean reportRawCountValue = false;

@XmlElement(name = "aws-endpoint-override")
private final @Nullable String awsEndpointOverride = null;

public final @NotNull List<Metric> getMetrics() {
return metrics;
}
Expand Down Expand Up @@ -85,6 +88,10 @@ public boolean getZeroValuesSubmission() {
return zeroValuesSubmission;
}

public @Nullable String getAwsEndpointOverride() {
return awsEndpointOverride;
}

@Override
public final @NotNull String toString() {
return "Config{" +
Expand Down

0 comments on commit 9238afc

Please sign in to comment.