Search in sources :

Example 1 with DimensionFilter

use of com.amazonaws.services.cloudwatch.model.DimensionFilter 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)

Aggregations

AmazonCloudWatchClient (com.amazonaws.services.cloudwatch.AmazonCloudWatchClient)1 DimensionFilter (com.amazonaws.services.cloudwatch.model.DimensionFilter)1 ListMetricsRequest (com.amazonaws.services.cloudwatch.model.ListMetricsRequest)1 Metric (com.amazonaws.services.cloudwatch.model.Metric)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 ProducerTemplate (org.apache.camel.ProducerTemplate)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 CwConstants (org.apache.camel.component.aws.cw.CwConstants)1 Deployment (org.jboss.arquillian.container.test.api.Deployment)1 Arquillian (org.jboss.arquillian.junit.Arquillian)1 ShrinkWrap (org.jboss.shrinkwrap.api.ShrinkWrap)1 EmptyAsset (org.jboss.shrinkwrap.api.asset.EmptyAsset)1 JavaArchive (org.jboss.shrinkwrap.api.spec.JavaArchive)1 Assert (org.junit.Assert)1 Assume (org.junit.Assume)1