use of com.amazonaws.services.cloudwatch.model.MetricStat in project aws-athena-query-federation by awslabs.
the class MetricUtilsTest method makeGetMetricDataRequest.
@Test
public void makeGetMetricDataRequest() {
String schema = "schema";
String table = "table";
Integer period = 60;
String statistic = "p90";
String metricName = "metricName";
String namespace = "namespace";
List<Dimension> dimensions = new ArrayList<>();
dimensions.add(new Dimension().withName("dim_name1").withValue("dim_value1"));
dimensions.add(new Dimension().withName("dim_name2").withValue("dim_value2"));
List<MetricStat> metricStats = new ArrayList<>();
metricStats.add(new MetricStat().withMetric(new Metric().withNamespace(namespace).withMetricName(metricName).withDimensions(dimensions)).withPeriod(60).withStat(statistic));
Split split = Split.newBuilder(null, null).add(NAMESPACE_FIELD, namespace).add(METRIC_NAME_FIELD, metricName).add(PERIOD_FIELD, String.valueOf(period)).add(STATISTIC_FIELD, statistic).add(SERIALIZED_METRIC_STATS_FIELD_NAME, MetricStatSerDe.serialize(metricStats)).build();
Schema schemaForRead = SchemaBuilder.newBuilder().addStringField(METRIC_NAME_FIELD).build();
Map<String, ValueSet> constraintsMap = new HashMap<>();
constraintsMap.put(TIMESTAMP_FIELD, SortedRangeSet.copyOf(Types.MinorType.BIGINT.getType(), ImmutableList.of(Range.greaterThan(allocator, Types.MinorType.BIGINT.getType(), 1L)), false));
ReadRecordsRequest request = new ReadRecordsRequest(identity, catalog, "queryId-" + System.currentTimeMillis(), new TableName(schema, table), schemaForRead, split, new Constraints(constraintsMap), // 100GB don't expect this to spill
100_000_000_000L, 100_000_000_000L);
GetMetricDataRequest actual = MetricUtils.makeGetMetricDataRequest(request);
assertEquals(1, actual.getMetricDataQueries().size());
assertNotNull(actual.getMetricDataQueries().get(0).getId());
MetricStat metricStat = actual.getMetricDataQueries().get(0).getMetricStat();
assertNotNull(metricStat);
assertEquals(metricName, metricStat.getMetric().getMetricName());
assertEquals(namespace, metricStat.getMetric().getNamespace());
assertEquals(statistic, metricStat.getStat());
assertEquals(period, metricStat.getPeriod());
assertEquals(2, metricStat.getMetric().getDimensions().size());
assertEquals(1000L, actual.getStartTime().getTime());
assertTrue(actual.getStartTime().getTime() <= System.currentTimeMillis() + 1_000);
}
use of com.amazonaws.services.cloudwatch.model.MetricStat in project aws-athena-query-federation by awslabs.
the class MetricUtils method makeGetMetricDataRequest.
/**
* Creates a Cloudwatch Metrics sample data request from the provided inputs
*
* @param readRecordsRequest The RecordReadRequest to make into a Cloudwatch Metrics Data request.
* @return The Cloudwatch Metrics Data request that matches the requested read operation.
*/
protected static GetMetricDataRequest makeGetMetricDataRequest(ReadRecordsRequest readRecordsRequest) {
Split split = readRecordsRequest.getSplit();
String serializedMetricStats = split.getProperty(MetricStatSerDe.SERIALIZED_METRIC_STATS_FIELD_NAME);
List<MetricStat> metricStats = MetricStatSerDe.deserialize(serializedMetricStats);
GetMetricDataRequest dataRequest = new GetMetricDataRequest();
com.amazonaws.services.cloudwatch.model.Metric metric = new com.amazonaws.services.cloudwatch.model.Metric();
metric.setNamespace(split.getProperty(NAMESPACE_FIELD));
metric.setMetricName(split.getProperty(METRIC_NAME_FIELD));
List<MetricDataQuery> metricDataQueries = new ArrayList<>();
int metricId = 1;
for (MetricStat nextMetricStat : metricStats) {
metricDataQueries.add(new MetricDataQuery().withMetricStat(nextMetricStat).withId("m" + metricId++));
}
dataRequest.withMetricDataQueries(metricDataQueries);
ValueSet timeConstraint = readRecordsRequest.getConstraints().getSummary().get(TIMESTAMP_FIELD);
if (timeConstraint instanceof SortedRangeSet && !timeConstraint.isNullAllowed()) {
// SortedRangeSet is how >, <, between is represented which are easiest and most common when
// searching logs so we attempt to push that down here as an optimization. SQL can represent complex
// overlapping ranges which Cloudwatch can not support so this is not a replacement for applying
// constraints using the ConstraintEvaluator.
Range basicPredicate = ((SortedRangeSet) timeConstraint).getSpan();
if (!basicPredicate.getLow().isNullValue()) {
Long lowerBound = (Long) basicPredicate.getLow().getValue();
// TODO: confirm timezone handling
logger.info("makeGetMetricsRequest: with startTime " + (lowerBound * 1000) + " " + new Date(lowerBound * 1000));
dataRequest.withStartTime(new Date(lowerBound * 1000));
} else {
// TODO: confirm timezone handling
dataRequest.withStartTime(new Date(0));
}
if (!basicPredicate.getHigh().isNullValue()) {
Long upperBound = (Long) basicPredicate.getHigh().getValue();
// TODO: confirm timezone handling
logger.info("makeGetMetricsRequest: with endTime " + (upperBound * 1000) + " " + new Date(upperBound * 1000));
dataRequest.withEndTime(new Date(upperBound * 1000));
} else {
// TODO: confirm timezone handling
dataRequest.withEndTime(new Date(System.currentTimeMillis()));
}
} else {
// TODO: confirm timezone handling
dataRequest.withStartTime(new Date(0));
dataRequest.withEndTime(new Date(System.currentTimeMillis()));
}
return dataRequest;
}
Aggregations