AWS SDK for Java version 2
Developer Guide

Publishing Custom Metric Data

A number of AWS services publish their own metrics in namespaces beginning with "AWS/" You can also publish custom metric data using your own namespace (as long as it doesn't begin with "AWS/").

Publish Custom Metric Data

To publish your own metric data, call the CloudWatchClient's putMetricData method with a PutMetricDataRequest. The PutMetricDataRequest must include the custom namespace to use for the data, and information about the data point itself in a MetricDatum object.


You cannot specify a namespace that begins with "AWS/". Namespaces that begin with "AWS/" are reserved for use by Amazon Web Services products.


package com.example.cloudwatch; import; import; import; import; import; import;


Double data_point = Double.parseDouble(args[0]); CloudWatchClient cw = CloudWatchClient.builder().build(); Dimension dimension = Dimension.builder() .name("UNIQUE_PAGES") .value("URLS").build(); MetricDatum datum = MetricDatum.builder() .metricName("PAGES_VISITED") .unit(StandardUnit.NONE) .value(data_point) .dimensions(dimension).build(); PutMetricDataRequest request = PutMetricDataRequest.builder() .namespace("SITE/TRAFFIC") .metricData(datum).build(); PutMetricDataResponse response = cw.putMetricData(request); System.out.printf("Successfully put data point %f", data_point);

See the complete example on GitHub.

More Information