The AWS SDK for Java 1.x has entered maintenance mode as of July 31, 2024,
and will reach end-of-support
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 AmazonCloudWatchClient’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.
Note
You cannot specify a namespace that begins with "
AWS
". Namespaces that begin with
"
AWS
" are reserved for use by Amazon Web Services products.
Imports
import com.amazonaws.services.cloudwatch.AmazonCloudWatch; import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder; import com.amazonaws.services.cloudwatch.model.Dimension; import com.amazonaws.services.cloudwatch.model.MetricDatum; import com.amazonaws.services.cloudwatch.model.PutMetricDataRequest; import com.amazonaws.services.cloudwatch.model.PutMetricDataResult; import com.amazonaws.services.cloudwatch.model.StandardUnit;
Code
final AmazonCloudWatch cw = AmazonCloudWatchClientBuilder.defaultClient(); Dimension dimension = new Dimension() .withName("UNIQUE_PAGES") .withValue("URLS"); MetricDatum datum = new MetricDatum() .withMetricName("PAGES_VISITED") .withUnit(StandardUnit.None) .withValue(data_point) .withDimensions(dimension); PutMetricDataRequest request = new PutMetricDataRequest() .withNamespace("SITE/TRAFFIC") .withMetricData(datum); PutMetricDataResult response = cw.putMetricData(request);
More Information
-
Using Amazon CloudWatch Metrics in the Amazon CloudWatch User Guide.
-
AWS Namespaces in the Amazon CloudWatch User Guide.
-
PutMetricData in the Amazon CloudWatch API Reference.