Applicable when
- Custom metrics need to be stored in CloudWatch so that data points can be added to them for our requirements (for example, frontend observability)
Implementation
You can use either the CLI or SDK if you would like to publish custom metrics manually. I could not find a similar option to do this via the AWS console at the time of writing this article (5th Nov, 2020).
In CLI,
aws cloudwatch put-metric-data --metric-name Metric1 --namespace MyNameSpace --unit Bytes --value 231434333 --dimensions InstanceId=1-23456789,InstanceType=m1.small --profile volt
You can the example for this usage in the AWS Docs here
In SDK,
import { CloudWatch, config, SharedIniFileCredentials } from "aws-sdk";
import { PutMetricDataInput } from "aws-sdk/clients/cloudwatch";
process.env.AWS_SDK_LOAD_CONFIG = "true";
process.env.AWS_PROFILE = "volt";
const credentials = new SharedIniFileCredentials({ profile: "volt" });
config.credentials = credentials;
const cloudwatch = new CloudWatch({ region: "us-east-1" });
const createCustomMetrics = async () => {
try {
const params: PutMetricDataInput = {
Namespace: "MyNameSpace",
MetricData: [
{
MetricName: "Metric2",
Dimensions: [
{
Name: "InstanceType",
Value: "t2.micro",
},
],
Value: 1,
},
],
};
const data = await cloudwatch.putMetricData(params).promise();
console.log("DATA:::", data);
} catch (err) {
throw Error(`cloudwatch.putMetricData failed: \n${err}`);
}
};
export default createCustomMetrics;
Once these metrics are created, you will find them in "CloudWatch => Metrics", under "Custom NameSpaces"
Comments
0 comments
Please sign in to leave a comment.