From the course: Monitoring AWS with CloudWatch

Collecting custom metrics

- [Instructor] In this demo, we are going to collect some custom metrics from our application and send them to CloudWatch. We can publish our own metrics to CloudWatch using the AWS ELI or the API. For this demo, we are going to do it via the API and integrate it in our demo application. Make sure you download the exercise files, and we'll go over it step by step. Our first step is going to be to add some dependencies, and in here, we are going to install the client CloudWatch and config resolver. The only thing different here might be the package version, as they might release new versions at the time you are watching this course. To make sure everything is the same, go ahead and use the same package version as me. Once the dependencies are in place, right click on the API folder and then open an integrated terminal. Let's go ahead and install the packages by doing mpm install. Now that the packages are installed, let's open our API folder and we are going to navigate to cloudwatchMetrics.js. In here, I've created a new file that will take care of publishing a custom metric. First, we need to import the CloudwatchClient and PutMetricDataCommand from the AWS SDK client CloudWatch. And next, we are going to need three things. First, we need a region ID, an access key, and a secret key. Let's switch to our AWS console. And in here, we are going to create a new user. In the search bar, type IAM, identity and access management. Before we create a new user, we are going to create a new policy that will give this user access to put metrics in our CloudWatch console. Let's navigate to policies. And in here, let's go ahead and create a new policy. In the service here, type CloudWatch. Let's go ahead and select a CloudWatch service. Next, in the access level, navigate to write and then select PutMetricData. Let's go ahead and move to the next step. We need to give this policy a name. Let's go with cloudwatchcustommetric. Now let's go ahead and create our policy. Once our policy is created, now we can go ahead and create a user that we are going to use to push our custom metrics. Let's navigate to users, and I've already created a user here for custommetrics, but let's go ahead and create a new one. Let's name it pushcustommetrics. We don't want to provide it access to the AWS Management console, so let's go ahead and move next. In the permission options, we are going to use attach policies directly. And now we need to find our policy that we created. Let's go ahead and change the type to customer managed. And in here, let's search for custom, and we'll find our policy, cloudwatchcustommetric. Let's go ahead and select it and then move next. In here, we do not need to change anything. In here, go ahead and make a quick review and then create a user. Once a user has been created, let's go ahead and navigate to it, and we need to navigate to security credentials. Scroll down under access keys and then create a new access key. In here, in the use case, choose local code. And finally, go ahead and check this confirmation and now click next. Let's give it the name custommetrics. And we get access key and secret key. Let's go ahead and copy this value. Let's switch back to our app, and in here, we need to navigate to the environment file and provide the value in the ACCESS_KEY_ID. Now we are going to need the secret key as well. Let's go ahead and click show and then copy. Also, make sure you specify the right AWS region. In my case, it's US East 1. Depending on your environment, make sure you specify the right region. And finally, we specify the namespace in here. Let's do CloudWatchApiCourse. And now we have specified all the values that we need. Let's switch back to our Cloudwatchmetrics.js. In here now, we get the region, the access key ID, and the secret access key. With this, we create a new CloudWatch client where we specify the details in here. And now finally, we have the function where we publish a metric. First, we define the parameters for our metric. We are going to send metric data and the namespace. In the metric data, we specify the metric name, dimensions, timestamp, value, and the unit for the value, and we also specify the namespace. This is how it's going to be grouped in the AWS console. And finally, we do cloudwatchClient.send and then we send the data in here. And then we export this method. In the solution explorer now, navigate to our server. In here, we import the file cloudwatchMetrics, and then we use it in here where we make a call to our API. First, we specify the start time, and then we call our API, and then we specify the end time. And from this, we determine how long it took to make the request. Next, we specify the dimensions. In here, you can add as many dimensions as you need. For example, we specify the environment, endpoint, and HTTP method, but you can add more if you wish to. And finally, we do cloudWatch.publishMetric. The metric name is ResponseTime, and then we specify the units, which is milliseconds. And finally, we specify the dimensions. This will help us query our data. Now every time we make a request, this is going to send custom metrics to our AWS CloudWatch console. Let's save the changes and run this. Make sure you save the changes, and now let's go ahead and run it. All you have to do is node server.js. Once application is up and running, let's go ahead and navigate to it. Make sure you reload the app to get the latest version, and now let's go ahead and send some requests. If you look at the console, you should see something like this. "Successfully published metric to CloudWatch." If there are any errors, you will see the error in here as well. As you can see in here, we do try catch, and then we log this to the console. If there are any errors, make sure, in the environment file, you typed this stuff correctly. Now let's navigate to our AWS CloudWatch console to find metrics for the namespace that we have created, CloudWatchApiCourse/Performance. In the CloudWatch console, let's navigate to all metrics. And now we have CloudWatchApiCourse/Performance. Let's open it, and then we have the dimensions, endpoint, environment, and HTTP method. And in here, we only have one metric, which is the response time. Let's go ahead and select it. Now let's navigate to our graphed metrics. Instead of average, as the value here is going to be very small, let's go ahead and do sum. And instead of five minutes, as it's not been five minutes yet, let's go ahead and change these to five seconds. And now we see some data in our graph. As you can see, using custom metrics enables each business to collect vital business decision-making data that helps them better understand their users and applications. Depending on your business's need, you can collect a number of data points. That will ultimately create enough data points for better decision making in your day-to-day operations.

Contents