mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudwatch): preserve weighted samples on public metric APIs (#8974)
* fix(cloudwatch): preserve weighted samples on public metric APIs * fix(cloudwatch): index Query fields before weighted parsing * fix(cloudwatch): include datum paths in weighted length errors Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
9795119ce0
commit
d8a638e4af
10 files changed
+470
-13
No files matched your search
+2
-2
@@ -128,7 +128,7 @@ public class CloudWatchMetricsJsonHandler {
|
||||
* to a value other than {@code null} or {@code false}, is refused rather than answered as if it
|
||||
* were unset, and a top-level {@code null} member is dropped. {@code PutMetricData} also checks
|
||||
* the complete batch for supported scalar or statistic-set shapes before storing any metrics;
|
||||
* {@code Values} and {@code Counts} are refused because this handler does not parse them.
|
||||
* {@code Values} and {@code Counts} remain outside this compatibility subset.
|
||||
* Other operations pass nested members on unchecked, nulls included. A {@code DescribeAlarms} answer that would list
|
||||
* an alarm defined with {@code Metrics} is refused too, since the console's API gives those
|
||||
* items its own shape, with {@code Alias} instead of {@code Id} and {@code MetricStat}. The
|
||||
@@ -964,7 +964,7 @@ public class CloudWatchMetricsJsonHandler {
|
||||
datum.setMaximum(MetricNumberValidation.parse(statsValues.path("Maximum"), statsPath + ".Maximum"));
|
||||
}
|
||||
|
||||
datums.add(datum);
|
||||
datums.addAll(MetricValues.fromJson(item, memberPath, datum));
|
||||
}
|
||||
return datums;
|
||||
}
|
||||
|
||||
+10
-4
@@ -710,6 +710,7 @@ public class CloudWatchMetricsQueryHandler {
|
||||
|
||||
private List<MetricDatum> parseMetricData(MultivaluedMap<String, String> params) {
|
||||
Map<String, SortedSet<Integer>> memberIndexes = new HashMap<>();
|
||||
Map<String, Map<String, List<String>>> fieldsByDatum = new HashMap<>();
|
||||
Set<String> statisticPrefixes = new HashSet<>();
|
||||
// An empty EntityMetricData entry serializes to no keys, so its index is absent
|
||||
// while later entries keep theirs: iterate the indexes present, not 1..first gap.
|
||||
@@ -720,6 +721,7 @@ public class CloudWatchMetricsQueryHandler {
|
||||
String prefix = member.group();
|
||||
memberIndexes.computeIfAbsent(member.group(1), ignored -> new TreeSet<>())
|
||||
.add(parseMetricMemberIndex(member.group(2), prefix));
|
||||
fieldsByDatum.computeIfAbsent(prefix, ignored -> new HashMap<>()).put(key, params.get(key));
|
||||
if (key.startsWith(prefix + "StatisticValues.")) {
|
||||
statisticPrefixes.add(prefix);
|
||||
}
|
||||
@@ -730,11 +732,13 @@ public class CloudWatchMetricsQueryHandler {
|
||||
}
|
||||
}
|
||||
List<MetricDatum> datums = parseMetricData(params, "MetricData",
|
||||
memberIndexes.getOrDefault("MetricData", Collections.emptySortedSet()), statisticPrefixes);
|
||||
memberIndexes.getOrDefault("MetricData", Collections.emptySortedSet()),
|
||||
fieldsByDatum, statisticPrefixes);
|
||||
for (int index : entityIndexes) {
|
||||
String listPrefix = "EntityMetricData.member." + index + ".MetricData";
|
||||
datums.addAll(parseMetricData(params, listPrefix,
|
||||
memberIndexes.getOrDefault(listPrefix, Collections.emptySortedSet()), statisticPrefixes));
|
||||
memberIndexes.getOrDefault(listPrefix, Collections.emptySortedSet()),
|
||||
fieldsByDatum, statisticPrefixes));
|
||||
}
|
||||
LOG.debugv("Query PutMetricData parsed {0} datums", datums.size());
|
||||
return datums;
|
||||
@@ -749,7 +753,9 @@ public class CloudWatchMetricsQueryHandler {
|
||||
}
|
||||
|
||||
private List<MetricDatum> parseMetricData(MultivaluedMap<String, String> params, String listPrefix,
|
||||
SortedSet<Integer> memberIndexes, Set<String> statisticPrefixes) {
|
||||
SortedSet<Integer> memberIndexes,
|
||||
Map<String, Map<String, List<String>>> fieldsByDatum,
|
||||
Set<String> statisticPrefixes) {
|
||||
List<MetricDatum> datums = new ArrayList<>();
|
||||
for (int i : memberIndexes) {
|
||||
String memberPrefix = listPrefix + ".member." + i + ".";
|
||||
@@ -797,7 +803,7 @@ public class CloudWatchMetricsQueryHandler {
|
||||
}
|
||||
datum.setDimensions(dims);
|
||||
|
||||
datums.add(datum);
|
||||
datums.addAll(MetricValues.fromQuery(fieldsByDatum.get(memberPrefix), memberPrefix, datum));
|
||||
}
|
||||
return datums;
|
||||
}
|
||||
|
||||
+1
-1
@@ -150,7 +150,7 @@ public class CloudWatchMetricsService {
|
||||
datum.setTimestamp(nowSeconds);
|
||||
}
|
||||
// Synthesize StatisticValues if only a scalar value was provided
|
||||
if (datum.getSampleCount() == 0 && datum.getSum() == 0) {
|
||||
if (!datum.hasSampleCount() && datum.getSum() == 0) {
|
||||
datum.setSampleCount(1);
|
||||
datum.setSum(datum.getValue());
|
||||
datum.setMinimum(datum.getValue());
|
||||
|
||||
@@ -41,7 +41,7 @@ final class MetricNumberValidation {
|
||||
return Double.isFinite(value) && Math.abs(value) <= MAX_ABSOLUTE_VALUE;
|
||||
}
|
||||
|
||||
private static double finite(double value, String path) {
|
||||
static double finite(double value, String path) {
|
||||
if (!isValid(value)) {
|
||||
throw invalid(path);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package io.floci.aws.services.cloudwatch.metrics;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.services.cloudwatch.metrics.model.MetricDatum;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
/** Normalize each distinct value to a recoverable statistic set without expanding its count. */
|
||||
final class MetricValues {
|
||||
private static final int MAX_VALUES = 150;
|
||||
|
||||
private MetricValues() {
|
||||
}
|
||||
|
||||
static List<MetricDatum> fromJson(JsonNode input, String memberPath, MetricDatum datum) {
|
||||
List<Double> values = jsonNumbers(input.get("Values"), "Values");
|
||||
List<Double> counts = jsonNumbers(input.get("Counts"), "Counts");
|
||||
return normalize(datum, memberPath + ".", values, counts,
|
||||
input.hasNonNull("Value") || input.hasNonNull("StatisticValues"));
|
||||
}
|
||||
|
||||
/** Parse fields indexed for one datum, so a batch never rescans the entire request per sample. */
|
||||
static List<MetricDatum> fromQuery(Map<String, List<String>> input, String prefix, MetricDatum datum) {
|
||||
List<Double> values = queryNumbers(input, prefix + "Values");
|
||||
List<Double> counts = queryNumbers(input, prefix + "Counts");
|
||||
boolean otherFormat = input.containsKey(prefix + "Value")
|
||||
|| input.keySet().stream().anyMatch(key -> key.startsWith(prefix + "StatisticValues."));
|
||||
return normalize(datum, prefix, values, counts, otherFormat);
|
||||
}
|
||||
|
||||
private static List<Double> jsonNumbers(JsonNode input, String field) {
|
||||
if (input == null || input.isNull()) {
|
||||
return null;
|
||||
}
|
||||
if (!input.isArray() || input.isEmpty() || input.size() > MAX_VALUES) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue(field + " must contain 1 to 150 numbers.");
|
||||
}
|
||||
List<Double> numbers = new ArrayList<>(input.size());
|
||||
for (JsonNode value : input) {
|
||||
if (!value.isNumber()) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue(field + " must contain numbers.");
|
||||
}
|
||||
numbers.add(value.doubleValue());
|
||||
}
|
||||
return numbers;
|
||||
}
|
||||
|
||||
private static List<Double> queryNumbers(Map<String, List<String>> input, String field) {
|
||||
TreeMap<Integer, Double> numbers = new TreeMap<>();
|
||||
for (Map.Entry<String, List<String>> entry : input.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (!key.equals(field) && !key.startsWith(field + ".")) {
|
||||
continue;
|
||||
}
|
||||
String prefix = field + ".member.";
|
||||
try {
|
||||
if (!key.startsWith(prefix) || entry.getValue().size() != 1) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
int index = Integer.parseInt(key.substring(prefix.length()));
|
||||
if (index < 1 || index > MAX_VALUES) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
double value = Double.parseDouble(entry.getValue().getFirst());
|
||||
Double previous = numbers.putIfAbsent(index, value);
|
||||
if (previous != null) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
} catch (IllegalArgumentException error) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue(
|
||||
field + " must contain 1 to 150 consecutively indexed numbers.");
|
||||
}
|
||||
}
|
||||
if (numbers.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
if (numbers.lastKey() != numbers.size()) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue(field + " has a missing member.");
|
||||
}
|
||||
return new ArrayList<>(numbers.values());
|
||||
}
|
||||
|
||||
private static List<MetricDatum> normalize(MetricDatum template, String memberPrefix, List<Double> values,
|
||||
List<Double> counts, boolean otherFormat) {
|
||||
if (values == null) {
|
||||
if (counts != null) {
|
||||
throw CloudWatchMetricsException.invalidParameterCombination("Counts requires Values.");
|
||||
}
|
||||
return List.of(template);
|
||||
}
|
||||
if (otherFormat) {
|
||||
throw CloudWatchMetricsException.invalidParameterCombination(
|
||||
"Values cannot be combined with Value or StatisticValues.");
|
||||
}
|
||||
if (counts != null && counts.size() != values.size()) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue("The parameters " + memberPrefix
|
||||
+ "Values and " + memberPrefix + "Counts must be of the same size.");
|
||||
}
|
||||
List<MetricDatum> result = new ArrayList<>(values.size());
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
double value = MetricNumberValidation.finite(values.get(i), "Values.member." + (i + 1));
|
||||
double count = counts == null ? 1 : counts.get(i);
|
||||
if (!Double.isFinite(count) || count < 0 || !Double.isFinite(value * count)) {
|
||||
throw CloudWatchMetricsException.invalidParameterValue(
|
||||
"Counts must be nonnegative finite numbers with a finite weighted sum.");
|
||||
}
|
||||
MetricDatum sample = MetricDatum.of(template.getMetricName(), template.getUnit(), value,
|
||||
List.copyOf(template.getDimensions()), template.getTimestamp());
|
||||
sample.setSampleCount(count);
|
||||
sample.setSum(value * count);
|
||||
sample.setMinimum(value);
|
||||
sample.setMaximum(value);
|
||||
result.add(sample);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package io.floci.aws.services.cloudwatch.metrics.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@@ -16,6 +17,8 @@ public class MetricDatum {
|
||||
private long timestamp;
|
||||
private double value;
|
||||
private double sampleCount;
|
||||
@JsonIgnore
|
||||
private boolean sampleCountSet;
|
||||
private double sum;
|
||||
private double minimum;
|
||||
private double maximum;
|
||||
@@ -54,7 +57,13 @@ public class MetricDatum {
|
||||
public void setValue(double value) { this.value = value; }
|
||||
|
||||
public double getSampleCount() { return sampleCount; }
|
||||
public void setSampleCount(double sampleCount) { this.sampleCount = sampleCount; }
|
||||
public void setSampleCount(double sampleCount) {
|
||||
this.sampleCount = sampleCount;
|
||||
this.sampleCountSet = true;
|
||||
}
|
||||
|
||||
@JsonIgnore
|
||||
public boolean hasSampleCount() { return sampleCountSet; }
|
||||
|
||||
public double getSum() { return sum; }
|
||||
public void setSum(double sum) { this.sum = sum; }
|
||||
|
||||
Reference in new issue
Block a user