This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 15k traffic Daily!!!

How to report CloudWatch metrics without AWS SDK


Generally when writing a Lambda operate, we have to ship a custom metric to CloudWatch Metrics. This could possibly be a technical metric (e.g., request time to an exterior service) or a metric a few enterprise course of (e.g., variety of consumer logins). The apparent means to do that is to make use of the putMetricData method from the AWS SDK:

const AWS = require("aws-sdk");
const cloudwatch = new AWS.CloudWatch();

exports.handler = async () => {
  // enterprise logic

  const metric = {
    Namespace: "Service1",
    MetricData: [
      {
        MetricName: "loginAttempts",
        Dimensions: [
          {
            Name: "tenant",
            Value: "client1",
          },
        ],
        Unit: "Rely",
        Worth: 1,
      },
    ],
  };

  await cloudwatch.putMetricData(metric).promise();

  // extra enterprise logic
};
Enter fullscreen mode

Exit fullscreen mode

This answer is adequate if we often ship such metrics. The issue begins if we need to ship a variety of these metrics, from completely different locations within the operate code. Calling the putMetricData methodology the identical means as calling some other methodology from AWS SDK, will increase the runtime of the operate, thus growing its value. Apart from, we will solely ship 40kB of information in a single name.
On the finish of 2019, AWS has enabled metrics reporting with out utilizing AWS SDK.



Embedded Metric Format

This may be completed by logging the info to stdout within the particular format referred to as Embedded Metric Format. For instance:

{
  "_aws": {
    "Timestamp": 1579211886742,
    "CloudWatchMetrics": [
      {
        "Dimensions": [["tenant"]],
        "Metrics": [
          {
            "Name": "loginAttempts",
            "Unit": "Count"
          }
        ],
        "Namespace": "Service1"
      }
    ]
  },
  "loginAttempts": 1,
  "tenant": "client1"
}
Enter fullscreen mode

Exit fullscreen mode

To make it simpler to create such an object, AWS has offered libraries for Node.js, Python, Java, and .NET. The above instance utilizing the AWS SDK can now be written as follows:

const { createMetricsLogger, Unit } = require("aws-embedded-metrics");

exports.handler = async (occasion, context) => {
  // enterprise logic

  const metrics = createMetricsLogger();
  metrics.setNamespace("Service1");
  metrics.putMetric("loginAttempts", 1, Unit.Rely);
  metrics.setDimensions({ tenant: "client1" });
  await metrics.flush();

  // extra enterprise logic
};
Enter fullscreen mode

Exit fullscreen mode

There are not any community calls to AWS concerned, so the operate name time doesn’t improve. The information is logged to stdout and the remainder is dealt with by CloudWatch, processing it and publishing it as metrics. Moreover, our operate doesn’t want the cloudwatch:PutMetricData permission.

The aws-embedded-metrics library additionally supplies a wrapper for Lambda features that eliminates the necessity to manually name the flush() methodology. This enables reporting metrics to be unfold all through the code and sending them to stdout will solely occur as soon as on the finish of the Lambda execution.

const { metricScope } = require("aws-embedded-metrics");

exports.handler = metricScope((metrics) => async () => {
  // enterprise logic

  metrics.setNamespace("Service2");
  metrics.putMetric("loginAttempts", 1, Unit.Rely);
  metrics.setDimensions({ tenant: "client2" });
  metrics.setProperty("RequestId", context.awsRequestId);

  // extra enterprise logic
});
Enter fullscreen mode

Exit fullscreen mode

Moreover, utilizing the setProperty methodology, we will add non-obligatory parameters that we will later seek for in CloudWatch Logs Insights.

In abstract, with Embedded Metric Format, we will optimally ship a variety of customized metrics to the CloudWatch service with out extending the operate period.




Keep on high of your logs 🌩

Cloudash screenshot

Introducing Cloudash, a desktop app for monitoring your serverless companies efficiency, invocations, errors and extra.

Did a manufacturing incident occur final week? Or 20 seconds in the past? With Cloudash you’ll be able to search, filter and browse your serverless logs and metrics effortlessly.

Seek for no matter you need, everytime you need. Cloudash comes with built-in filtering capabilities enabling to unravel your issues sooner than ever earlier than.

Get began here.

The Article was Inspired from tech community site.
Contact us if this is inspired from your article and we will give you credit for it for serving the community.

This Banner is For Sale !!
Get your ad here for a week in 20$ only and get upto 10k Tech related traffic daily !!!

Leave a Reply

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

Want to Contribute to us or want to have 15k+ Audience read your Article ? Or Just want to make a strong Backlink?