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

The following examples show how to use com.amazonaws.services.cloudwatch.model.Metric. 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: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
/**
 * Check if a metric is matched in `aws_tag_select`
 */
private boolean metricIsInAwsTagSelect(MetricRule rule, List<String> tagBasedResourceIds, Metric metric) {
  if (rule.awsTagSelect.tagSelections == null) {
    return true;
  }
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (rule.awsTagSelect.resourceIdDimension.equals(dimensionName)) {
      if (!tagBasedResourceIds.contains(dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #8
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 #9
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
/**
 * Check if a metric is matched in `aws_tag_select`
 */
private boolean metricIsInAwsTagSelect(MetricRule rule, List<String> tagBasedResourceIds, Metric metric) {
  if (rule.awsTagSelect.tagSelections == null) {
    return true;
  }
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (rule.awsTagSelect.resourceIdDimension.equals(dimensionName)) {
      if (!tagBasedResourceIds.contains(dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #10
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 #11
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 #12
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 #13
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric is matched in `aws_dimension_select`
 */
private boolean metricsIsInAwsDimensionSelect(MetricRule rule, Metric metric) {
  Set<String> dimensionSelectKeys = rule.awsDimensionSelect.keySet();
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (dimensionSelectKeys.contains(dimensionName)) {
      List<String> allowedDimensionValues = rule.awsDimensionSelect.get(dimensionName);
      if (!allowedDimensionValues.contains(dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #14
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 #15
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 #16
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric should be used according to `aws_dimension_select`, `aws_dimension_select_regex` and dynamic `aws_tag_select`
 */
private boolean useMetric(MetricRule rule, List<String> tagBasedResourceIds, Metric metric) {
  if (rule.awsDimensionSelect != null && !metricsIsInAwsDimensionSelect(rule, metric)) {
    return false;
  }
  if (rule.awsDimensionSelectRegex != null && !metricIsInAwsDimensionSelectRegex(rule, metric)) {
    return false;
  }
  if (rule.awsTagSelect != null && !metricIsInAwsTagSelect(rule, tagBasedResourceIds, metric)) {
    return false;
  }
  return true;
}
 
Example #17
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 #18
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric is matched in `aws_dimension_select_regex`
 */
private boolean metricIsInAwsDimensionSelectRegex(MetricRule rule, Metric metric) {
  Set<String> dimensionSelectRegexKeys = rule.awsDimensionSelectRegex.keySet();
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (dimensionSelectRegexKeys.contains(dimensionName)) {
      List<String> allowedDimensionValues = rule.awsDimensionSelectRegex.get(dimensionName);
      if (!regexListMatch(allowedDimensionValues, dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #19
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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric is matched in `aws_dimension_select_regex`
 */
private boolean metricIsInAwsDimensionSelectRegex(MetricRule rule, Metric metric) {
  Set<String> dimensionSelectRegexKeys = rule.awsDimensionSelectRegex.keySet();
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (dimensionSelectRegexKeys.contains(dimensionName)) {
      List<String> allowedDimensionValues = rule.awsDimensionSelectRegex.get(dimensionName);
      if (!regexListMatch(allowedDimensionValues, dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #27
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric is matched in `aws_dimension_select`
 */
private boolean metricsIsInAwsDimensionSelect(MetricRule rule, Metric metric) {
  Set<String> dimensionSelectKeys = rule.awsDimensionSelect.keySet();
  for (Dimension dimension : metric.getDimensions()) {
    String dimensionName = dimension.getName();
    String dimensionValue = dimension.getValue();
    if (dimensionSelectKeys.contains(dimensionName)) {
      List<String> allowedDimensionValues = rule.awsDimensionSelect.get(dimensionName);
      if (!allowedDimensionValues.contains(dimensionValue)) {
        return false;
      }
    }
  }
  return true;
}
 
Example #28
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
/**
 * Check if a metric should be used according to `aws_dimension_select`, `aws_dimension_select_regex` and dynamic `aws_tag_select`
 */
private boolean useMetric(MetricRule rule, List<String> tagBasedResourceIds, Metric metric) {
  if (rule.awsDimensionSelect != null && !metricsIsInAwsDimensionSelect(rule, metric)) {
    return false;
  }
  if (rule.awsDimensionSelectRegex != null && !metricIsInAwsDimensionSelectRegex(rule, metric)) {
    return false;
  }
  if (rule.awsTagSelect != null && !metricIsInAwsTagSelect(rule, tagBasedResourceIds, metric)) {
    return false;
  }
  return true;
}
 
Example #29
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 #30
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;
            }
        }
    }