Search in sources :

Example 1 with ListMetricsRequest

use of com.amazonaws.services.cloudwatch.model.ListMetricsRequest in project iep by Netflix.

the class PaginationTest method cloudwatch.

@Test
public void cloudwatch() throws Exception {
    SortedSet<String> pages = newPageSet(5);
    final Iterator<String> reqIt = pages.iterator();
    final Iterator<String> resIt = pages.iterator();
    Function<ListMetricsRequest, ListMetricsResult> f = r -> {
        if (r.getNextToken() != null) {
            Assert.assertEquals(reqIt.next(), r.getNextToken());
        }
        return new ListMetricsResult().withNextToken(resIt.hasNext() ? resIt.next() : null);
    };
    Publisher<ListMetricsResult> publisher = Pagination.createPublisher(new ListMetricsRequest(), f);
    Iterable<String> iter = Flowable.fromPublisher(publisher).filter(r -> r.getNextToken() != null).map(ListMetricsResult::getNextToken).blockingIterable();
    SortedSet<String> results = new TreeSet<>();
    for (String s : iter) {
        results.add(s);
    }
    Assert.assertEquals(pages, results);
    Assert.assertFalse(reqIt.hasNext());
}
Also used : ListHostedZonesResult(com.amazonaws.services.route53.model.ListHostedZonesResult) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) SortedSet(java.util.SortedSet) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ListMetricsResult(com.amazonaws.services.cloudwatch.model.ListMetricsResult) DescribeLoadBalancersRequest(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersRequest) Function(java.util.function.Function) TreeSet(java.util.TreeSet) PutMetricDataRequest(com.amazonaws.services.cloudwatch.model.PutMetricDataRequest) DescribeTargetGroupsRequest(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsRequest) Flowable(io.reactivex.Flowable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) Map(java.util.Map) DescribeAutoScalingGroupsResult(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult) ListClustersResult(com.amazonaws.services.elasticmapreduce.model.ListClustersResult) DescribeTargetGroupsResult(com.amazonaws.services.elasticloadbalancingv2.model.DescribeTargetGroupsResult) Iterator(java.util.Iterator) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Publisher(org.reactivestreams.Publisher) DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) DescribeAutoScalingGroupsRequest(com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest) ListResourceRecordSetsResult(com.amazonaws.services.route53.model.ListResourceRecordSetsResult) ListHostedZonesRequest(com.amazonaws.services.route53.model.ListHostedZonesRequest) ListClustersRequest(com.amazonaws.services.elasticmapreduce.model.ListClustersRequest) DescribeLoadBalancersResult(com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult) PutMetricDataResult(com.amazonaws.services.cloudwatch.model.PutMetricDataResult) Assert(org.junit.Assert) ListResourceRecordSetsRequest(com.amazonaws.services.route53.model.ListResourceRecordSetsRequest) TreeSet(java.util.TreeSet) ListMetricsResult(com.amazonaws.services.cloudwatch.model.ListMetricsResult) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) Test(org.junit.Test)

Example 2 with ListMetricsRequest

use of com.amazonaws.services.cloudwatch.model.ListMetricsRequest in project wildfly-camel by wildfly-extras.

the class CloudWatchIntegrationTest method testKeyValueOperations.

@Test
public void testKeyValueOperations() throws Exception {
    AmazonCloudWatchClient cwClient = provider.getClient();
    Assume.assumeNotNull("AWS client not null", cwClient);
    List<Metric> staleMetrics = cwClient.listMetrics(new ListMetricsRequest().withNamespace(NAMESPACE)).getMetrics().stream().filter(metric -> !metric.getMetricName().startsWith(CloudWatchIntegrationTest.class.getSimpleName()) || System.currentTimeMillis() - AWSUtils.toEpochMillis(metric.getMetricName()) > // 
    AWSUtils.TWO_WEEKS).collect(Collectors.toList());
    if (staleMetrics.size() > 0) {
        Assert.fail("Found '" + CloudWatchIntegrationTest.class.getName() + "-*' metrics older than two weeks: " + staleMetrics);
    }
    WildFlyCamelContext camelctx = new WildFlyCamelContext();
    camelctx.getNamingContext().bind("cwClient", cwClient);
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:metrics").to("aws-cw://" + NAMESPACE + "?amazonCwClient=#cwClient");
        }
    });
    camelctx.start();
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(CwConstants.METRIC_NAME, METRIC_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_NAME, DIM_NAME);
        headers.put(CwConstants.METRIC_DIMENSION_VALUE, DIM_VALUE);
        ListMetricsRequest request = new ListMetricsRequest().withNamespace(NAMESPACE).withMetricName(METRIC_NAME).withDimensions(new DimensionFilter().withName(DIM_NAME).withValue(DIM_VALUE));
        List<Metric> metrics = Collections.emptyList();
        ProducerTemplate producer = camelctx.createProducerTemplate();
        for (int i = 100; i < 105 && metrics.size() == 0; i++) {
            producer.sendBodyAndHeaders("direct:metrics", new Double(i), headers);
            metrics = cwClient.listMetrics(request).getMetrics();
            System.out.println("metrics #" + i + ": " + metrics);
            Thread.sleep(1000);
        }
    // It may take several minutes for the metric to show up
    // Assert.assertEquals(1, metrics.size());
    } finally {
        camelctx.stop();
    }
}
Also used : ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) EmptyAsset(org.jboss.shrinkwrap.api.asset.EmptyAsset) AWSUtils(org.wildfly.camel.test.common.aws.AWSUtils) Arquillian(org.jboss.arquillian.junit.Arquillian) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) CloudWatchClientProducer(org.wildfly.camel.test.aws.subA.CloudWatchClientProducer) CloudWatchClientProvider(org.wildfly.camel.test.aws.subA.CloudWatchClientProducer.CloudWatchClientProvider) Inject(javax.inject.Inject) CamelAware(org.wildfly.extension.camel.CamelAware) Map(java.util.Map) BasicCredentialsProvider(org.wildfly.camel.test.common.aws.BasicCredentialsProvider) ProducerTemplate(org.apache.camel.ProducerTemplate) Assume(org.junit.Assume) Metric(com.amazonaws.services.cloudwatch.model.Metric) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) Test(org.junit.Test) AmazonCloudWatchClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchClient) WildFlyCamelContext(org.wildfly.extension.camel.WildFlyCamelContext) Collectors(java.util.stream.Collectors) CloudWatchUtils(org.wildfly.camel.test.common.aws.CloudWatchUtils) DimensionFilter(com.amazonaws.services.cloudwatch.model.DimensionFilter) List(java.util.List) RouteBuilder(org.apache.camel.builder.RouteBuilder) Deployment(org.jboss.arquillian.container.test.api.Deployment) JavaArchive(org.jboss.shrinkwrap.api.spec.JavaArchive) CwConstants(org.apache.camel.component.aws.cw.CwConstants) Assert(org.junit.Assert) Collections(java.util.Collections) ProducerTemplate(org.apache.camel.ProducerTemplate) AmazonCloudWatchClient(com.amazonaws.services.cloudwatch.AmazonCloudWatchClient) RouteBuilder(org.apache.camel.builder.RouteBuilder) HashMap(java.util.HashMap) DimensionFilter(com.amazonaws.services.cloudwatch.model.DimensionFilter) Metric(com.amazonaws.services.cloudwatch.model.Metric) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) WildFlyCamelContext(org.wildfly.extension.camel.WildFlyCamelContext) Test(org.junit.Test)

Example 3 with ListMetricsRequest

use of com.amazonaws.services.cloudwatch.model.ListMetricsRequest in project aws-doc-sdk-examples by awsdocs.

the class ListMetrics method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a metric name and metric namespace\n" + "Ex: ListMetrics <metric-name> <metric-namespace>\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String name = args[0];
    String namespace = args[1];
    final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient();
    ListMetricsRequest request = new ListMetricsRequest().withMetricName(name).withNamespace(namespace);
    boolean done = false;
    while (!done) {
        ListMetricsResult response = cw.listMetrics(request);
        for (Metric metric : response.getMetrics()) {
            System.out.printf("Retrieved metric %s", metric.getMetricName());
        }
        request.setNextToken(response.getNextToken());
        if (response.getNextToken() == null) {
            done = true;
        }
    }
}
Also used : AmazonCloudWatch(com.amazonaws.services.cloudwatch.AmazonCloudWatch) ListMetricsResult(com.amazonaws.services.cloudwatch.model.ListMetricsResult) ListMetricsRequest(com.amazonaws.services.cloudwatch.model.ListMetricsRequest) Metric(com.amazonaws.services.cloudwatch.model.Metric)

Aggregations

ListMetricsRequest (com.amazonaws.services.cloudwatch.model.ListMetricsRequest)3 ListMetricsResult (com.amazonaws.services.cloudwatch.model.ListMetricsResult)2 Metric (com.amazonaws.services.cloudwatch.model.Metric)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2 RunWith (org.junit.runner.RunWith)2 DescribeAutoScalingGroupsRequest (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsRequest)1 DescribeAutoScalingGroupsResult (com.amazonaws.services.autoscaling.model.DescribeAutoScalingGroupsResult)1 AmazonCloudWatch (com.amazonaws.services.cloudwatch.AmazonCloudWatch)1 AmazonCloudWatchClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchClient)1 DimensionFilter (com.amazonaws.services.cloudwatch.model.DimensionFilter)1 PutMetricDataRequest (com.amazonaws.services.cloudwatch.model.PutMetricDataRequest)1 PutMetricDataResult (com.amazonaws.services.cloudwatch.model.PutMetricDataResult)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)1 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)1 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)1 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)1