mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(cloudwatch): implement GetMetricData (#451)
Replaces the stubs in both the query and JSON protocol handlers with a full implementation that delegates to the existing getMetricStatistics storage and aggregation logic. - Add MetricStat, MetricDataQuery, and MetricDataResult records to CloudWatchMetricsService - getMetricData() iterates queries, buckets by period, resolves the requested statistic (Average/Sum/Min/Max/SampleCount/p*), and returns Complete results - Query handler: parseMetricDataQueries walks the deeply nested MetricDataQueries.member.N.* form encoding - JSON handler: parseInstantNode handles numeric epoch timestamps sent by AWS SDK v2 (GetMetricData uses the JSON protocol); previously the time filter silently failed, returning all stored data
This commit is contained in:
1 parent
bf67c61d60
commit
79ce5220bf
4 files changed
+473
-5
No files matched your search
+67
-1
@@ -47,7 +47,7 @@ public class CloudWatchMetricsJsonHandler {
|
||||
case "ListTagsForResource" -> handleListTagsForResource(request, region);
|
||||
case "TagResource" -> handleTagResource(request, region);
|
||||
case "UntagResource" -> handleUntagResource(request, region);
|
||||
case "GetMetricData" -> Response.ok(objectMapper.createObjectNode().putArray("MetricDataResults")).build();
|
||||
case "GetMetricData" -> handleGetMetricData(request, region);
|
||||
default -> Response.status(400)
|
||||
.entity(new AwsErrorResponse("UnsupportedOperation", "Operation " + action + " is not supported by CloudWatch JSON."))
|
||||
.build();
|
||||
@@ -241,6 +241,63 @@ public class CloudWatchMetricsJsonHandler {
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleGetMetricData(JsonNode request, String region) {
|
||||
Instant startTime = parseInstantNode(request.path("StartTime"));
|
||||
Instant endTime = parseInstantNode(request.path("EndTime"));
|
||||
|
||||
List<CloudWatchMetricsService.MetricDataQuery> queries = new ArrayList<>();
|
||||
JsonNode queriesNode = request.path("MetricDataQueries");
|
||||
if (queriesNode.isArray()) {
|
||||
for (JsonNode qNode : queriesNode) {
|
||||
String id = qNode.path("Id").asText();
|
||||
String expression = qNode.has("Expression") ? qNode.path("Expression").asText() : null;
|
||||
String label = qNode.has("Label") ? qNode.path("Label").asText() : null;
|
||||
boolean returnData = qNode.path("ReturnData").asBoolean(true);
|
||||
|
||||
CloudWatchMetricsService.MetricStat metricStat = null;
|
||||
JsonNode msNode = qNode.path("MetricStat");
|
||||
if (!msNode.isMissingNode()) {
|
||||
JsonNode metricNode = msNode.path("Metric");
|
||||
String namespace = metricNode.path("Namespace").asText();
|
||||
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;
|
||||
List<Dimension> dims = parseDimensionsJson(metricNode.path("Dimensions"));
|
||||
|
||||
metricStat = new CloudWatchMetricsService.MetricStat(
|
||||
namespace, metricName, dims, period, stat, unit);
|
||||
}
|
||||
|
||||
queries.add(new CloudWatchMetricsService.MetricDataQuery(
|
||||
id, metricStat, expression, label, returnData));
|
||||
}
|
||||
}
|
||||
|
||||
List<CloudWatchMetricsService.MetricDataResult> results =
|
||||
metricsService.getMetricData(queries, startTime, endTime, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode resultsArray = response.putArray("MetricDataResults");
|
||||
for (var r : results) {
|
||||
ObjectNode rNode = resultsArray.addObject();
|
||||
rNode.put("Id", r.id());
|
||||
rNode.put("Label", r.label());
|
||||
rNode.put("StatusCode", r.statusCode());
|
||||
|
||||
ArrayNode tsArray = rNode.putArray("Timestamps");
|
||||
for (Instant ts : r.timestamps()) {
|
||||
tsArray.add(ts.getEpochSecond());
|
||||
}
|
||||
|
||||
ArrayNode valArray = rNode.putArray("Values");
|
||||
for (Double v : r.values()) {
|
||||
valArray.add(v);
|
||||
}
|
||||
}
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private List<MetricDatum> parseMetricDataJson(JsonNode node) {
|
||||
List<MetricDatum> datums = new ArrayList<>();
|
||||
if (!node.isArray()) return datums;
|
||||
@@ -278,6 +335,15 @@ public class CloudWatchMetricsJsonHandler {
|
||||
return dims;
|
||||
}
|
||||
|
||||
/** Parse a JsonNode that may be a numeric epoch (long/double) or an ISO-8601 string. */
|
||||
private Instant parseInstantNode(JsonNode node) {
|
||||
if (node == null || node.isMissingNode() || node.isNull()) return null;
|
||||
if (node.isNumber()) {
|
||||
return Instant.ofEpochSecond(node.asLong());
|
||||
}
|
||||
return parseInstant(node.asText(null));
|
||||
}
|
||||
|
||||
private Instant parseInstant(String value) {
|
||||
if (value == null || value.isBlank()) return null;
|
||||
try {
|
||||
|
||||
+74
-4
@@ -37,7 +37,7 @@ public class CloudWatchMetricsQueryHandler {
|
||||
case "PutMetricData" -> handlePutMetricData(params, region);
|
||||
case "ListMetrics" -> handleListMetrics(params, region);
|
||||
case "GetMetricStatistics" -> handleGetMetricStatistics(params, region);
|
||||
case "GetMetricData" -> handleGetMetricDataStub(params);
|
||||
case "GetMetricData" -> handleGetMetricData(params, region);
|
||||
case "PutMetricAlarm" -> handlePutMetricAlarm(params, region);
|
||||
case "DescribeAlarms" -> handleDescribeAlarms(params, region);
|
||||
case "DeleteAlarms" -> handleDeleteAlarms(params, region);
|
||||
@@ -133,9 +133,79 @@ public class CloudWatchMetricsQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelope("GetMetricStatistics", null, xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleGetMetricDataStub(MultivaluedMap<String, String> params) {
|
||||
String result = new XmlBuilder().start("MetricDataResults").end("MetricDataResults").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetMetricData", null, result)).build();
|
||||
private Response handleGetMetricData(MultivaluedMap<String, String> params, String region) {
|
||||
Instant startTime = parseInstant(params.getFirst("StartTime"));
|
||||
Instant endTime = parseInstant(params.getFirst("EndTime"));
|
||||
|
||||
List<CloudWatchMetricsService.MetricDataQuery> queries = parseMetricDataQueries(params);
|
||||
|
||||
List<CloudWatchMetricsService.MetricDataResult> results =
|
||||
metricsService.getMetricData(queries, startTime, endTime, region);
|
||||
|
||||
DateTimeFormatter fmt = DateTimeFormatter.ISO_INSTANT;
|
||||
var xml = new XmlBuilder().start("MetricDataResults");
|
||||
for (var r : results) {
|
||||
xml.start("member")
|
||||
.elem("Id", r.id())
|
||||
.elem("Label", r.label())
|
||||
.elem("StatusCode", r.statusCode());
|
||||
|
||||
xml.start("Timestamps");
|
||||
for (Instant ts : r.timestamps()) {
|
||||
xml.elem("member", fmt.format(ts));
|
||||
}
|
||||
xml.end("Timestamps");
|
||||
|
||||
xml.start("Values");
|
||||
for (Double v : r.values()) {
|
||||
xml.elem("member", String.valueOf(v));
|
||||
}
|
||||
xml.end("Values");
|
||||
|
||||
xml.end("member");
|
||||
}
|
||||
xml.end("MetricDataResults");
|
||||
return Response.ok(AwsQueryResponse.envelope("GetMetricData", null, xml.build())).build();
|
||||
}
|
||||
|
||||
private List<CloudWatchMetricsService.MetricDataQuery> parseMetricDataQueries(
|
||||
MultivaluedMap<String, String> params) {
|
||||
List<CloudWatchMetricsService.MetricDataQuery> queries = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String prefix = "MetricDataQueries.member." + i;
|
||||
String id = params.getFirst(prefix + ".Id");
|
||||
if (id == null) break;
|
||||
|
||||
String expression = params.getFirst(prefix + ".Expression");
|
||||
String label = params.getFirst(prefix + ".Label");
|
||||
boolean returnData = !"false".equals(params.getFirst(prefix + ".ReturnData"));
|
||||
|
||||
CloudWatchMetricsService.MetricStat metricStat = null;
|
||||
String msNamespace = params.getFirst(prefix + ".MetricStat.Metric.Namespace");
|
||||
if (msNamespace != null) {
|
||||
String msMetricName = params.getFirst(prefix + ".MetricStat.Metric.MetricName");
|
||||
int msPeriod = parseIntParam(params, prefix + ".MetricStat.Period", 60);
|
||||
String msStat = params.getFirst(prefix + ".MetricStat.Stat");
|
||||
String msUnit = params.getFirst(prefix + ".MetricStat.Unit");
|
||||
|
||||
List<Dimension> dims = new ArrayList<>();
|
||||
for (int j = 1; ; j++) {
|
||||
String dimName = params.getFirst(
|
||||
prefix + ".MetricStat.Metric.Dimensions.member." + j + ".Name");
|
||||
if (dimName == null) break;
|
||||
String dimValue = params.getFirst(
|
||||
prefix + ".MetricStat.Metric.Dimensions.member." + j + ".Value");
|
||||
dims.add(new Dimension(dimName, dimValue));
|
||||
}
|
||||
|
||||
metricStat = new CloudWatchMetricsService.MetricStat(
|
||||
msNamespace, msMetricName, dims, msPeriod, msStat, msUnit);
|
||||
}
|
||||
|
||||
queries.add(new CloudWatchMetricsService.MetricDataQuery(
|
||||
id, metricStat, expression, label, returnData));
|
||||
}
|
||||
return queries;
|
||||
}
|
||||
|
||||
private Response handlePutMetricAlarm(MultivaluedMap<String, String> params, String region) {
|
||||
|
||||
+75
@@ -100,6 +100,31 @@ public class CloudWatchMetricsService {
|
||||
public record Datapoint(Instant timestamp, double sampleCount, double sum,
|
||||
double average, double minimum, double maximum, String unit) {}
|
||||
|
||||
public record MetricStat(
|
||||
String namespace,
|
||||
String metricName,
|
||||
List<Dimension> dimensions,
|
||||
int period,
|
||||
String stat,
|
||||
String unit
|
||||
) {}
|
||||
|
||||
public record MetricDataQuery(
|
||||
String id,
|
||||
MetricStat metricStat,
|
||||
String expression,
|
||||
String label,
|
||||
boolean returnData
|
||||
) {}
|
||||
|
||||
public record MetricDataResult(
|
||||
String id,
|
||||
String label,
|
||||
List<Instant> timestamps,
|
||||
List<Double> values,
|
||||
String statusCode
|
||||
) {}
|
||||
|
||||
public List<Datapoint> getMetricStatistics(String namespace, String metricName,
|
||||
List<Dimension> dimensions,
|
||||
Instant startTime, Instant endTime,
|
||||
@@ -159,6 +184,56 @@ public class CloudWatchMetricsService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public List<MetricDataResult> getMetricData(
|
||||
List<MetricDataQuery> queries,
|
||||
Instant startTime,
|
||||
Instant endTime,
|
||||
String region) {
|
||||
|
||||
List<MetricDataResult> results = new ArrayList<>();
|
||||
|
||||
for (MetricDataQuery query : queries) {
|
||||
if (!query.returnData()) {
|
||||
continue;
|
||||
}
|
||||
if (query.metricStat() != null) {
|
||||
MetricStat stat = query.metricStat();
|
||||
int period = stat.period() > 0 ? stat.period() : 60;
|
||||
|
||||
List<Datapoint> datapoints = getMetricStatistics(
|
||||
stat.namespace(), stat.metricName(), stat.dimensions(),
|
||||
startTime, endTime, period,
|
||||
List.of(stat.stat()), stat.unit(), region);
|
||||
|
||||
List<Instant> timestamps = new ArrayList<>();
|
||||
List<Double> values = new ArrayList<>();
|
||||
for (Datapoint dp : datapoints) {
|
||||
timestamps.add(dp.timestamp());
|
||||
values.add(resolveStatValue(dp, stat.stat()));
|
||||
}
|
||||
|
||||
String label = query.label() != null ? query.label() : stat.metricName();
|
||||
results.add(new MetricDataResult(query.id(), label, timestamps, values, "Complete"));
|
||||
}
|
||||
// Expression-based queries are out of scope for this implementation
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private double resolveStatValue(Datapoint dp, String stat) {
|
||||
return switch (stat) {
|
||||
case "Average" -> dp.average();
|
||||
case "Sum" -> dp.sum();
|
||||
case "Minimum" -> dp.minimum();
|
||||
case "Maximum" -> dp.maximum();
|
||||
case "SampleCount" -> dp.sampleCount();
|
||||
default -> {
|
||||
if (stat.startsWith("p")) yield dp.maximum();
|
||||
else yield dp.average();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public void putMetricAlarm(MetricAlarm alarm, String region) {
|
||||
if (alarm.getAlarmArn() == null) {
|
||||
alarm.setAlarmArn(regionResolver.buildArn("cloudwatch", region, "alarm:" + alarm.getAlarmName()));
|
||||
|
||||
Reference in new issue
Block a user