mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(cloudwatch): honor Unit filter in JSON GetMetricStatistics (#3932)
This commit is contained in:
1 parent
ace99b34b0
commit
8ccc89df41
5 files changed
+233
-3
No files matched your search
@@ -140,4 +140,49 @@ class CloudWatchTest {
|
||||
assertThat(dp.maximum()).isEqualTo(40.0);
|
||||
assertThat(dp.average()).isEqualTo(30.0); // sum / sampleCount
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("GetMetricStatistics honors the Unit filter")
|
||||
void testGetMetricStatisticsUnitFilter() {
|
||||
String namespace = "JavaTestUnitFilter";
|
||||
Instant now = Instant.now();
|
||||
|
||||
cloudWatchClient.putMetricData(request -> request
|
||||
.namespace(namespace)
|
||||
.metricData(
|
||||
datum -> datum
|
||||
.metricName("UnitMetric")
|
||||
.value(10.0)
|
||||
.unit(StandardUnit.COUNT),
|
||||
datum -> datum
|
||||
.metricName("UnitMetric")
|
||||
.value(20.0)
|
||||
.unit(StandardUnit.BYTES)
|
||||
)
|
||||
);
|
||||
|
||||
GetMetricStatisticsResponse matching = cloudWatchClient.getMetricStatistics(request -> request
|
||||
.namespace(namespace)
|
||||
.metricName("UnitMetric")
|
||||
.startTime(now.minus(1, ChronoUnit.HOURS))
|
||||
.endTime(now.plus(1, ChronoUnit.MINUTES))
|
||||
.period(3600)
|
||||
.statistics(Statistic.SUM)
|
||||
.unit(StandardUnit.COUNT)
|
||||
);
|
||||
assertThat(matching.datapoints()).hasSize(1);
|
||||
assertThat(matching.datapoints().get(0).sum()).isEqualTo(10.0);
|
||||
assertThat(matching.datapoints().get(0).unit()).isEqualTo(StandardUnit.COUNT);
|
||||
|
||||
GetMetricStatisticsResponse noMatch = cloudWatchClient.getMetricStatistics(request -> request
|
||||
.namespace(namespace)
|
||||
.metricName("UnitMetric")
|
||||
.startTime(now.minus(1, ChronoUnit.HOURS))
|
||||
.endTime(now.plus(1, ChronoUnit.MINUTES))
|
||||
.period(3600)
|
||||
.statistics(Statistic.SUM)
|
||||
.unit(StandardUnit.MILLISECONDS)
|
||||
);
|
||||
assertThat(noMatch.datapoints()).isEmpty();
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -127,9 +127,11 @@ public class CloudWatchMetricsJsonHandler {
|
||||
statsNode.forEach(s -> statistics.add(s.asText()));
|
||||
}
|
||||
|
||||
String unit = request.hasNonNull("Unit") ? request.path("Unit").asText() : null;
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> datapoints =
|
||||
metricsService.getMetricStatistics(namespace, metricName, dimensions,
|
||||
startTime, endTime, period, statistics, null, region);
|
||||
startTime, endTime, period, statistics, unit, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
response.put("Label", metricName);
|
||||
@@ -334,7 +336,7 @@ public class CloudWatchMetricsJsonHandler {
|
||||
String metricName = metricNode.path("MetricName").asText();
|
||||
int period = msNode.path("Period").asInt(60);
|
||||
String stat = msNode.path("Stat").asText();
|
||||
String unit = msNode.has("Unit") ? msNode.path("Unit").asText() : null;
|
||||
String unit = msNode.hasNonNull("Unit") ? msNode.path("Unit").asText() : null;
|
||||
List<Dimension> dims = parseDimensionsJson(metricNode.path("Dimensions"));
|
||||
|
||||
metricStat = new CloudWatchMetricsService.MetricStat(
|
||||
|
||||
+103
-1
@@ -65,12 +65,21 @@ class CloudWatchMetricsJsonHandlerTest {
|
||||
private Response putMetric(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
double value, Instant timestamp) {
|
||||
return putMetric(namespace, metricName, dimName, dimValue, value, timestamp, null);
|
||||
}
|
||||
|
||||
private Response putMetric(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
double value, Instant timestamp, String unit) {
|
||||
ObjectNode req = MAPPER.createObjectNode();
|
||||
req.put("Namespace", namespace);
|
||||
var datum = req.putArray("MetricData").addObject();
|
||||
ObjectNode datum = req.putArray("MetricData").addObject();
|
||||
datum.put("MetricName", metricName);
|
||||
datum.put("Value", value);
|
||||
datum.put("Timestamp", sdkTimestamp(timestamp));
|
||||
if (unit != null) {
|
||||
datum.put("Unit", unit);
|
||||
}
|
||||
datum.putArray("Dimensions").addObject()
|
||||
.put("Name", dimName).put("Value", dimValue);
|
||||
return handler.handle("PutMetricData", req, REGION);
|
||||
@@ -79,6 +88,28 @@ class CloudWatchMetricsJsonHandlerTest {
|
||||
private ObjectNode getStats(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
Instant startTime, Instant endTime, int period) {
|
||||
return getStatsWithUnit(namespace, metricName, dimName, dimValue,
|
||||
startTime, endTime, period, null, false);
|
||||
}
|
||||
|
||||
private ObjectNode getStats(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
Instant startTime, Instant endTime, int period, String unit) {
|
||||
return getStatsWithUnit(namespace, metricName, dimName, dimValue,
|
||||
startTime, endTime, period, unit, false);
|
||||
}
|
||||
|
||||
private ObjectNode getStatsWithExplicitNullUnit(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
Instant startTime, Instant endTime, int period) {
|
||||
return getStatsWithUnit(namespace, metricName, dimName, dimValue,
|
||||
startTime, endTime, period, null, true);
|
||||
}
|
||||
|
||||
private ObjectNode getStatsWithUnit(String namespace, String metricName,
|
||||
String dimName, String dimValue,
|
||||
Instant startTime, Instant endTime, int period,
|
||||
String unit, boolean explicitNull) {
|
||||
ObjectNode req = MAPPER.createObjectNode();
|
||||
req.put("Namespace", namespace);
|
||||
req.put("MetricName", metricName);
|
||||
@@ -88,6 +119,11 @@ class CloudWatchMetricsJsonHandlerTest {
|
||||
req.putArray("Dimensions").addObject()
|
||||
.put("Name", dimName).put("Value", dimValue);
|
||||
req.putArray("Statistics").add("Sum");
|
||||
if (explicitNull) {
|
||||
req.putNull("Unit");
|
||||
} else if (unit != null) {
|
||||
req.put("Unit", unit);
|
||||
}
|
||||
Response resp = handler.handle("GetMetricStatistics", req, REGION);
|
||||
assertEquals(200, resp.getStatus());
|
||||
return (ObjectNode) resp.getEntity();
|
||||
@@ -225,4 +261,70 @@ class CloudWatchMetricsJsonHandlerTest {
|
||||
assertEquals(0, oldResult.get("Datapoints").size(),
|
||||
"metric from 24h ago must not be returned for a 20-second window around now");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricStatistics_jsonHonorsUnitFilter() {
|
||||
putMetric("NS", "M", "type", "current", 10.0, EPOCH_NOW, "Count");
|
||||
putMetric("NS", "M", "type", "current", 20.0, EPOCH_NOW, "Bytes");
|
||||
|
||||
ObjectNode count = getStats("NS", "M", "type", "current",
|
||||
EPOCH_NOW.minusSeconds(10), EPOCH_NOW.plusSeconds(10), 60, "Count");
|
||||
assertEquals(1, count.get("Datapoints").size());
|
||||
assertEquals(10.0, count.get("Datapoints").get(0).get("Sum").asDouble());
|
||||
assertEquals("Count", count.get("Datapoints").get(0).get("Unit").asText());
|
||||
|
||||
ObjectNode bytes = getStats("NS", "M", "type", "current",
|
||||
EPOCH_NOW.minusSeconds(10), EPOCH_NOW.plusSeconds(10), 60, "Bytes");
|
||||
assertEquals(1, bytes.get("Datapoints").size());
|
||||
assertEquals(20.0, bytes.get("Datapoints").get(0).get("Sum").asDouble());
|
||||
assertEquals("Bytes", bytes.get("Datapoints").get(0).get("Unit").asText());
|
||||
|
||||
ObjectNode noMatch = getStats("NS", "M", "type", "current",
|
||||
EPOCH_NOW.minusSeconds(10), EPOCH_NOW.plusSeconds(10), 60, "Percent");
|
||||
assertEquals(0, noMatch.get("Datapoints").size(),
|
||||
"a unit no datum carries must filter every datapoint out");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricStatistics_jsonTreatsOmittedAndNullUnitAsNoFilter() {
|
||||
putMetric("NS", "M", "type", "current", 10.0, EPOCH_NOW, "Count");
|
||||
putMetric("NS", "M", "type", "current", 20.0, EPOCH_NOW, "Bytes");
|
||||
|
||||
ObjectNode omitted = getStats("NS", "M", "type", "current",
|
||||
EPOCH_NOW.minusSeconds(10), EPOCH_NOW.plusSeconds(10), 60);
|
||||
ObjectNode explicitNull = getStatsWithExplicitNullUnit("NS", "M", "type", "current",
|
||||
EPOCH_NOW.minusSeconds(10), EPOCH_NOW.plusSeconds(10), 60);
|
||||
|
||||
assertTrue(omitted.get("Datapoints").size() > 0,
|
||||
"an omitted Unit must not filter every datapoint out");
|
||||
assertEquals(omitted.get("Datapoints"), explicitNull.get("Datapoints"),
|
||||
"an explicit JSON null Unit must mean no filter, exactly like an omitted Unit");
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricData_jsonTreatsExplicitNullUnitAsNoFilter() {
|
||||
putMetric("NS", "M", "type", "current", 10.0, EPOCH_NOW, "Count");
|
||||
putMetric("NS", "M", "type", "current", 20.0, EPOCH_NOW, "Bytes");
|
||||
|
||||
ObjectNode req = MAPPER.createObjectNode();
|
||||
req.put("StartTime", sdkTimestamp(EPOCH_NOW.minusSeconds(10)));
|
||||
req.put("EndTime", sdkTimestamp(EPOCH_NOW.plusSeconds(10)));
|
||||
ObjectNode query = req.putArray("MetricDataQueries").addObject();
|
||||
query.put("Id", "m0");
|
||||
ObjectNode metricStat = query.putObject("MetricStat");
|
||||
ObjectNode metric = metricStat.putObject("Metric");
|
||||
metric.put("Namespace", "NS");
|
||||
metric.put("MetricName", "M");
|
||||
metric.putArray("Dimensions").addObject()
|
||||
.put("Name", "type").put("Value", "current");
|
||||
metricStat.put("Period", 60);
|
||||
metricStat.put("Stat", "Sum");
|
||||
metricStat.putNull("Unit");
|
||||
|
||||
Response resp = handler.handle("GetMetricData", req, REGION);
|
||||
assertEquals(200, resp.getStatus());
|
||||
ObjectNode body = (ObjectNode) resp.getEntity();
|
||||
assertTrue(body.get("MetricDataResults").get(0).get("Values").size() > 0,
|
||||
"an explicit JSON null Unit must not filter every datapoint out");
|
||||
}
|
||||
}
|
||||
+27
@@ -8,13 +8,18 @@ import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* The JSON 1.0 handler defaults {@code ActionsEnabled} to {@code true} when omitted, matching
|
||||
@@ -96,4 +101,26 @@ class CloudWatchMetricsQueryHandlerTest {
|
||||
verify(metricsService).putMetricAlarm(captor.capture(), anyString());
|
||||
assertEquals(2, captor.getValue().getDatapointsToAlarm());
|
||||
}
|
||||
|
||||
// The JSON handler forwards the Unit filter now; this pins the Query path to the same
|
||||
// contract so the two protocols cannot drift apart again.
|
||||
@Test
|
||||
void getMetricStatisticsForwardsTheUnitFilterToTheService() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("Namespace", "NS");
|
||||
params.putSingle("MetricName", "M");
|
||||
params.putSingle("Period", "60");
|
||||
params.putSingle("Statistics.member.1", "Sum");
|
||||
params.putSingle("Unit", "Count");
|
||||
|
||||
when(metricsService.getMetricStatistics(any(), any(), any(), any(), any(),
|
||||
anyInt(), any(), any(), any())).thenReturn(List.of());
|
||||
|
||||
handler.handle("GetMetricStatistics", params, REGION);
|
||||
|
||||
ArgumentCaptor<String> unitCaptor = ArgumentCaptor.forClass(String.class);
|
||||
verify(metricsService).getMetricStatistics(any(), any(), any(), any(), any(),
|
||||
anyInt(), any(), unitCaptor.capture(), any());
|
||||
assertEquals("Count", unitCaptor.getValue());
|
||||
}
|
||||
}
|
||||
+54
@@ -126,6 +126,60 @@ class CloudWatchMetricsServiceTest {
|
||||
|
||||
// ──────────────────────────── GetMetricStatistics ────────────────────────────
|
||||
|
||||
@Test
|
||||
void getMetricStatisticsFiltersByUnit() {
|
||||
MetricDatum count = datum("Requests", 10);
|
||||
count.setUnit("Count");
|
||||
MetricDatum bytes = datum("Requests", 20);
|
||||
bytes.setUnit("Bytes");
|
||||
service.putMetricData(NAMESPACE, List.of(count, bytes), REGION);
|
||||
|
||||
Instant start = Instant.now().minusSeconds(60);
|
||||
Instant end = Instant.now().plusSeconds(60);
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> matching = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), "Count", REGION);
|
||||
assertEquals(1, matching.size());
|
||||
assertEquals(10.0, matching.getFirst().sum());
|
||||
assertEquals("Count", matching.getFirst().unit());
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> otherUnit = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), "Bytes", REGION);
|
||||
assertEquals(1, otherUnit.size());
|
||||
assertEquals(20.0, otherUnit.getFirst().sum());
|
||||
assertEquals("Bytes", otherUnit.getFirst().unit());
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> noMatch = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), "Percent", REGION);
|
||||
assertTrue(noMatch.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricStatisticsTreatsNullOrNoneOrBlankUnitAsNoFilter() {
|
||||
MetricDatum count = datum("Requests", 10);
|
||||
count.setUnit("Count");
|
||||
MetricDatum bytes = datum("Requests", 20);
|
||||
bytes.setUnit("Bytes");
|
||||
service.putMetricData(NAMESPACE, List.of(count, bytes), REGION);
|
||||
|
||||
Instant start = Instant.now().minusSeconds(60);
|
||||
Instant end = Instant.now().plusSeconds(60);
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> allNull = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), null, REGION);
|
||||
assertEquals(1, allNull.size());
|
||||
assertEquals(30.0, allNull.getFirst().sum());
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> allNone = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), "None", REGION);
|
||||
assertEquals(1, allNone.size());
|
||||
assertEquals(30.0, allNone.getFirst().sum());
|
||||
|
||||
List<CloudWatchMetricsService.Datapoint> allBlank = service.getMetricStatistics(
|
||||
NAMESPACE, "Requests", null, start, end, 60, List.of("Sum"), " ", REGION);
|
||||
assertEquals(1, allBlank.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getMetricStatisticsBucketsByPeriod() {
|
||||
long now = Instant.now().getEpochSecond();
|
||||
|
||||
Reference in new issue
Block a user