com.amazonaws.services.cloudwatch.model.DimensionFilter Java Examples

The following examples show how to use com.amazonaws.services.cloudwatch.model.DimensionFilter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MetricUtils.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to push the supplied predicate constraints onto the Cloudwatch Metrics request.
 */
protected static void pushDownPredicate(Constraints constraints, ListMetricsRequest listMetricsRequest)
{
    Map<String, ValueSet> summary = constraints.getSummary();

    ValueSet namespaceConstraint = summary.get(NAMESPACE_FIELD);
    if (namespaceConstraint != null && namespaceConstraint.isSingleValue()) {
        listMetricsRequest.setNamespace(namespaceConstraint.getSingleValue().toString());
    }

    ValueSet metricConstraint = summary.get(METRIC_NAME_FIELD);
    if (metricConstraint != null && metricConstraint.isSingleValue()) {
        listMetricsRequest.setMetricName(metricConstraint.getSingleValue().toString());
    }

    ValueSet dimensionNameConstraint = summary.get(DIMENSION_NAME_FIELD);
    ValueSet dimensionValueConstraint = summary.get(DIMENSION_VALUE_FIELD);
    if (dimensionNameConstraint != null && dimensionNameConstraint.isSingleValue() &&
            dimensionValueConstraint != null && dimensionValueConstraint.isSingleValue()) {
        DimensionFilter filter = new DimensionFilter()
                .withName(dimensionNameConstraint.getSingleValue().toString())
                .withValue(dimensionValueConstraint.getSingleValue().toString());
        listMetricsRequest.setDimensions(Collections.singletonList(filter));
    }
}
 
Example #2
Source File: MetricUtilsTest.java    From aws-athena-query-federation with Apache License 2.0 6 votes vote down vote up
@Test
public void pushDownPredicate()
{
    Map<String, ValueSet> constraintsMap = new HashMap<>();
    constraintsMap.put(NAMESPACE_FIELD, makeStringEquals(allocator, "match1"));
    constraintsMap.put(METRIC_NAME_FIELD, makeStringEquals(allocator, "match2"));
    constraintsMap.put(STATISTIC_FIELD, makeStringEquals(allocator, "match3"));
    constraintsMap.put(DIMENSION_NAME_FIELD, makeStringEquals(allocator, "match4"));
    constraintsMap.put(DIMENSION_VALUE_FIELD, makeStringEquals(allocator, "match5"));

    ListMetricsRequest request = new ListMetricsRequest();
    MetricUtils.pushDownPredicate(new Constraints(constraintsMap), request);

    assertEquals("match1", request.getNamespace());
    assertEquals("match2", request.getMetricName());
    assertEquals(1, request.getDimensions().size());
    assertEquals(new DimensionFilter().withName("match4").withValue("match5"), request.getDimensions().get(0));
}
 
Example #3
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
private List<List<Dimension>> listDimensions(MetricRule rule, List<String> tagBasedResourceIds, AmazonCloudWatch cloudWatchClient) {
  List<List<Dimension>> dimensions = new ArrayList<List<Dimension>>();
  if (rule.awsDimensions == null) {
    dimensions.add(new ArrayList<Dimension>());
    return dimensions;
  }

  ListMetricsRequest request = new ListMetricsRequest();
  request.setNamespace(rule.awsNamespace);
  request.setMetricName(rule.awsMetricName);
  List<DimensionFilter> dimensionFilters = new ArrayList<DimensionFilter>();
  for (String dimension: rule.awsDimensions) {
    dimensionFilters.add(new DimensionFilter().withName(dimension));
  }
  request.setDimensions(dimensionFilters);

  String nextToken = null;
  do {
    request.setNextToken(nextToken);
    ListMetricsResult result = cloudWatchClient.listMetrics(request);
    cloudwatchRequests.labels("listMetrics", rule.awsNamespace).inc();
    for (Metric metric: result.getMetrics()) {
      if (metric.getDimensions().size() != dimensionFilters.size()) {
        // AWS returns all the metrics with dimensions beyond the ones we ask for,
        // so filter them out.
        continue;
      }
      if (useMetric(rule, tagBasedResourceIds, metric)) {
        dimensions.add(metric.getDimensions());
      }
    }
    nextToken = result.getNextToken();
  } while (nextToken != null);

  return dimensions;
}
 
Example #4
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
public ListMetricsRequestMatcher Dimensions(String... dimensions) {
  this.dimensions = new ArrayList<DimensionFilter>();
  for (int i = 0; i < dimensions.length; i++) {
    this.dimensions.add(new DimensionFilter().withName(dimensions[i]));
  }
  return this;
}
 
Example #5
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
private List<List<Dimension>> listDimensions(MetricRule rule, List<String> tagBasedResourceIds, AmazonCloudWatch cloudWatchClient) {
  List<List<Dimension>> dimensions = new ArrayList<List<Dimension>>();
  if (rule.awsDimensions == null) {
    dimensions.add(new ArrayList<Dimension>());
    return dimensions;
  }

  ListMetricsRequest request = new ListMetricsRequest();
  request.setNamespace(rule.awsNamespace);
  request.setMetricName(rule.awsMetricName);
  List<DimensionFilter> dimensionFilters = new ArrayList<DimensionFilter>();
  for (String dimension: rule.awsDimensions) {
    dimensionFilters.add(new DimensionFilter().withName(dimension));
  }
  request.setDimensions(dimensionFilters);

  String nextToken = null;
  do {
    request.setNextToken(nextToken);
    ListMetricsResult result = cloudWatchClient.listMetrics(request);
    cloudwatchRequests.labels("listMetrics", rule.awsNamespace).inc();
    for (Metric metric: result.getMetrics()) {
      if (metric.getDimensions().size() != dimensionFilters.size()) {
        // AWS returns all the metrics with dimensions beyond the ones we ask for,
        // so filter them out.
        continue;
      }
      if (useMetric(rule, tagBasedResourceIds, metric)) {
        dimensions.add(metric.getDimensions());
      }
    }
    nextToken = result.getNextToken();
  } while (nextToken != null);

  return dimensions;
}
 
Example #6
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
public ListMetricsRequestMatcher Dimensions(String... dimensions) {
  this.dimensions = new ArrayList<DimensionFilter>();
  for (int i = 0; i < dimensions.length; i++) {
    this.dimensions.add(new DimensionFilter().withName(dimensions[i]));
  }
  return this;
}
 
Example #7
Source File: CloudWatchIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testKeyValueOperations() throws Exception {

    AmazonCloudWatchClient cwClient = provider.getClient();
    Assume.assumeNotNull("AWS client not null", cwClient);

    List<Metric> staleMetrics = cwClient.listMetrics(new ListMetricsRequest().withNamespace(NAMESPACE)).getMetrics()
            .stream() //
            .filter(metric -> !metric.getMetricName().startsWith(CloudWatchIntegrationTest.class.getSimpleName())
                    || System.currentTimeMillis()
                            - AWSUtils.toEpochMillis(metric.getMetricName()) > AWSUtils.TWO_WEEKS) //
            .collect(Collectors.toList());
    if (staleMetrics.size() > 0) {
        Assert.fail("Found '" + CloudWatchIntegrationTest.class.getName() + "-*' metrics older than two weeks: "
                + staleMetrics);
    }

    WildFlyCamelContext camelctx = new WildFlyCamelContext();
    camelctx.getNamingContext().bind("cwClient", cwClient);

    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:metrics").to("aws-cw://" + NAMESPACE + "?amazonCwClient=#cwClient");
        }
    });

    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(CwConstants.METRIC_NAME, METRIC_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_NAME, DIM_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_VALUE, DIM_VALUE);

        ListMetricsRequest request = new ListMetricsRequest().withNamespace(NAMESPACE).withMetricName(METRIC_NAME)
                .withDimensions(new DimensionFilter().withName(DIM_NAME).withValue(DIM_VALUE));

        List<Metric> metrics = Collections.emptyList();
        ProducerTemplate producer = camelctx.createProducerTemplate();
        for (int i = 100; i < 105 && metrics.size() == 0; i++) {
            producer.sendBodyAndHeaders("direct:metrics", new Double(i), headers);
            metrics = cwClient.listMetrics(request).getMetrics();
            System.out.println("metrics #" + i + ": " + metrics);
            Thread.sleep(1000);
        }

        // It may take several minutes for the metric to show up
        // Assert.assertEquals(1, metrics.size());

    } finally {
        camelctx.close();
    }
}