mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
fix(cloudwatch): paginate ListMetrics results (#7236)
This commit is contained in:
1 parent
32823e6341
commit
87936c19b3
6 files changed
+267
-12
No files matched your search
+16
-8
@@ -46,11 +46,11 @@ public class CloudWatchMetricsJsonHandler {
|
||||
|
||||
/**
|
||||
* The operations served through the console's {@code CloudWatchVersion20130116.} target, each
|
||||
* with the request members accepted there: some of the ones this handler honours, plus
|
||||
* {@code NextToken} and {@code MaxRecords}, which change nothing because every result comes
|
||||
* back at once. DescribeAlarms' {@code StateValue}, {@code ActionPrefix},
|
||||
* {@code ChildrenOfAlarmName} and {@code ParentsOfAlarmName} stay refused here until the
|
||||
* console's API is known to give them the same shapes.
|
||||
* with the request members accepted there: some of the ones this handler honours. ListMetrics
|
||||
* honours {@code NextToken}; DescribeAlarms' {@code MaxRecords} and {@code NextToken} still
|
||||
* change nothing because every alarm comes back at once. DescribeAlarms' {@code StateValue},
|
||||
* {@code ActionPrefix}, {@code ChildrenOfAlarmName} and {@code ParentsOfAlarmName}
|
||||
* stay refused here until the console's API is known to give them the same shapes.
|
||||
*/
|
||||
private static final Map<String, Set<String>> CONSOLE_TARGET_OPERATIONS = Map.of(
|
||||
"ListMetrics", Set.of("Namespace", "MetricName", "Dimensions", "NextToken"),
|
||||
@@ -177,12 +177,17 @@ public class CloudWatchMetricsJsonHandler {
|
||||
String metricName = request.has("MetricName") ? request.path("MetricName").asText() : null;
|
||||
List<Dimension> dimensions = parseDimensionsJson(request.path("Dimensions"));
|
||||
|
||||
List<CloudWatchMetricsService.MetricIdentity> metrics =
|
||||
metricsService.listMetrics(namespace, metricName, dimensions, region);
|
||||
JsonNode tokenNode = request.get("NextToken");
|
||||
if (tokenNode != null && !tokenNode.isNull() && !tokenNode.isTextual()) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue("Invalid NextToken.");
|
||||
}
|
||||
String nextToken = tokenNode == null || tokenNode.isNull() ? null : tokenNode.textValue();
|
||||
PaginatedResult<CloudWatchMetricsService.MetricIdentity> page =
|
||||
metricsService.listMetricsPage(namespace, metricName, dimensions, nextToken, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode metricsArray = response.putArray("Metrics");
|
||||
for (CloudWatchMetricsService.MetricIdentity m : metrics) {
|
||||
for (CloudWatchMetricsService.MetricIdentity m : page.items()) {
|
||||
ObjectNode mNode = metricsArray.addObject();
|
||||
mNode.put("Namespace", m.namespace());
|
||||
mNode.put("MetricName", m.metricName());
|
||||
@@ -193,6 +198,9 @@ public class CloudWatchMetricsJsonHandler {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (page.nextToken() != null) {
|
||||
response.put("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
|
||||
+6
-3
@@ -98,11 +98,11 @@ public class CloudWatchMetricsQueryHandler {
|
||||
String metricName = params.getFirst("MetricName");
|
||||
List<Dimension> dimensions = parseDimensionFilters(params);
|
||||
|
||||
List<CloudWatchMetricsService.MetricIdentity> metrics =
|
||||
metricsService.listMetrics(namespace, metricName, dimensions, region);
|
||||
PaginatedResult<CloudWatchMetricsService.MetricIdentity> page =
|
||||
metricsService.listMetricsPage(namespace, metricName, dimensions, params.getFirst("NextToken"), region);
|
||||
|
||||
XmlBuilder xml = new XmlBuilder().start("Metrics");
|
||||
for (CloudWatchMetricsService.MetricIdentity m : metrics) {
|
||||
for (CloudWatchMetricsService.MetricIdentity m : page.items()) {
|
||||
XmlBuilder member = xml.start("member")
|
||||
.elem("Namespace", m.namespace())
|
||||
.elem("MetricName", m.metricName())
|
||||
@@ -116,6 +116,9 @@ public class CloudWatchMetricsQueryHandler {
|
||||
xml.end("Dimensions").end("member");
|
||||
}
|
||||
xml.end("Metrics");
|
||||
if (page.nextToken() != null) {
|
||||
xml.elem("NextToken", page.nextToken());
|
||||
}
|
||||
return Response.ok(AwsQueryResponse.envelope("ListMetrics", null, xml.build())).build();
|
||||
}
|
||||
|
||||
|
||||
+31
@@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.Digests;
|
||||
import io.github.hectorvent.floci.core.common.PaginatedResult;
|
||||
import io.github.hectorvent.floci.core.common.Pagination;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
@@ -75,6 +76,7 @@ public class CloudWatchMetricsService {
|
||||
.ofPattern("EEEE dd MMMM, yyyy HH:mm:ss 'UTC'", Locale.ENGLISH).withZone(ZoneOffset.UTC);
|
||||
/** The most StateReasonData can hold, in Unicode code points as SetAlarmState's length check counts them. */
|
||||
private static final int MAX_STATE_REASON_DATA = 4000;
|
||||
private static final int LIST_METRICS_PAGE_SIZE = 500;
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper()
|
||||
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
|
||||
.enable(DeserializationFeature.FAIL_ON_TRAILING_TOKENS);
|
||||
@@ -243,6 +245,35 @@ public class CloudWatchMetricsService {
|
||||
return new ArrayList<>(deduped.values());
|
||||
}
|
||||
|
||||
public PaginatedResult<MetricIdentity> listMetricsPage(String namespace, String metricName,
|
||||
List<Dimension> dimensions, String nextToken,
|
||||
String region) {
|
||||
String accountId = metricStore instanceof AccountAwareStorageBackend<?> aware
|
||||
? aware.accountId() : regionResolver.getAccountId();
|
||||
ArrayNode scope = MAPPER.createArrayNode();
|
||||
scope.add(accountId).add(region).add(namespace).add(metricName);
|
||||
appendMetricDimensions(scope, dimensions);
|
||||
return Pagination.paginate(listMetrics(namespace, metricName, dimensions, region),
|
||||
CloudWatchMetricsService::metricCursor, LIST_METRICS_PAGE_SIZE, nextToken,
|
||||
"cloudwatch-list-metrics-" + Digests.sha256Hex(scope.toString()),
|
||||
token -> CloudWatchMetricsException.invalidParameterValue("Invalid NextToken."));
|
||||
}
|
||||
|
||||
static String metricCursor(MetricIdentity metric) {
|
||||
String identity = metric.namespace() + "::" + metric.metricName() + "::" + buildDimKey(metric.dimensions());
|
||||
return Digests.sha256Hex(identity);
|
||||
}
|
||||
|
||||
private static void appendMetricDimensions(ArrayNode key, List<Dimension> dimensions) {
|
||||
if (dimensions == null) {
|
||||
return;
|
||||
}
|
||||
dimensions.stream().sorted(Comparator.comparing(Dimension::name,
|
||||
Comparator.nullsFirst(String::compareTo))
|
||||
.thenComparing(Dimension::value, Comparator.nullsFirst(String::compareTo)))
|
||||
.forEach(dimension -> key.addArray().add(dimension.name()).add(dimension.value()));
|
||||
}
|
||||
|
||||
public record Datapoint(Instant timestamp, double sampleCount, double sum,
|
||||
double average, double minimum, double maximum, String unit) {}
|
||||
|
||||
|
||||
Reference in new issue
Block a user