Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.j256.simplemetrics.metric.MetricValueDetails;
import com.j256.simplemetrics.utils.MiscUtils;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClient;
import software.amazon.awssdk.services.cloudwatch.CloudWatchClientBuilder;
import software.amazon.awssdk.services.cloudwatch.model.Dimension;
import software.amazon.awssdk.services.cloudwatch.model.MetricDatum;
import software.amazon.awssdk.services.cloudwatch.model.MetricDatum.Builder;
Expand Down Expand Up @@ -50,6 +52,7 @@ public class CloudWatchMetricsPersister implements MetricDetailsPersister {
private static final String AWS_INSTANCE_INFO_IP = "169.254.169.254";
private static final String INSTANCE_ID_FETCH_PATH = "/latest/meta-data/instance-id";
private static final int AWS_CONNECT_TIMEOUT_MILLIS = 2000;
private static final String AWS_REGION_ENV_VAR = "AWS_REGION";

// makes sure that we are running in EC2 and extracts the instance-id name
private static final Pattern INSTANCE_ID_RESULT_REGEX = Pattern.compile("(?s).*Server: EC2ws.*\r?\n\r?\n(i-.*)");
Expand All @@ -58,6 +61,7 @@ public class CloudWatchMetricsPersister implements MetricDetailsPersister {
private String applicationName = "unknown";
private String nameSpacePrefix = DEFAULT_NAME_SPACE_PREFIX;
private boolean addInstanceData = true;
private String awsRegion;

private CloudWatchClient cloudWatchClient;
private static String instanceId;
Expand Down Expand Up @@ -104,7 +108,18 @@ public CloudWatchMetricsPersister(String applicationName, boolean addInstanceDat
*/
public void initialize() {
if (cloudWatchClient == null) {
cloudWatchClient = CloudWatchClient.builder().build();
CloudWatchClientBuilder builder = CloudWatchClient.builder();
if (awsRegion == null) {
awsRegion = System.getenv(AWS_REGION_ENV_VAR);
}
if (awsRegion != null) {
builder.region(Region.of(awsRegion));
}
try {
cloudWatchClient = builder.build();
} catch (Exception e) {
throw new RuntimeException("Could not build CloudWatch client", e);
}
}
if (addInstanceData) {
instanceId = downloadInstanceId(AWS_CONNECT_TIMEOUT_MILLIS);
Expand Down Expand Up @@ -184,6 +199,10 @@ public void setCloudWatchClient(CloudWatchClient cloudWatchClient) {
this.cloudWatchClient = cloudWatchClient;
}

public void setAwsRegion(String awsRegion) {
this.awsRegion = awsRegion;
}

/**
* Return a map of metric name to associated data.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/main/javadoc/doc-files/changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2.4: ?/?/2026
* Added support for AWS_REGION env variable for testing purposes.

2.3: 2/16/2026
* Had to re-release to fix some javadoc issues. Grrrr.

Expand Down
Loading