-
Notifications
You must be signed in to change notification settings - Fork 962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate cloudwatch2 registry using localstack #4979
Open
sullis
wants to merge
1
commit into
micrometer-metrics:main
Choose a base branch
from
sullis:sean/cloudwatch2-localstack
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,7 @@ dependencies { | |
|
||
testImplementation project(':micrometer-test') | ||
testImplementation(libs.mockitoCore) | ||
testImplementation(libs.testcontainers.localstack) | ||
testImplementation(libs.awsSdk2NettyClient) | ||
testImplementation(libs.logbackLatest) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this? |
||
} |
123 changes: 123 additions & 0 deletions
123
...gistry-cloudwatch2/src/test/java/io/micrometer/cloudwatch2/CloudWatchIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* Copyright 2024 VMware, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micrometer.cloudwatch2; | ||
|
||
import io.micrometer.core.instrument.*; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
import org.testcontainers.containers.localstack.LocalStackContainer; | ||
import org.testcontainers.utility.DockerImageName; | ||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; | ||
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; | ||
import software.amazon.awssdk.http.async.SdkAsyncHttpClient; | ||
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.cloudwatch.CloudWatchAsyncClient; | ||
import software.amazon.awssdk.services.cloudwatch.model.*; | ||
|
||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.*; | ||
|
||
@Tag("docker") | ||
class CloudWatchIntegrationTest { | ||
|
||
private static final SdkAsyncHttpClient.Builder<?> AWS_SDK_HTTP_CLIENT_BUILDER = NettyNioAsyncHttpClient.builder(); | ||
|
||
private static final LocalStackContainer LOCALSTACK = new LocalStackContainer( | ||
DockerImageName.parse("localstack/localstack:3.3.0")) | ||
.withServices(LocalStackContainer.Service.CLOUDWATCH); | ||
|
||
private static final AwsCredentialsProvider AWS_CREDENTIALS_PROVIDER = StaticCredentialsProvider | ||
.create(AwsBasicCredentials.create(LOCALSTACK.getAccessKey(), LOCALSTACK.getSecretKey())); | ||
|
||
private static final Region AWS_REGION = Region.of(LOCALSTACK.getRegion()); | ||
|
||
private String meterName; | ||
|
||
private final CloudWatchConfig config = new CloudWatchConfig() { | ||
@Override | ||
public String get(String key) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String namespace() { | ||
return "namespace"; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("deprecation") | ||
public Duration readTimeout() { | ||
return Duration.ofSeconds(1); | ||
} | ||
}; | ||
|
||
private MockClock clock; | ||
|
||
private CloudWatchMeterRegistry registry; | ||
|
||
private CloudWatchAsyncClient cloudWatchAsyncClient; | ||
|
||
@BeforeEach | ||
void beforeEachTest() { | ||
clock = new MockClock(); | ||
meterName = UUID.randomUUID().toString(); | ||
cloudWatchAsyncClient = spy(CloudWatchAsyncClient.builder() | ||
.httpClient(AWS_SDK_HTTP_CLIENT_BUILDER.build()) | ||
.credentialsProvider(AWS_CREDENTIALS_PROVIDER) | ||
.region(AWS_REGION) | ||
.build()); | ||
registry = new CloudWatchMeterRegistry(config, clock, cloudWatchAsyncClient); | ||
} | ||
|
||
@Test | ||
void batchSizeShouldWorkOnMetricDatum() throws Exception { | ||
for (int i = 0; i < CloudWatchConfig.MAX_BATCH_SIZE; i++) { | ||
Timer.builder("timer." + i).register(this.registry); | ||
} | ||
assertThat(this.registry.metricData()).hasSize(2000); | ||
this.registry.publish(); | ||
@SuppressWarnings("unchecked") | ||
ArgumentCaptor<PutMetricDataRequest> argumentCaptor = ArgumentCaptor.forClass(PutMetricDataRequest.class); | ||
verify(this.cloudWatchAsyncClient, times(2)).putMetricData(argumentCaptor.capture()); | ||
List<PutMetricDataRequest> allValues = argumentCaptor.getAllValues(); | ||
assertThat(allValues.get(0).metricData()).hasSize(CloudWatchConfig.MAX_BATCH_SIZE); | ||
assertThat(allValues.get(1).metricData()).hasSize(CloudWatchConfig.MAX_BATCH_SIZE); | ||
} | ||
|
||
@Test | ||
void putMetricDataShouldBeCalledOnPublish() { | ||
registry.counter(meterName).increment(); | ||
registry.publish(); | ||
|
||
verify(cloudWatchAsyncClient, times(1)).putMetricData(isA(PutMetricDataRequest.class)); | ||
} | ||
|
||
@Test | ||
void emptyRegistryDoesNotPublishToAws() { | ||
assertThat(registry.metricData()).isEmpty(); | ||
registry.publish(); | ||
verifyNoInteractions(cloudWatchAsyncClient); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we use the default client shipped with the cloudwatch dependency?