diff --git a/src/main/java/com/plausiblelabs/metrics/reporting/CloudWatchReporter.java b/src/main/java/com/plausiblelabs/metrics/reporting/CloudWatchReporter.java index e2dcdbf..3e5492c 100644 --- a/src/main/java/com/plausiblelabs/metrics/reporting/CloudWatchReporter.java +++ b/src/main/java/com/plausiblelabs/metrics/reporting/CloudWatchReporter.java @@ -100,6 +100,17 @@ public Enabler(String namespace, AmazonCloudWatchClient client) { this.client = client; } + /** + * Sets the endpoint for the AmazonCloudWatchClient + * + * @param endpoint the region specific endpoint url. Must be non-null and not empty. + * @return this Enabler. + */ + public Enabler withEndpoint(String endpoint) { + this.client.setEndpoint(endpoint); + return this; + } + /** *

The histogram and meter percentiles to send. If .5 is included, it'll be reported as * median.This defaults to .5, .95, and .99. @@ -401,7 +412,7 @@ private void sendToCloudWatch() { } } - private boolean sentTooSmall, sentTooLarge; + private boolean sentTooSmall, sentTooLarge, sentNaN; private void sendValue(Date timestamp, String name, double value, StandardUnit unit, List dimensions) { double absValue = Math.abs(value); @@ -427,7 +438,14 @@ private void sendValue(Date timestamp, String name, double value, StandardUnit u LOG.debug("Value for {} is larger than what CloudWatch supports; trimming to {}. Further large values won't be logged.", name, value); sentTooLarge = true; } + } else if (Double.isNaN(value)) { + if (!sentNaN) { + LOG.debug("Value for {} is NaN; setting to 0. Further NaN values won't be logged.", name, value); + sentNaN = true; + } + value = 0; } + // TODO limit to 10 dimensions MetricDatum datum = new MetricDatum() .withTimestamp(timestamp) diff --git a/src/test/java/com/plausiblelabs/metrics/reporting/CloudWatchReporterTest.java b/src/test/java/com/plausiblelabs/metrics/reporting/CloudWatchReporterTest.java index c8a5cf5..8e64934 100644 --- a/src/test/java/com/plausiblelabs/metrics/reporting/CloudWatchReporterTest.java +++ b/src/test/java/com/plausiblelabs/metrics/reporting/CloudWatchReporterTest.java @@ -23,7 +23,8 @@ public class CloudWatchReporterTest { private MetricsRegistry testRegistry = new MetricsRegistry(); private DummyCloudWatchClient client = new DummyCloudWatchClient(); private CloudWatchReporter.Enabler enabler = - new CloudWatchReporter.Enabler("testnamespace", client).withRegistry(testRegistry); + new CloudWatchReporter.Enabler("testnamespace", client).withRegistry(testRegistry) + .withEndpoint("monitoring.us-west-2.amazonaws.com"); @After public void shutdownRegistry() { diff --git a/src/test/java/com/plausiblelabs/metrics/reporting/RemoteCloudWatchTest.java b/src/test/java/com/plausiblelabs/metrics/reporting/RemoteCloudWatchTest.java index e616a0e..8d654d1 100644 --- a/src/test/java/com/plausiblelabs/metrics/reporting/RemoteCloudWatchTest.java +++ b/src/test/java/com/plausiblelabs/metrics/reporting/RemoteCloudWatchTest.java @@ -55,7 +55,13 @@ public Double value() { return CloudWatchReporter.LARGEST_SENDABLE * 10; } }); - new CloudWatchReporter.Enabler("cxabf", creds) + Metrics.newGauge(new MetricName("test", "limits", "NaN"), new Gauge() { + @Override + public Double value() { + return Double.NaN; + } + }); + new CloudWatchReporter.Enabler("cxabf", creds).withEndpoint("monitoring.us-west-2.amazonaws.com") .withInstanceIdDimension("test").build().run(); }