Bobcares

Push custom metrics to CloudWatch

by | Jul 22, 2021

Wondering how to push custom metrics to CloudWatch? We can help you.

Here at Bobcares, we have seen several such AWS-related queries as part of our AWS Support Services for AWS users, and online service providers.

Today, we shall see how to push custom metrics to CloudWatch.

 

How to push custom metrics to CloudWatch?

AWS services push data points to CloudWatch by default.

However, there are instances where you must calibrate the performance of your resources based on metrics that aren’t supported by AWS services.

In these cases, you can push custom metrics to CloudWatch using the unified CloudWatch agent or the API.

Today, let us see the methods followed by our Support Techs to push custom metrics.

Push custom metrics using the CloudWatch agent

The unified CloudWatch agent collects system-level metrics and sends them to CloudWatch as custom metrics.

You can use the agent to push custom metrics from:

  • Linux or Windows servers
  • Amazon Elastic Compute Cloud (Amazon EC2) instances or on-premises servers

For Linux, refer to the list of supported metrics.

Sample agent configuration file metric block for disk metrics (Linux):

"disk":
{
"measurement": [

"used_percent"
],
"resources": [

"*"
],
"drop_device":
true
}

For Windows, you can reference any counter mentioned in the Windows Performance Monitor in the agent configuration file.

Sample agent configuration file metric block for Processor Counter (Windows):

"Processor": {
"measurement": [
{"name": "% Idle Time", "rename": "CPU_IDLE", "unit": "Percent"},
"% Interrupt Time",
"% User Time",
"% Processor Time"
],
"resources": [
"*"
],
"append_dimensions": {
"d1": "win_foo",
"d2": "win_bar"
}
}

You can also use “StatsD” and “collectd” protocols to retrieve custom metrics from your applications or services.

Then, the metrics are push through the agent. StatsD is supported on both Linux and Windows servers.

Collectd is supported only on Linux servers. To use these protocols:

1. Firstly, install the CloudWatch unified agent.

2. Then, assign a role or credentials to the instance with CloudWatch permissions.

3. Next, create the CloudWatch agent configuration file.

4. Finally, start the agent.

 

Push custom metrics using PutMetricData

If your use case doesn’t support the use of the CloudWatch unified agent, you can use the PutMetricData API to push custom metrics to CloudWatch.

For example, to push connections for a specific port as a custom metric, you can retrieve the values locally and transfer them in the API:

total_conn=$(netstat -an | grep <port> | wc -l)
aws cloudwatch put-metric-data --namespace "totalconn" --metric-name <port> --dimensions Instance=<InstanceId> --value $ total_conn

Same way, you can use AWS SDKs to use the PutMetricData API and send custom metrics to CloudWatch.

Create a local script that runs periodically to send custom metrics.

The API is optimal for pushing different metrics and multiple values for the following reasons:

  • Firstly, you can use up to twenty different metrics in a single API.
  • Using the Values and Counts method, you can publish up to 150 values per metric with one PutMetricData request.
  • Next, you can use up to ten dimensions per metric.
  • Each PutMetricData request is limit to 40 KB for HTTP POST requests.

For example, instead of making a separate API call for each metric, you can push multiple metrics using a single API call:

aws cloudwatch put-metric-data --namespace "Usage Metrics" --metric-data file://metric.json

where metric.json is:

[
{
"MetricName": "DiskMetric",
"Value": <value_derived_by_some_calculation>,
"Unit": "Count"
},
{
"MetricName": "MemoryMetric",

"Value": <value_derived_by_some_calculation>,
"Unit": "Count"
}
]

[Still, stuck with the query? – We are here to help you.]

Conclusion

In short, today we saw how our Support Techs push custom metrics to CloudWatch.

 

Get 24x7 monitoring for your AWS servers

There are proven ways to get even more out of your AWS Infrastructure! Let us help you.

Spend your time in growing business and we will take care of AWS Infrastructure for you.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.