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

The following examples show how to use com.amazonaws.services.cloudwatch.model.GetMetricStatisticsResult. 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: CloudWatchBaseTest.java    From aws-mock with MIT License 6 votes vote down vote up
/**
 * GetMetricStaticticsTest to get the data points
 *
 * @return Datapoint
 */
protected final Datapoint getMetricStaticticsTest(String metricName) {
    Datapoint dataPoint = null;
    GetMetricStatisticsRequest request = new GetMetricStatisticsRequest();
    request.setStartTime(new DateTime().plusHours(HOURS).toDate());
    request.withNamespace(NAMESPACE);
    request.withPeriod(60 * 60);
    request.withMetricName(metricName);
    request.withStatistics("Average", "SampleCount");
    request.withEndTime(new Date());
    GetMetricStatisticsResult result = amazonCloudWatchClient.getMetricStatistics(request);
    if (result != null && !result.getDatapoints().isEmpty()) {
        dataPoint = result.getDatapoints().get(0);
    }

    return dataPoint;
}
 
Example #2
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtendedStatistics() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: Latency\n  aws_extended_statistics:\n  - p95\n  - p99.99", cloudWatchClient, taggingClient).register(registry);

  HashMap<String, Double> extendedStatistics = new HashMap<String, Double>();
  extendedStatistics.put("p95", 1.0);
  extendedStatistics.put("p99.99", 2.0);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("Latency"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withExtendedStatistics(extendedStatistics)));

  assertEquals(1.0, registry.getSampleValue("aws_elb_latency_p95", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_latency_p99_99", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .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 testAllSelectDimensionsKnown() 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\n    AvailabilityZone:\n    - a\n    - b", cloudWatchClient, taggingClient).register(registry);
  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 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 #7
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 #8
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudwatchTimestamps() throws Exception {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  set_timestamp: true\n- aws_namespace: AWS/ELB\n  aws_metric_name: HTTPCode_Backend_2XX\n  set_timestamp: false"
          , cloudWatchClient, taggingClient).register(registry);

  Date timestamp = new Date();
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
          new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount"))))
          .thenReturn(new GetMetricStatisticsResult().withDatapoints(
                  new Datapoint().withTimestamp(timestamp).withAverage(1.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
          new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("HTTPCode_Backend_2XX"))))
          .thenReturn(new GetMetricStatisticsResult().withDatapoints(
                  new Datapoint().withTimestamp(timestamp).withAverage(1.0)));

  assertMetricTimestampEquals(registry, "aws_elb_request_count_average", timestamp.getTime());
  assertMetricTimestampEquals(registry, "aws_elb_httpcode_backend_2_xx_average", null);

}
 
Example #9
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllStatistics() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(1.0)
              .withMaximum(2.0).withMinimum(3.0).withSampleCount(4.0).withSum(5.0)));

  assertEquals(1.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_maximum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_minimum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(4.0, registry.getSampleValue("aws_elb_request_count_sample_count", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(5.0, registry.getSampleValue("aws_elb_request_count_sum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
}
 
Example #10
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultPeriod() {
  new CloudWatchCollector(
                  "---\nregion: reg\nperiod_seconds: 100\nrange_seconds: 200\ndelay_seconds: 300\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) anyObject()))
          .thenReturn(new GetMetricStatisticsResult());

  registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""});


  Mockito.verify(cloudWatchClient).getMetricStatistics((GetMetricStatisticsRequest) argThat(
          new GetMetricStatisticsRequestMatcher()
                  .Namespace("AWS/ELB")
                  .MetricName("RequestCount")
                  .Period(100)
  ));
}
 
Example #11
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricPeriod() {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  period_seconds: 100\n  range_seconds: 200\n  delay_seconds: 300", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) anyObject()))
          .thenReturn(new GetMetricStatisticsResult());

  registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""});


  Mockito.verify(cloudWatchClient).getMetricStatistics((GetMetricStatisticsRequest) argThat(
          new GetMetricStatisticsRequestMatcher()
                  .Namespace("AWS/ELB")
                  .MetricName("RequestCount")
                  .Period(100)
  ));
}
 
Example #12
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 #13
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 #14
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testExtendedStatistics() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: Latency\n  aws_extended_statistics:\n  - p95\n  - p99.99", cloudWatchClient, taggingClient).register(registry);

  HashMap<String, Double> extendedStatistics = new HashMap<String, Double>();
  extendedStatistics.put("p95", 1.0);
  extendedStatistics.put("p99.99", 2.0);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("Latency"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withExtendedStatistics(extendedStatistics)));

  assertEquals(1.0, registry.getSampleValue("aws_elb_latency_p95", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_latency_p99_99", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
}
 
Example #15
Source File: SimplifiedKinesisClientTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCountBytesWhenSingleDataPointReturned() throws Exception {
  Instant countSince = new Instant("2017-04-06T10:00:00.000Z");
  Instant countTo = new Instant("2017-04-06T11:00:00.000Z");
  Minutes periodTime = Minutes.minutesBetween(countSince, countTo);
  GetMetricStatisticsRequest metricStatisticsRequest =
      underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime);
  GetMetricStatisticsResult result =
      new GetMetricStatisticsResult().withDatapoints(new Datapoint().withSum(1.0));

  when(cloudWatch.getMetricStatistics(metricStatisticsRequest)).thenReturn(result);

  long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo);

  assertThat(backlogBytes).isEqualTo(1L);
}
 
Example #16
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllStatistics() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withAverage(1.0)
              .withMaximum(2.0).withMinimum(3.0).withSampleCount(4.0).withSum(5.0)));

  assertEquals(1.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_maximum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(3.0, registry.getSampleValue("aws_elb_request_count_minimum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(4.0, registry.getSampleValue("aws_elb_request_count_sample_count", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
  assertEquals(5.0, registry.getSampleValue("aws_elb_request_count_sum", new String[]{"job", "instance"}, new String[]{"aws_elb", ""}), .01);
}
 
Example #17
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloudwatchTimestamps() throws Exception {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  set_timestamp: true\n- aws_namespace: AWS/ELB\n  aws_metric_name: HTTPCode_Backend_2XX\n  set_timestamp: false"
          , cloudWatchClient, taggingClient).register(registry);

  Date timestamp = new Date();
  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
          new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount"))))
          .thenReturn(new GetMetricStatisticsResult().withDatapoints(
                  new Datapoint().withTimestamp(timestamp).withAverage(1.0)));

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
          new GetMetricStatisticsRequestMatcher().Namespace("AWS/ELB").MetricName("HTTPCode_Backend_2XX"))))
          .thenReturn(new GetMetricStatisticsResult().withDatapoints(
                  new Datapoint().withTimestamp(timestamp).withAverage(1.0)));

  assertMetricTimestampEquals(registry, "aws_elb_request_count_average", timestamp.getTime());
  assertMetricTimestampEquals(registry, "aws_elb_httpcode_backend_2_xx_average", null);

}
 
Example #18
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 #19
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDefaultPeriod() {
  new CloudWatchCollector(
                  "---\nregion: reg\nperiod_seconds: 100\nrange_seconds: 200\ndelay_seconds: 300\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) anyObject()))
          .thenReturn(new GetMetricStatisticsResult());

  registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""});


  Mockito.verify(cloudWatchClient).getMetricStatistics((GetMetricStatisticsRequest) argThat(
          new GetMetricStatisticsRequestMatcher()
                  .Namespace("AWS/ELB")
                  .MetricName("RequestCount")
                  .Period(100)
  ));
}
 
Example #20
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 #21
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricPeriod() {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  period_seconds: 100\n  range_seconds: 200\n  delay_seconds: 300", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest) anyObject()))
          .thenReturn(new GetMetricStatisticsResult());

  registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance"}, new String[]{"aws_elb", ""});


  Mockito.verify(cloudWatchClient).getMetricStatistics((GetMetricStatisticsRequest) argThat(
          new GetMetricStatisticsRequestMatcher()
                  .Namespace("AWS/ELB")
                  .MetricName("RequestCount")
                  .Period(100)
  ));
}
 
Example #22
Source File: SimplifiedKinesisClientTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldCountBytesWhenMultipleDataPointsReturned() throws Exception {
  Instant countSince = new Instant("2017-04-06T10:00:00.000Z");
  Instant countTo = new Instant("2017-04-06T11:00:00.000Z");
  Minutes periodTime = Minutes.minutesBetween(countSince, countTo);
  GetMetricStatisticsRequest metricStatisticsRequest =
      underTest.createMetricStatisticsRequest(STREAM, countSince, countTo, periodTime);
  GetMetricStatisticsResult result =
      new GetMetricStatisticsResult()
          .withDatapoints(
              new Datapoint().withSum(1.0),
              new Datapoint().withSum(3.0),
              new Datapoint().withSum(2.0));

  when(cloudWatch.getMetricStatistics(metricStatisticsRequest)).thenReturn(result);

  long backlogBytes = underTest.getBacklogBytes(STREAM, countSince, countTo);

  assertThat(backlogBytes).isEqualTo(6L);
}
 
Example #23
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testAllSelectDimensionsKnown() 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\n    AvailabilityZone:\n    - a\n    - b", cloudWatchClient, taggingClient).register(registry);
  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 #24
Source File: SimplifiedKinesisClient.java    From beam with Apache License 2.0 6 votes vote down vote up
/**
 * Gets total size in bytes of all events that remain in Kinesis stream between specified
 * instants.
 *
 * @return total size in bytes of all Kinesis events after specified instant
 */
public long getBacklogBytes(
    final String streamName, final Instant countSince, final Instant countTo)
    throws TransientKinesisException {
  return wrapExceptions(
      () -> {
        Minutes period = Minutes.minutesBetween(countSince, countTo);
        if (period.isLessThan(Minutes.ONE)) {
          return 0L;
        }

        GetMetricStatisticsRequest request =
            createMetricStatisticsRequest(streamName, countSince, countTo, period);

        long totalSizeInBytes = 0;
        GetMetricStatisticsResult result = cloudWatch.getMetricStatistics(request);
        for (Datapoint point : result.getDatapoints()) {
          totalSizeInBytes += point.getSum().longValue();
        }
        return totalSizeInBytes;
      });
}
 
Example #25
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 #26
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 #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 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 #29
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamoNoDimensions() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/DynamoDB\n  aws_metric_name: AccountProvisionedReadCapacityUtilization\n", cloudWatchClient, taggingClient).register(registry);

  Mockito.when(cloudWatchClient.getMetricStatistics((GetMetricStatisticsRequest)argThat(
      new GetMetricStatisticsRequestMatcher().Namespace("AWS/DynamoDB").MetricName("AccountProvisionedReadCapacityUtilization"))))
      .thenReturn(new GetMetricStatisticsResult().withDatapoints(
          new Datapoint().withTimestamp(new Date()).withSum(1.0)));

  assertEquals(1.0, registry.getSampleValue("aws_dynamodb_account_provisioned_read_capacity_utilization_sum", new String[]{"job", "instance"}, new String[]{"aws_dynamodb", ""}), .01);
}
 
Example #30
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);
}