Search in sources :

Example 1 with GetMetricDataRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricDataRequest in project aws-athena-query-federation by awslabs.

the class MetricsRecordHandlerTest method mockMetricData.

private GetMetricDataResult mockMetricData(InvocationOnMock invocation, int numMetrics, int numSamples) {
    GetMetricDataRequest request = invocation.getArgumentAt(0, GetMetricDataRequest.class);
    /**
     * Confirm that all available criteria were pushed down into Cloudwatch Metrics
     */
    List<MetricDataQuery> queries = request.getMetricDataQueries();
    assertEquals(1, queries.size());
    MetricDataQuery query = queries.get(0);
    MetricStat stat = query.getMetricStat();
    assertEquals("m1", query.getId());
    assertNotNull(stat.getPeriod());
    assertNotNull(stat.getMetric());
    assertNotNull(stat.getStat());
    assertNotNull(stat.getMetric().getMetricName());
    assertNotNull(stat.getMetric().getNamespace());
    assertNotNull(stat.getMetric().getDimensions());
    assertEquals(1, stat.getMetric().getDimensions().size());
    String nextToken = (request.getNextToken() == null) ? "valid" : null;
    List<MetricDataResult> samples = new ArrayList<>();
    for (int i = 0; i < numMetrics; i++) {
        List<Double> values = new ArrayList<>();
        List<Date> timestamps = new ArrayList<>();
        for (double j = 0; j < numSamples; j++) {
            values.add(j);
            timestamps.add(new Date(System.currentTimeMillis() + (int) j));
        }
        samples.add(new MetricDataResult().withValues(values).withTimestamps(timestamps).withId("m1"));
    }
    return new GetMetricDataResult().withNextToken(nextToken).withMetricDataResults(samples);
}
Also used : MetricStat(com.amazonaws.services.cloudwatch.model.MetricStat) ArrayList(java.util.ArrayList) GetMetricDataResult(com.amazonaws.services.cloudwatch.model.GetMetricDataResult) MetricDataResult(com.amazonaws.services.cloudwatch.model.MetricDataResult) Matchers.anyString(org.mockito.Matchers.anyString) GetMetricDataResult(com.amazonaws.services.cloudwatch.model.GetMetricDataResult) Date(java.util.Date) GetMetricDataRequest(com.amazonaws.services.cloudwatch.model.GetMetricDataRequest) MetricDataQuery(com.amazonaws.services.cloudwatch.model.MetricDataQuery)

Example 2 with GetMetricDataRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricDataRequest in project aws-athena-query-federation by awslabs.

the class MetricsRecordHandler method readMetricSamplesWithConstraint.

/**
 * Handles retrieving the samples for a specific metric from Cloudwatch Metrics.
 */
private void readMetricSamplesWithConstraint(BlockSpiller blockSpiller, ReadRecordsRequest request, QueryStatusChecker queryStatusChecker) throws TimeoutException {
    GetMetricDataRequest dataRequest = MetricUtils.makeGetMetricDataRequest(request);
    Map<String, MetricDataQuery> queries = new HashMap<>();
    for (MetricDataQuery query : dataRequest.getMetricDataQueries()) {
        queries.put(query.getId(), query);
    }
    String prevToken;
    ValueSet dimensionNameConstraint = request.getConstraints().getSummary().get(DIMENSION_NAME_FIELD);
    ValueSet dimensionValueConstraint = request.getConstraints().getSummary().get(DIMENSION_VALUE_FIELD);
    do {
        prevToken = dataRequest.getNextToken();
        GetMetricDataResult result = invoker.invoke(() -> metrics.getMetricData(dataRequest));
        for (MetricDataResult nextMetric : result.getMetricDataResults()) {
            MetricStat metricStat = queries.get(nextMetric.getId()).getMetricStat();
            List<Date> timestamps = nextMetric.getTimestamps();
            List<Double> values = nextMetric.getValues();
            for (int i = 0; i < nextMetric.getValues().size(); i++) {
                int sampleNum = i;
                blockSpiller.writeRows((Block block, int row) -> {
                    /**
                     * Most constraints were already applied at split generation so we only need to apply
                     * a subset.
                     */
                    block.offerValue(METRIC_NAME_FIELD, row, metricStat.getMetric().getMetricName());
                    block.offerValue(NAMESPACE_FIELD, row, metricStat.getMetric().getNamespace());
                    block.offerValue(STATISTIC_FIELD, row, metricStat.getStat());
                    block.offerComplexValue(DIMENSIONS_FIELD, row, (Field field, Object val) -> {
                        if (field.getName().equals(DIMENSION_NAME_FIELD)) {
                            return ((Dimension) val).getName();
                        } else if (field.getName().equals(DIMENSION_VALUE_FIELD)) {
                            return ((Dimension) val).getValue();
                        }
                        throw new RuntimeException("Unexpected field " + field.getName());
                    }, metricStat.getMetric().getDimensions());
                    // This field is 'faked' in that we just use it as a convenient way to filter single dimensions. As such
                    // we always populate it with the value of the filter if the constraint passed and the filter was singleValue
                    String dimName = (dimensionNameConstraint == null || !dimensionNameConstraint.isSingleValue()) ? null : dimensionNameConstraint.getSingleValue().toString();
                    block.offerValue(DIMENSION_NAME_FIELD, row, dimName);
                    // This field is 'faked' in that we just use it as a convenient way to filter single dimensions. As such
                    // we always populate it with the value of the filter if the constraint passed and the filter was singleValue
                    String dimVal = (dimensionValueConstraint == null || !dimensionValueConstraint.isSingleValue()) ? null : dimensionValueConstraint.getSingleValue().toString();
                    block.offerValue(DIMENSION_VALUE_FIELD, row, dimVal);
                    block.offerValue(PERIOD_FIELD, row, metricStat.getPeriod());
                    boolean matches = true;
                    block.offerValue(VALUE_FIELD, row, values.get(sampleNum));
                    long timestamp = timestamps.get(sampleNum).getTime() / 1000;
                    block.offerValue(TIMESTAMP_FIELD, row, timestamp);
                    return matches ? 1 : 0;
                });
            }
        }
        dataRequest.setNextToken(result.getNextToken());
    } while (dataRequest.getNextToken() != null && !dataRequest.getNextToken().equalsIgnoreCase(prevToken) && queryStatusChecker.isQueryRunning());
}
Also used : HashMap(java.util.HashMap) MetricStat(com.amazonaws.services.cloudwatch.model.MetricStat) GetMetricDataResult(com.amazonaws.services.cloudwatch.model.GetMetricDataResult) MetricDataResult(com.amazonaws.services.cloudwatch.model.MetricDataResult) GetMetricDataResult(com.amazonaws.services.cloudwatch.model.GetMetricDataResult) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) Date(java.util.Date) Field(org.apache.arrow.vector.types.pojo.Field) GetMetricDataRequest(com.amazonaws.services.cloudwatch.model.GetMetricDataRequest) Block(com.amazonaws.athena.connector.lambda.data.Block) MetricDataQuery(com.amazonaws.services.cloudwatch.model.MetricDataQuery) ValueSet(com.amazonaws.athena.connector.lambda.domain.predicate.ValueSet)

Example 3 with GetMetricDataRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricDataRequest 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);
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.arrow.vector.types.pojo.Schema) ArrayList(java.util.ArrayList) MetricStat(com.amazonaws.services.cloudwatch.model.MetricStat) Dimension(com.amazonaws.services.cloudwatch.model.Dimension) TableName(com.amazonaws.athena.connector.lambda.domain.TableName) ReadRecordsRequest(com.amazonaws.athena.connector.lambda.records.ReadRecordsRequest) Constraints(com.amazonaws.athena.connector.lambda.domain.predicate.Constraints) GetMetricDataRequest(com.amazonaws.services.cloudwatch.model.GetMetricDataRequest) Metric(com.amazonaws.services.cloudwatch.model.Metric) Split(com.amazonaws.athena.connector.lambda.domain.Split) ValueSet(com.amazonaws.athena.connector.lambda.domain.predicate.ValueSet) Test(org.junit.Test)

Example 4 with GetMetricDataRequest

use of com.amazonaws.services.cloudwatch.model.GetMetricDataRequest 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;
}
Also used : MetricStat(com.amazonaws.services.cloudwatch.model.MetricStat) ArrayList(java.util.ArrayList) Range(com.amazonaws.athena.connector.lambda.domain.predicate.Range) Date(java.util.Date) SortedRangeSet(com.amazonaws.athena.connector.lambda.domain.predicate.SortedRangeSet) GetMetricDataRequest(com.amazonaws.services.cloudwatch.model.GetMetricDataRequest) Metric(com.amazonaws.services.cloudwatch.model.Metric) Split(com.amazonaws.athena.connector.lambda.domain.Split) MetricDataQuery(com.amazonaws.services.cloudwatch.model.MetricDataQuery) ValueSet(com.amazonaws.athena.connector.lambda.domain.predicate.ValueSet) Metric(com.amazonaws.services.cloudwatch.model.Metric)

Aggregations

GetMetricDataRequest (com.amazonaws.services.cloudwatch.model.GetMetricDataRequest)4 MetricStat (com.amazonaws.services.cloudwatch.model.MetricStat)4 ValueSet (com.amazonaws.athena.connector.lambda.domain.predicate.ValueSet)3 MetricDataQuery (com.amazonaws.services.cloudwatch.model.MetricDataQuery)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 Split (com.amazonaws.athena.connector.lambda.domain.Split)2 Dimension (com.amazonaws.services.cloudwatch.model.Dimension)2 GetMetricDataResult (com.amazonaws.services.cloudwatch.model.GetMetricDataResult)2 Metric (com.amazonaws.services.cloudwatch.model.Metric)2 MetricDataResult (com.amazonaws.services.cloudwatch.model.MetricDataResult)2 HashMap (java.util.HashMap)2 Block (com.amazonaws.athena.connector.lambda.data.Block)1 TableName (com.amazonaws.athena.connector.lambda.domain.TableName)1 Constraints (com.amazonaws.athena.connector.lambda.domain.predicate.Constraints)1 Range (com.amazonaws.athena.connector.lambda.domain.predicate.Range)1 SortedRangeSet (com.amazonaws.athena.connector.lambda.domain.predicate.SortedRangeSet)1 ReadRecordsRequest (com.amazonaws.athena.connector.lambda.records.ReadRecordsRequest)1 Field (org.apache.arrow.vector.types.pojo.Field)1 Schema (org.apache.arrow.vector.types.pojo.Schema)1