ѕтƒ
ѕтƒ

Reputation: 3637

Can I create Cloudwatch custom metrics with Java?

Is it possible to create AWS CloudWatch custom metrics using Java SDK provided by AWS?

The developer guide talks about publishing custom metrics through Command line tools.
Is it possible with the Java SDK? If yes, please provide the links or tutorials for that.

Upvotes: 6

Views: 8400

Answers (2)

dassum
dassum

Reputation: 5093

The below code can be used to Publish Custom Metrics in AWS CloudWatch using JAVA.

AmazonCloudWatch amazonCloudWatch = 
AmazonCloudWatchClientBuilder.standard().withEndpointConfiguration(
    new AwsClientBuilder.EndpointConfiguration("monitoring.us-west-1.amazonaws.com","us-west-1")).build();

PutMetricDataRequest putMetricDataRequest = new PutMetricDataRequest();
putMetricDataRequest.setNamespace("CUSTOM/SQS");

MetricDatum metricDatum1 = new MetricDatum().withMetricName("MessageCount").withDimensions(new Dimension().withName("Personalization").withValue("123"));
        metricDatum1.setValue(-1.00);
        metricDatum1.setUnit(StandardUnit.Count);
        putMetricDataRequest.getMetricData().add(metricDatum1);

PutMetricDataResult result = amazonCloudWatch.putMetricData(putMetricDataRequest);

Only thing to remember is to include aws-java-sdk-cloudwatch

Upvotes: 6

Simon Curd
Simon Curd

Reputation: 840

Yep, it's absolutely possible to create custom metrics programmatically using the AWS SDK. Here's a link to the CloudWatch client as part of the AWS SDK for Java:

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/cloudwatch/AmazonCloudWatchClient.html

If I remember correctly, you just start putting data into CloudWatch using the putMetricData(..) method, and it will start showing up (after a short delay of a minute or two).

Upvotes: 4

Related Questions