software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.cloudwatch.model.PutMetricDataRequest. 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: MetricsPublisherImplTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishDurationMetric() {
    final MetricsPublisherImpl providerMetricsPublisher = new MetricsPublisherImpl(providerCloudWatchProvider, loggerProxy,
                                                                                   awsAccountId, resourceTypeName);
    providerMetricsPublisher.refreshClient();

    final Instant instant = Instant.parse("2019-06-04T17:50:00Z");
    providerMetricsPublisher.publishDurationMetric(instant, Action.UPDATE, 123456);

    final ArgumentCaptor<PutMetricDataRequest> argument1 = ArgumentCaptor.forClass(PutMetricDataRequest.class);
    verify(providerCloudWatchClient).putMetricData(argument1.capture());

    final PutMetricDataRequest request = argument1.getValue();
    assertThat(request.namespace())
        .isEqualTo(String.format("%s/%s/%s", "AWS/CloudFormation", awsAccountId, "AWS/Test/TestModel"));

    assertThat(request.metricData()).hasSize(1);
    final MetricDatum metricDatum = request.metricData().get(0);
    assertThat(metricDatum.metricName()).isEqualTo("HandlerInvocationDuration");
    assertThat(metricDatum.unit()).isEqualTo(StandardUnit.MILLISECONDS);
    assertThat(metricDatum.value()).isEqualTo(123456);
    assertThat(metricDatum.timestamp()).isEqualTo(Instant.parse("2019-06-04T17:50:00Z"));
    assertThat(metricDatum.dimensions()).containsExactlyInAnyOrder(Dimension.builder().name("Action").value("UPDATE").build(),
        Dimension.builder().name("ResourceType").value(resourceTypeName).build());
}
 
Example #2
Source File: CloudWatchMetricsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testMetricsPublisher() {
    final CompletableFuture<PutMetricDataResponse> putResponseFuture = new CompletableFuture<>();
    putResponseFuture.complete(PutMetricDataResponse.builder().build());
    when(cloudWatchClient.putMetricData(any(PutMetricDataRequest.class))).thenReturn(putResponseFuture);

    List<MetricDatumWithKey<CloudWatchMetricKey>> dataToPublish = constructMetricDatumWithKeyList(25);
    List<Map<String, MetricDatum>> expectedData = constructMetricDatumListMap(dataToPublish);
    publisher.publishMetrics(dataToPublish);
    
    ArgumentCaptor<PutMetricDataRequest> argument = ArgumentCaptor.forClass(PutMetricDataRequest.class);
    Mockito.verify(cloudWatchClient, Mockito.atLeastOnce()).putMetricData(argument.capture());

    List<PutMetricDataRequest> requests = argument.getAllValues();
    Assert.assertEquals(expectedData.size(), requests.size());

    for (int i = 0; i < requests.size(); i++) {
        assertMetricData(expectedData.get(i), requests.get(i));
    }

}
 
Example #3
Source File: MetricsPublisherImplTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 6 votes vote down vote up
@Test
public void testPublishInvocationMetric() {
    final MetricsPublisherImpl providerMetricsPublisher = new MetricsPublisherImpl(providerCloudWatchProvider, loggerProxy,
                                                                                   awsAccountId, resourceTypeName);
    providerMetricsPublisher.refreshClient();

    final Instant instant = Instant.parse("2019-06-04T17:50:00Z");
    providerMetricsPublisher.publishInvocationMetric(instant, Action.UPDATE);

    final ArgumentCaptor<PutMetricDataRequest> argument1 = ArgumentCaptor.forClass(PutMetricDataRequest.class);
    verify(providerCloudWatchClient).putMetricData(argument1.capture());

    final PutMetricDataRequest request = argument1.getValue();
    assertThat(request.namespace())
        .isEqualTo(String.format("%s/%s/%s", "AWS/CloudFormation", awsAccountId, "AWS/Test/TestModel"));

    assertThat(request.metricData()).hasSize(1);
    final MetricDatum metricDatum = request.metricData().get(0);
    assertThat(metricDatum.metricName()).isEqualTo("HandlerInvocationCount");
    assertThat(metricDatum.unit()).isEqualTo(StandardUnit.COUNT);
    assertThat(metricDatum.value()).isEqualTo(1.0);
    assertThat(metricDatum.timestamp()).isEqualTo(Instant.parse("2019-06-04T17:50:00Z"));
    assertThat(metricDatum.dimensions()).containsExactlyInAnyOrder(Dimension.builder().name("Action").value("UPDATE").build(),
        Dimension.builder().name("ResourceType").value(resourceTypeName).build());
}
 
Example #4
Source File: PutMetricData.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void putMetData(CloudWatchClient cw, Double dataPoint ) {

        try {
            Dimension dimension = Dimension.builder()
                .name("UNIQUE_PAGES")
                .value("URLS").build();

            MetricDatum datum = MetricDatum.builder()
                .metricName("PAGES_VISITED")
                .unit(StandardUnit.NONE)
                .value(dataPoint)
                .dimensions(dimension).build();

            PutMetricDataRequest request = PutMetricDataRequest.builder()
                .namespace("SITE/TRAFFIC")
                .metricData(datum).build();

            PutMetricDataResponse response = cw.putMetricData(request);

        } catch (CloudWatchException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        System.out.printf("Successfully put data point %f", dataPoint);
        // snippet-end:[cloudwatch.java2.put_metric_data.main]
    }
 
Example #5
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
private MetricDatum metricDatumByDimensionFromCapturedRequest(final String dimensionValue) {
    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    final List<MetricDatum> metricData = putMetricDataRequest.metricData();

    final Optional<MetricDatum> metricDatumOptional =
            metricData
                    .stream()
                    .filter(metricDatum -> metricDatum.dimensions()
                            .contains(Dimension.builder().name(DIMENSION_NAME_TYPE).value(dimensionValue).build()))
                    .findFirst();

    if (metricDatumOptional.isPresent()) {
        return metricDatumOptional.get();
    }

    throw new IllegalStateException("Could not find MetricDatum for Dimension value: " + dimensionValue);
}
 
Example #6
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
@Test
public void shouldNotReportCounterValueDeltaWhenReportingRawCountValue() throws Exception {
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    final CloudWatchReporter cloudWatchReporter = reporterBuilder.withReportRawCountValue().build();

    cloudWatchReporter.report();
    MetricDatum metricDatum = firstMetricDatumFromCapturedRequest();
    assertThat(metricDatum.value().intValue()).isEqualTo(2);
    metricDataRequestCaptor.getAllValues().clear();

    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();

    cloudWatchReporter.report();
    metricDatum = firstMetricDatumFromCapturedRequest();
    assertThat(metricDatum.value().intValue()).isEqualTo(8);

    verify(mockAmazonCloudWatchAsyncClient, times(2)).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #7
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
@Test
public void shouldReportCounterValueDelta() throws Exception {
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    final CloudWatchReporter cloudWatchReporter = reporterBuilder.build();

    cloudWatchReporter.report();
    MetricDatum metricDatum = firstMetricDatumFromCapturedRequest();
    assertThat(metricDatum.value().intValue()).isEqualTo(2);
    metricDataRequestCaptor.getAllValues().clear();

    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();

    cloudWatchReporter.report();
    metricDatum = firstMetricDatumFromCapturedRequest();
    assertThat(metricDatum.value().intValue()).isEqualTo(6);

    verify(mockAmazonCloudWatchAsyncClient, times(2)).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #8
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 6 votes vote down vote up
@Test
public void reportMetersCountersGaugesWithZeroValuesOnlyWhenConfigured() throws Exception {
    metricRegistry.register(ARBITRARY_GAUGE_NAME, (Gauge<Long>) () -> 0L);
    metricRegistry.meter(ARBITRARY_METER_NAME).mark(0);
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc(0);
    metricRegistry.timer(ARBITRARY_TIMER_NAME).update(-1L, TimeUnit.NANOSECONDS);

    buildReportWithSleep(reporterBuilder
            .withArithmeticMean()
            .withOneMinuteMeanRate()
            .withFiveMinuteMeanRate()
            .withFifteenMinuteMeanRate()
            .withZeroValuesSubmission()
            .withMeanRate());

    verify(mockAmazonCloudWatchAsyncClient, times(1)).putMetricData(metricDataRequestCaptor.capture());

    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    final List<MetricDatum> metricData = putMetricDataRequest.metricData();
    for (final MetricDatum metricDatum : metricData) {
        assertThat(metricDatum.value()).isEqualTo(0.0);
    }
}
 
Example #9
Source File: CloudWatchMetricsPublisherTest.java    From amazon-kinesis-client with Apache License 2.0 5 votes vote down vote up
public static void assertMetricData(Map<String, MetricDatum> expected, PutMetricDataRequest actual) {
    List<MetricDatum> actualData = actual.metricData();
    for (MetricDatum actualDatum : actualData) {
        String metricName = actualDatum.metricName();
        Assert.assertNotNull(expected.get(metricName));
        Assert.assertTrue(expected.get(metricName).equals(actualDatum));
        expected.remove(metricName);
    }

    Assert.assertTrue(expected.isEmpty());
}
 
Example #10
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 5 votes vote down vote up
private List<Dimension> allDimensionsFromCapturedRequest() {
    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    final List<MetricDatum> metricData = putMetricDataRequest.metricData();
    final List<Dimension> all = new LinkedList<>();
    for (final MetricDatum metricDatum : metricData) {
        all.addAll(metricDatum.dimensions());
    }
    return all;
}
 
Example #11
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 5 votes vote down vote up
@Test
public void shouldNotReportUnchangedCounterValue() throws Exception {
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    final CloudWatchReporter cloudWatchReporter = reporterBuilder.build();

    cloudWatchReporter.report();
    MetricDatum metricDatum = firstMetricDatumFromCapturedRequest();
    assertThat(metricDatum.value().intValue()).isEqualTo(1);
    metricDataRequestCaptor.getAllValues().clear();

    cloudWatchReporter.report();

    verify(mockAmazonCloudWatchAsyncClient, times(1)).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #12
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 5 votes vote down vote up
@Test
public void neverReportMetersCountersGaugesWithZeroValues() throws Exception {
    metricRegistry.register(ARBITRARY_GAUGE_NAME, (Gauge<Long>) () -> 0L);
    metricRegistry.meter(ARBITRARY_METER_NAME).mark(0);
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc(0);

    buildReportWithSleep(reporterBuilder
            .withArithmeticMean()
            .withOneMinuteMeanRate()
            .withFiveMinuteMeanRate()
            .withFifteenMinuteMeanRate()
            .withMeanRate());

    verify(mockAmazonCloudWatchAsyncClient, never()).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #13
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 5 votes vote down vote up
@Test
public void shouldNotReportGaugeWhenMetricValueNotOfTypeNumber() throws Exception {
    metricRegistry.register(ARBITRARY_GAUGE_NAME, (Gauge<String>) () -> "bad value type");
    reporterBuilder.build().report();

    verify(mockAmazonCloudWatchAsyncClient, never()).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #14
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 5 votes vote down vote up
@Test
public void shouldNotInvokeCloudWatchClientInDryRunMode() throws Exception {
    metricRegistry.counter(ARBITRARY_COUNTER_NAME).inc();
    reporterBuilder.withDryRun().build().report();

    verify(mockAmazonCloudWatchAsyncClient, never()).putMetricData(any(PutMetricDataRequest.class));
}
 
Example #15
Source File: CloudWatchIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests handling a "request too large" error. This breaks our parser right
 * now and is therefore disabled.
 */

@Test
public void put_metric_large_data_throws_request_entity_large_exception()
        throws Exception {
    String measureName = this.getClass().getName() + System.currentTimeMillis();
    long now = System.currentTimeMillis();
    double value = 42.0;

    Collection<MetricDatum> data = new LinkedList<>();
    for (int i = ONE_WEEK_IN_MILLISECONDS; i >= 0; i -= ONE_HOUR_IN_MILLISECONDS) {
        long time = now - i;
        MetricDatum datum = MetricDatum.builder().dimensions(
                Dimension.builder().name("InstanceType").value("m1.small").build())
                                             .metricName(measureName).timestamp(Instant.now())
                                             .unit("Count").value(value).build();
        data.add(datum);
    }

    try {
        cloudwatch.putMetricData(PutMetricDataRequest.builder().namespace(
                "AWS/EC2").metricData(data).build());
        fail("Expected an error");
    } catch (SdkServiceException e) {
        assertTrue(413 == e.statusCode());
    }
}
 
Example #16
Source File: CloudWatchMeterRegistry.java    From micrometer with Apache License 2.0 5 votes vote down vote up
void sendMetricData(List<MetricDatum> metricData) throws InterruptedException {
    PutMetricDataRequest putMetricDataRequest = PutMetricDataRequest.builder()
            .namespace(config.namespace())
            .metricData(metricData)
            .build();
    CountDownLatch latch = new CountDownLatch(1);
    cloudWatchAsyncClient.putMetricData(putMetricDataRequest).whenCompleteAsync((response, t) -> {
        if (t != null) {
            if (t instanceof AbortedException) {
                logger.warn("sending metric data was aborted: {}", t.getMessage());
            } else {
                logger.error("error sending metric data.", t);
            }
        } else {
            logger.debug("published metric with namespace:{}", putMetricDataRequest.namespace());
        }
        latch.countDown();
    });
    try {
        @SuppressWarnings("deprecation")
        long readTimeoutMillis = config.readTimeout().toMillis();
        latch.await(readTimeoutMillis, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        logger.warn("metrics push to cloudwatch took longer than expected");
        throw e;
    }
}
 
Example #17
Source File: MetricsPublisherImplTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 5 votes vote down vote up
@Test
public void testPublishExceptionMetric() {
    final MetricsPublisherImpl providerMetricsPublisher = new MetricsPublisherImpl(providerCloudWatchProvider, loggerProxy,
                                                                                   awsAccountId, resourceTypeName);
    providerMetricsPublisher.refreshClient();

    final Instant instant = Instant.parse("2019-06-03T17:50:00Z");
    final RuntimeException e = new RuntimeException("some error");
    providerMetricsPublisher.publishExceptionMetric(instant, Action.CREATE, e, HandlerErrorCode.InternalFailure);

    final ArgumentCaptor<PutMetricDataRequest> argument1 = ArgumentCaptor.forClass(PutMetricDataRequest.class);
    verify(providerCloudWatchClient).putMetricData(argument1.capture());

    final PutMetricDataRequest request = argument1.getValue();
    assertThat(request.namespace())
        .isEqualTo(String.format("%s/%s/%s", "AWS/CloudFormation", awsAccountId, "AWS/Test/TestModel"));

    assertThat(request.metricData()).hasSize(1);
    final MetricDatum metricDatum = request.metricData().get(0);
    assertThat(metricDatum.metricName()).isEqualTo("HandlerException");
    assertThat(metricDatum.unit()).isEqualTo(StandardUnit.COUNT);
    assertThat(metricDatum.value()).isEqualTo(1.0);
    assertThat(metricDatum.timestamp()).isEqualTo(Instant.parse("2019-06-03T17:50:00Z"));
    assertThat(metricDatum.dimensions()).containsExactlyInAnyOrder(Dimension.builder().name("Action").value("CREATE").build(),
        Dimension.builder().name("ExceptionType").value("class java.lang.RuntimeException").build(),
        Dimension.builder().name("ResourceType").value(resourceTypeName).build(),
        Dimension.builder().name("HandlerErrorCode").value("InternalFailure").build());
}
 
Example #18
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 4 votes vote down vote up
private MetricDatum firstMetricDatumFromCapturedRequest() {
    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    return putMetricDataRequest.metricData().get(0);
}
 
Example #19
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 4 votes vote down vote up
private List<MetricDatum> allMetricDataFromCapturedRequests() {
    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    return putMetricDataRequest.metricData();
}
 
Example #20
Source File: CloudWatchReporterTest.java    From codahale-aggregated-metrics-cloudwatch-reporter with MIT License 4 votes vote down vote up
private List<Dimension> firstMetricDatumDimensionsFromCapturedRequest() {
    final PutMetricDataRequest putMetricDataRequest = metricDataRequestCaptor.getValue();
    final MetricDatum metricDatum = putMetricDataRequest.metricData().get(0);
    return metricDatum.dimensions();
}
 
Example #21
Source File: CloudWatchIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
/**
 * Tests putting metrics and then getting them back.
 */

@Test
public void put_get_metricdata_list_metric_returns_success() throws
                                                             InterruptedException {
    String measureName = this.getClass().getName() + System.currentTimeMillis();

    MetricDatum datum = MetricDatum.builder().dimensions(
            Dimension.builder().name("InstanceType").value("m1.small").build())
                                         .metricName(measureName).timestamp(Instant.now())
                                         .unit("Count").value(42.0).build();

    cloudwatch.putMetricData(PutMetricDataRequest.builder()
                                     .namespace("AWS.EC2").metricData(datum).build());

    GetMetricStatisticsResponse result =
            Waiter.run(() -> cloudwatch.getMetricStatistics(r -> r.startTime(Instant.now().minus(Duration.ofDays(7)))
                                                                  .namespace("AWS.EC2")
                                                                  .period(60 * 60)
                                                                  .dimensions(Dimension.builder().name("InstanceType")
                                                                                       .value("m1.small").build())
                                                                  .metricName(measureName)
                                                                  .statisticsWithStrings("Average", "Maximum", "Minimum", "Sum")
                                                                  .endTime(Instant.now())))
                  .until(r -> r.datapoints().size() == 1)
                  .orFailAfter(Duration.ofMinutes(2));

    assertNotNull(result.label());
    assertEquals(measureName, result.label());

    assertEquals(1, result.datapoints().size());
    for (Datapoint datapoint : result.datapoints()) {
        assertEquals(datum.value(), datapoint.average());
        assertEquals(datum.value(), datapoint.maximum());
        assertEquals(datum.value(), datapoint.minimum());
        assertEquals(datum.value(), datapoint.sum());
        assertNotNull(datapoint.timestamp());
        assertEquals(datum.unit(), datapoint.unit());
    }

    ListMetricsResponse listResult = cloudwatch.listMetrics(ListMetricsRequest.builder().build());

    boolean seenDimensions = false;
    assertTrue(listResult.metrics().size() > 0);
    for (Metric metric : listResult.metrics()) {
        assertNotNull(metric.metricName());
        assertNotNull(metric.namespace());

        for (Dimension dimension : metric.dimensions()) {
            seenDimensions = true;
            assertNotNull(dimension.name());
            assertNotNull(dimension.value());
        }
    }
    assertTrue(seenDimensions);
}
 
Example #22
Source File: MetricsPublisherImplTest.java    From cloudformation-cli-java-plugin with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void beforeEach() {
    when(providerCloudWatchProvider.get()).thenReturn(providerCloudWatchClient);
    when(providerCloudWatchClient.putMetricData(any(PutMetricDataRequest.class)))
        .thenReturn(mock(PutMetricDataResponse.class));
}