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

The following examples show how to use com.amazonaws.services.cloudwatch.model.Dimension. 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: 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 #2
Source File: DimensionMapperTest.java    From swage with Apache License 2.0 6 votes vote down vote up
@Test
public void global_and_normal_dimensions_combine() throws Exception {
    MetricRecorder.RecorderContext context = new MetricRecorder.RecorderContext(ContextData.withId(ID)
                                  .add(StandardContext.SERVICE, SERVICE_NAME)
                                  .build());

    DimensionMapper mapper = new DimensionMapper.Builder()
            .addGlobalDimension(ContextData.ID)
            .addMetric(METRIC, Arrays.asList(StandardContext.SERVICE))
            .build();
    Dimension expectedIdDimension = new Dimension().withName(ContextData.ID.name).withValue(ID);
    Dimension expectedServiceDimension = new Dimension().withName(StandardContext.SERVICE.name).withValue(SERVICE_NAME);

    List<Dimension> dims = mapper.getDimensions(METRIC, context);
    assertTrue("Dimension list is missing mapped ID dimension", dims.contains(expectedIdDimension));
    assertTrue("Dimension list is missing mapped SERVICE dimension", dims.contains(expectedServiceDimension));
    assertEquals("Unexpected size of dimension list", 2, dims.size());
}
 
Example #3
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
private List<List<Dimension>> permuteDimensions(List<String> dimensions, Map<String, List<String>> dimensionValues) {
    ArrayList<List<Dimension>> result = new ArrayList<List<Dimension>>();

    if (dimensions.size() == 0) {
        result.add(new ArrayList<Dimension>());
    } else {
        List<String> dimensionsCopy = new ArrayList<String>(dimensions);
        String dimensionName = dimensionsCopy.remove(dimensionsCopy.size() - 1);
        for (List<Dimension> permutation : permuteDimensions(dimensionsCopy, dimensionValues)) {
            for (String dimensionValue : dimensionValues.get(dimensionName)) {
                Dimension dimension = new Dimension();
                dimension.setValue(dimensionValue);
                dimension.setName(dimensionName);
                ArrayList<Dimension> permutationCopy = new ArrayList<Dimension>(permutation);
                permutationCopy.add(dimension);
                result.add(permutationCopy);
            }
        }
    }

    return result;
}
 
Example #4
Source File: MetricDataAggregator.java    From swage with Apache License 2.0 6 votes vote down vote up
/**
 * Add a metric event to be aggregated.
 * Events with the same name, unit, and attributes will have their values
 * aggregated into {@link StatisticSet}s, with the aggregated data
 * available via {@link #flush}.
 *
 * @param context Metric context to use for dimension information
 * @param name Metric name
 * @param value Recorded value for the metric event
 * @param unit Unit for interpreting the value
 */
public void add(
        final MetricRecorder.RecorderContext context,
        final Metric name,
        final double value,
        final StandardUnit unit)
{
    //TODO: avoid doing this every time for a context - caching, or?
    List<Dimension> dimensions = dimensionMapper.getDimensions(name, context);

    DatumKey key = new DatumKey(name.toString(), unit, dimensions);
    statisticsMap.merge(
            key,
            new StatisticSet()
                    .withMaximum(value)
                    .withMinimum(value)
                    .withSampleCount(1D)
                    .withSum(value),
            MetricDataAggregator::sum);
}
 
Example #5
Source File: DimensionMapper.java    From swage with Apache License 2.0 6 votes vote down vote up
/**
 * Pull desired info out of the metric context attributes and convert to
 * CloudWatch {@link Dimension} objects, according to the filter mapping
 * defined.
 *
 * If the context does not contain an entry for a mapped dimension key,
 * the dimension will be added with a value of "null".
 *
 * @param metric Metric being sent
 * @param context the context in which the metric was recorded.
 * @return A list of Dimensions appropriate to send to CloudWatch
 */
public List<Dimension> getDimensions(final Metric metric, final MetricRecorder.RecorderContext context)
{
    //Flattening the context hierarchy here allows this dimension mapper to work with multiple cloudwatch
    //recorders. If a recorder wants to precompute the flattened hierarchy it can do so in its implementation
    //of RecorderContext.
    TypedMap attributes = flattenContextHierarchy(context);
    Set<TypedMap.Key> dimensionKeys = filterMap.get(metric);
    if (dimensionKeys == null) {
        dimensionKeys = Collections.emptySet();
    }

    Stream<TypedMap.Key> keys = Stream.concat(globalDimensions.stream(), dimensionKeys.stream());
    return keys.map(key -> new Dimension().withName(key.name).withValue(String.valueOf(attributes.get(key))))
        .distinct()
        .sorted(Comparator.comparing(Dimension::getName))
        .collect(Collectors.toList());
}
 
Example #6
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 #7
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 #8
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 #9
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 #10
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetDimensionsUsesNextToken() throws Exception {
  new CloudWatchCollector(
          "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withNextToken("ABC"));
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName").NextToken("ABC"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB"))));

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

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
}
 
Example #11
Source File: DimensionMapperTest.java    From swage with Apache License 2.0 6 votes vote down vote up
@Test
public void parent_and_child_attributes_combine() throws Exception {
    String childId = UUID.randomUUID().toString();
    MetricRecorder.RecorderContext context = new MetricRecorder.RecorderContext(ContextData.withId(ID)
                                  .add(StandardContext.SERVICE, SERVICE_NAME)
                                  .build());

    MetricRecorder.RecorderContext childContext = context.newChild(ContextData.withId(childId)
            .add(StandardContext.OPERATION, OPERATION)
            .build());

    DimensionMapper mapper = new DimensionMapper.Builder()
            .addGlobalDimension(ContextData.ID)
            .addMetric(METRIC, Arrays.asList(StandardContext.SERVICE, StandardContext.OPERATION))
            .build();

    Dimension expectedIdDimension = new Dimension().withName(ContextData.ID.name).withValue(childId);
    Dimension expectedServiceDimension = new Dimension().withName(StandardContext.SERVICE.name).withValue(SERVICE_NAME);
    Dimension expectedOperationDimension = new Dimension().withName(StandardContext.OPERATION.name).withValue(OPERATION);
    List<Dimension> expected = Arrays.asList(expectedIdDimension,
            expectedOperationDimension,
            expectedServiceDimension);

    List<Dimension> dims = mapper.getDimensions(METRIC, childContext);
    assertEquals(expected, dims);
}
 
Example #12
Source File: AwsCloudWatchService.java    From cloudbreak with Apache License 2.0 6 votes vote down vote up
public void addCloudWatchAlarmsForSystemFailures(List<CloudResource> instances, CloudStack stack, String regionName, AwsCredentialView credentialView) {
    if (isCloudwatchEnabled(stack)) {
        instances.stream().forEach(instance -> {
            try {
                PutMetricAlarmRequest metricAlarmRequest = new PutMetricAlarmRequest();
                metricAlarmRequest.setAlarmActions(Arrays.asList("arn:aws:automate:" + regionName + ":ec2:recover"));
                metricAlarmRequest.setAlarmName(instance.getInstanceId() + alarmSuffix);
                metricAlarmRequest.setMetricName("StatusCheckFailed_System");
                metricAlarmRequest.setStatistic("Maximum");
                metricAlarmRequest.setNamespace("AWS/EC2");
                metricAlarmRequest.setDimensions(Arrays.asList(new Dimension().withName("InstanceId").withValue(instance.getInstanceId())));
                metricAlarmRequest.setPeriod(cloudwatchPeriod);
                metricAlarmRequest.setEvaluationPeriods(cloudwatchEvaluationPeriods);
                metricAlarmRequest.setThreshold(cloudwatchThreshhold);
                metricAlarmRequest.setComparisonOperator("GreaterThanOrEqualToThreshold");
                AmazonCloudWatchClient amazonCloudWatchClient = awsClient.createCloudWatchClient(credentialView, regionName);
                amazonCloudWatchClient.putMetricAlarm(metricAlarmRequest);
                LOGGER.debug("Created cloudwatch alarm for instanceId {}.", instance.getInstanceId());
            } catch (AmazonCloudWatchException acwe) {
                LOGGER.error("Unable to create cloudwatch alarm for instanceId {}: {}", instance.getInstanceId(), acwe.getLocalizedMessage());
            }
        });
    }
}
 
Example #13
Source File: CloudWatchCollectorTest.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
@Test
public void testDimensionSelect() throws Exception {
  new CloudWatchCollector(
      "---\nregion: reg\nmetrics:\n- aws_namespace: AWS/ELB\n  aws_metric_name: RequestCount\n  aws_dimensions:\n  - AvailabilityZone\n  - LoadBalancerName\n  aws_dimension_select:\n    LoadBalancerName:\n    - myLB", cloudWatchClient, taggingClient).register(registry);
  Mockito.when(cloudWatchClient.listMetrics((ListMetricsRequest)argThat(
      new ListMetricsRequestMatcher().Namespace("AWS/ELB").MetricName("RequestCount").Dimensions("AvailabilityZone", "LoadBalancerName"))))
      .thenReturn(new ListMetricsResult().withMetrics(
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("b"), new Dimension().withName("LoadBalancerName").withValue("myLB")),
          new Metric().withDimensions(new Dimension().withName("AvailabilityZone").withValue("a"), new Dimension().withName("LoadBalancerName").withValue("myOtherLB"))));

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

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

  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myLB"}), .01);
  assertEquals(2.0, registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "b", "myLB"}), .01);
  assertNull(registry.getSampleValue("aws_elb_request_count_average", new String[]{"job", "instance", "availability_zone", "load_balancer_name"}, new String[]{"aws_elb", "", "a", "myOtherLB"}));
}
 
Example #14
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 6 votes vote down vote up
private List<List<Dimension>> permuteDimensions(List<String> dimensions, Map<String, List<String>> dimensionValues) {
    ArrayList<List<Dimension>> result = new ArrayList<List<Dimension>>();

    if (dimensions.size() == 0) {
        result.add(new ArrayList<Dimension>());
    } else {
        List<String> dimensionsCopy = new ArrayList<String>(dimensions);
        String dimensionName = dimensionsCopy.remove(dimensionsCopy.size() - 1);
        for (List<Dimension> permutation : permuteDimensions(dimensionsCopy, dimensionValues)) {
            for (String dimensionValue : dimensionValues.get(dimensionName)) {
                Dimension dimension = new Dimension();
                dimension.setValue(dimensionValue);
                dimension.setName(dimensionName);
                ArrayList<Dimension> permutationCopy = new ArrayList<Dimension>(permutation);
                permutationCopy.add(dimension);
                result.add(permutationCopy);
            }
        }
    }

    return result;
}
 
Example #15
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 #16
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 #17
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 #18
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 #19
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 #20
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 #21
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 #22
Source File: CloudWatchRecorderTest.java    From swage with Apache License 2.0 6 votes vote down vote up
private MetricDatum makeDatum(
        final String id,
        final String name,
        final double sum,
        final double min,
        final double max,
        final int count,
        final StandardUnit unit)
{
    MetricDatum md = new MetricDatum().withMetricName(name).withUnit(unit);

    final StatisticSet statSet = new StatisticSet()
            .withSampleCount(Double.valueOf(count))
            .withSum(sum)
            .withMinimum(min)
            .withMaximum(max);
    md.setStatisticValues(statSet);

    List<Dimension> dimensions = new ArrayList<>(1);
    Dimension trace = new Dimension().withName(ContextData.ID.name).withValue(id);

    dimensions.add(trace);
    md.setDimensions(dimensions);

    return md;
}
 
Example #23
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
private List<List<Dimension>> listDimensions(MetricRule rule, List<String> tagBasedResourceIds, AmazonCloudWatch cloudWatchClient) {
  List<List<Dimension>> dimensions = new ArrayList<List<Dimension>>();
  if (rule.awsDimensions == null) {
    dimensions.add(new ArrayList<Dimension>());
    return dimensions;
  }

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

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

  return dimensions;
}
 
Example #24
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
private List<List<Dimension>> getDimensions(MetricRule rule, List<String> tagBasedResourceIds, AmazonCloudWatch cloudWatchClient) {
    if (
            rule.awsDimensions != null &&
            rule.awsDimensionSelect != null &&
            rule.awsDimensions.size() > 0 &&
            rule.awsDimensions.size() == rule.awsDimensionSelect.size() &&
            rule.awsDimensionSelect.keySet().containsAll(rule.awsDimensions) &&
            rule.awsTagSelect == null
    ) {
        // The full list of dimensions is known so no need to request it from cloudwatch.
        return permuteDimensions(rule.awsDimensions, rule.awsDimensionSelect);
    } else {
        return listDimensions(rule, tagBasedResourceIds, cloudWatchClient);
    }
}
 
Example #25
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 #26
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 #27
Source File: CloudWatchCollector.java    From cloudwatch_exporter with Apache License 2.0 5 votes vote down vote up
private List<List<Dimension>> getDimensions(MetricRule rule, List<String> tagBasedResourceIds, AmazonCloudWatch cloudWatchClient) {
    if (
            rule.awsDimensions != null &&
            rule.awsDimensionSelect != null &&
            rule.awsDimensions.size() > 0 &&
            rule.awsDimensions.size() == rule.awsDimensionSelect.size() &&
            rule.awsDimensionSelect.keySet().containsAll(rule.awsDimensions) &&
            rule.awsTagSelect == null
    ) {
        // The full list of dimensions is known so no need to request it from cloudwatch.
        return permuteDimensions(rule.awsDimensions, rule.awsDimensionSelect);
    } else {
        return listDimensions(rule, tagBasedResourceIds, cloudWatchClient);
    }
}
 
Example #28
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 #29
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 #30
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);
}