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

The following examples show how to use com.amazonaws.services.cloudwatch.model.ListMetricsRequest. 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: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoSelection() throws Exception {
  // When no selection is made, all metrics should be returned
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}), .01);
}
 
Example #2
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoSelection() throws Exception {
  // When no selection is made, all metrics should be returned
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}), .01);
}
 
Example #3
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDimensionsUsesNextToken() throws Exception {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withNextToken("ABC"));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName").NextToken("ABC"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
}
 
Example #4
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionSelectRegex() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select_regex:\n    LoadBalancerName:\n    - myLB(.*)", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest) argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB1")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB2")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB1"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB2"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"}));
}
 
Example #5
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionSelect() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"}));
}
 
Example #6
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensions() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName", cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"), new Dimension().withName("ThisExtraDimensionIsIgnored").withValue("dummy")),
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myOtherLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myOtherLB"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name", "this_extra_dimension_is_ignored"}, new String[]{"aws_elb", "", "a", "myLB", "dummy"}));
}
 
Example #7
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
public boolean matches(Object o) {
 ListMetricsRequest request = (ListMetricsRequest) o;
 if (request == null) return false;
 if (namespace != null && !namespace.equals(request.getNamespace())){
   return false;
 }
 if (metricName != null && !metricName.equals(request.getMetricName())){
   return false;
 }
 if (nextToken == null ^ request.getNextToken() == null) {
   return false;
 }
 if (nextToken != null && !nextToken.equals(request.getNextToken())) {
   return false;
 }
 if (!dimensions.equals(request.getDimensions())) {
   return false;
 }
 return true;
}
 
Example #8
Source File: SpectatorRequestMetricCollectorTest.java    From spectator with Apache License 2.0 6 votes vote down vote up
private void execRequest(String endpoint, int status) {
  AWSRequestMetrics metrics = new AWSRequestMetricsFullSupport();
  metrics.addProperty(AWSRequestMetrics.Field.ServiceName, "AmazonCloudWatch");
  metrics.addProperty(AWSRequestMetrics.Field.ServiceEndpoint, endpoint);
  metrics.addProperty(AWSRequestMetrics.Field.StatusCode, "" + status);
  if (status == 503) {
    metrics.addProperty(AWSRequestMetrics.Field.AWSErrorCode, "Throttled");
  }
  String counterName = "BytesProcessed";
  String timerName = "ClientExecuteTime";
  metrics.setCounter(counterName, 12345);
  metrics.getTimingInfo().addSubMeasurement(timerName, TimingInfo.unmodifiableTimingInfo(100000L, 200000L));

  Request<?> req = new DefaultRequest(new ListMetricsRequest(), "AmazonCloudWatch");
  req.setAWSRequestMetrics(metrics);
  req.setEndpoint(URI.create(endpoint));

  HttpResponse hr = new HttpResponse(req, new HttpPost(endpoint));
  hr.setStatusCode(status);
  Response<?> resp = new Response<>(null, new HttpResponse(req, new HttpPost(endpoint)));

  collector.collectMetrics(req, resp);
}
 
Example #9
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 #10
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDimensionsUsesNextToken() throws Exception {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withNextToken("ABC"));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName").NextToken("ABC"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
}
 
Example #11
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionSelectRegex() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select_regex:\n    LoadBalancerName:\n    - myLB(.*)", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest) argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB1")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB2")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB1"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB2"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"}));
}
 
Example #12
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 #13
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionSelect() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"}));
}
 
Example #14
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensions() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName", cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"), new Dimension().withName("ThisExtraDimensionIsIgnored").withValue("dummy")),
        new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancerName", "myLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancerName", "myOtherLB"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myOtherLB"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name", "this_extra_dimension_is_ignored"}, new String[]{"aws_elb", "", "a", "myLB", "dummy"}));
}
 
Example #15
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
public boolean matches(Object o) {
 ListMetricsRequest request = (ListMetricsRequest) o;
 if (request == null) return false;
 if (namespace != null && !namespace.equals(request.getNamespace())){
   return false;
 }
 if (metricName != null && !metricName.equals(request.getMetricName())){
   return false;
 }
 if (nextToken == null ^ request.getNextToken() == null) {
   return false;
 }
 if (nextToken != null && !nextToken.equals(request.getNextToken())) {
   return false;
 }
 if (!dimensions.equals(request.getDimensions())) {
   return false;
 }
 return true;
}
 
Example #16
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamoIndexDimensions() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: ConsumedReadCapacityUnits\n  aws_dimensions:\n  - TableName\n  - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: OnlineIndexConsumedWriteCapacity\n  aws_dimensions:\n  - TableName\n  - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: ConsumedReadCapacityUnits\n  aws_dimensions:\n  - TableName", cloudWatchClient, taggingClient).register(registry);
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName", "GlobalSecondaryIndexName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex"))));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimensions("TableName", "GlobalSecondaryIndexName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex"))));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(1.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(2.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(3.0)));

  assertEquals(1.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_index_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_dynamodb_online_index_consumed_write_capacity_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_sum", new String[]{"job", "instance", "table_name"}, new String[]{"aws_dynamodb", "", "myTable"}), .01);
}
 
Example #17
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectUsesPaginationToken() throws Exception {
  // Testing "aws_tag_select" with an EC2
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);

  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withPaginationToken("ABC").withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1")));
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().PaginationToken("ABC").ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
}
 
Example #18
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectALB() throws Exception {
  // Testing "aws_tag_select" with an ALB, which have a fairly complex ARN 
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ApplicationELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancer\n  aws_tag_select:\n    resource_type_selection: \"elasticloadbalancing:loadbalancer/app\"\n    resource_id_dimension: LoadBalancer\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("elasticloadbalancing:loadbalancer/app").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:elasticloadbalancing:us-east-1:121212121212:loadbalancer/app/myLB/123")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancer"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancer").withValue("app/myLB/123")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancer").withValue("app/myLB/123")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancer").withValue("app/myOtherLB/456"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancer", "app/myLB/123"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancer", "app/myLB/123"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancer", "app/myOtherLB/456"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(4.0)));
  
  assertEquals(2.0, registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "a", "app/myLB/123"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "b", "app/myLB/123"}), .01);
  assertNull(registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "a", "app/myOtherLB/456"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "load_balancer", "tag_Monitoring"}, new String[]{"aws_applicationelb", "", "arn:aws:elasticloadbalancing:us-east-1:121212121212:loadbalancer/app/myLB/123", "app/myLB/123", "enabled"}), .01);
}
 
Example #19
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectEC2() throws Exception {
  // Testing "aws_tag_select" with an EC2
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
}
 
Example #20
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamoIndexDimensions() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: ConsumedReadCapacityUnits\n  aws_dimensions:\n  - TableName\n  - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: OnlineIndexConsumedWriteCapacity\n  aws_dimensions:\n  - TableName\n  - GlobalSecondaryIndexName\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: ConsumedReadCapacityUnits\n  aws_dimensions:\n  - TableName", cloudWatchClient, taggingClient).register(registry);
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName", "GlobalSecondaryIndexName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex"))));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimensions("TableName", "GlobalSecondaryIndexName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"), new Dimension().withName("GlobalSecondaryIndexName").withValue("myIndex"))));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimensions("TableName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
        new Metric().withDimensions(new Dimension().withName("TableName").withValue("myTable"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(1.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("OnlineIndexConsumedWriteCapacity").Dimension("TableName", "myTable").Dimension("GlobalSecondaryIndexName", "myIndex"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(2.0)));
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("ConsumedReadCapacityUnits").Dimension("TableName", "myTable"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(3.0)));

  assertEquals(1.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_index_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_dynamodb_online_index_consumed_write_capacity_sum", new String[]{"job", "instance", "table_name", "global_secondary_index_name"}, new String[]{"aws_dynamodb", "", "myTable", "myIndex"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_dynamodb_consumed_read_capacity_units_sum", new String[]{"job", "instance", "table_name"}, new String[]{"aws_dynamodb", "", "myTable"}), .01);
}
 
Example #21
Source File: ListMetrics.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        final String USAGE =
            "To run this example, supply a metric name and metric namespace\n" +
            "Ex: ListMetrics <metric-name> <metric-namespace>\n";

        if (args.length != 2) {
            System.out.println(USAGE);
            System.exit(1);
        }

        String name = args[0];
        String namespace = args[1];

        final AmazonCloudWatch cw =
            AmazonCloudWatchClientBuilder.defaultClient();

        ListMetricsRequest request = new ListMetricsRequest()
                .withMetricName(name)
                .withNamespace(namespace);

        boolean done = false;

        while(!done) {
            ListMetricsResult response = cw.listMetrics(request);

            for(Metric metric : response.getMetrics()) {
                System.out.printf(
                    "Retrieved metric %s", metric.getMetricName());
            }

            request.setNextToken(response.getNextToken());

            if(response.getNextToken() == null) {
                done = true;
            }
        }
    }
 
Example #22
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 #23
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 #24
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleSelection() throws Exception {
  // When multiple selections are made, "and" logic should be applied on metrics
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n  aws_dimension_select:\n    InstanceId: [\"i-1\"]", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1"),
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
}
 
Example #25
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultipleSelection() throws Exception {
  // When multiple selections are made, "and" logic should be applied on metrics
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n  aws_dimension_select:\n    InstanceId: [\"i-1\"]", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1"),
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
}
 
Example #26
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectUsesPaginationToken() throws Exception {
  // Testing "aws_tag_select" with an EC2
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);

  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withPaginationToken("ABC").withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1")));
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().PaginationToken("ABC").ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
}
 
Example #27
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectALB() throws Exception {
  // Testing "aws_tag_select" with an ALB, which have a fairly complex ARN 
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ApplicationELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancer\n  aws_tag_select:\n    resource_type_selection: \"elasticloadbalancing:loadbalancer/app\"\n    resource_id_dimension: LoadBalancer\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("elasticloadbalancing:loadbalancer/app").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:elasticloadbalancing:us-east-1:121212121212:loadbalancer/app/myLB/123")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancer"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancer").withValue("app/myLB/123")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancer").withValue("app/myLB/123")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancer").withValue("app/myOtherLB/456"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancer", "app/myLB/123"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "b").Dimension("LoadBalancer", "app/myLB/123"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ApplicationELB").MetricName("RequestCount").Dimension("AvailabilityZone", "a").Dimension("LoadBalancer", "app/myOtherLB/456"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(4.0)));
  
  assertEquals(2.0, registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "a", "app/myLB/123"}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "b", "app/myLB/123"}), .01);
  assertNull(registry.getSampleValue("aws_applicationelb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer"}, new String[]{"aws_applicationelb", "", "a", "app/myOtherLB/456"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "load_balancer", "tag_Monitoring"}, new String[]{"aws_applicationelb", "", "arn:aws:elasticloadbalancing:us-east-1:121212121212:loadbalancer/app/myLB/123", "app/myLB/123", "enabled"}), .01);
}
 
Example #28
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testTagSelectEC2() throws Exception {
  // Testing "aws_tag_select" with an EC2
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n    tag_selections:\n      Monitoring: [enabled]\n", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance").TagFilter("Monitoring", Arrays.asList("enabled")))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
}
 
Example #29
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 4 votes vote down vote up
@Test
public void testOptionalTagSelection() throws Exception {
  // aws_tag_select can be used without tag_selection to activate the aws_resource_info metric on tagged (or previously tagged) resources
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n  aws_dimension_select:\n    InstanceId: [\"i-1\", \"i-no-tag\"]", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance"))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1"),
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-no-tag"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-no-tag"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(4.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(4.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-no-tag"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
  assertNull(registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-no-tag", "i-no-tag", "enabled"}));
}
 
Example #30
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 4 votes vote down vote up
@Test
public void testOptionalTagSelection() throws Exception {
  // aws_tag_select can be used without tag_selection to activate the aws_resource_info metric on tagged (or previously tagged) resources
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/EC2\n  aws_metric_name: CPUUtilization\n  aws_dimensions:\n  - InstanceId\n  aws_tag_select:\n    resource_type_selection: \"ec2:instance\"\n    resource_id_dimension: InstanceId\n  aws_dimension_select:\n    InstanceId: [\"i-1\", \"i-no-tag\"]", 
      cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(taggingClient.getResources((GetResourcesRequest)argThat(
      new GetResourcesRequestMatcher().ResourceTypeFilter("ec2:instance"))))
      .thenReturn(new GetResourcesResult().withResourceTagMappingList(
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-1"),
          new ResourceTagMapping().withTags(new Tag().withKey("Monitoring").withValue("enabled")).withResourceARN("arn:aws:ec2:us-east-1:121212121212:instance/i-2")));
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimensions("InstanceId"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-1")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-2")),
          new Metric().withDimensions(new Dimension().withName("InstanceId").withValue("i-no-tag"))));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-1"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(2.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-2"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(3.0)));
  
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/EC2").MetricName("CPUUtilization").Dimension("InstanceId", "i-no-tag"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(4.0)));

  assertEquals(2.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-1"}), .01);
  assertNull(registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-2"}));
  assertEquals(4.0, registry.getSampleValue("aws_ec2_cpuutilization_average", new String[]{"job", "instance", "instance_id"}, new String[]{"aws_ec2", "", "i-no-tag"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-1", "i-1", "enabled"}), .01);
  assertEquals(1.0, registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-2", "i-2", "enabled"}), .01);
  assertNull(registry.getSampleValue("aws_resource_info", new String[]{"job", "instance", "arn", "instance_id", "tag_Monitoring"}, new String[]{"aws_ec2", "", "arn:aws:ec2:us-east-1:121212121212:instance/i-no-tag", "i-no-tag", "enabled"}));
}