aws-embedded-metrics-express v1.0.0
AWS CloudWatch Embedded Metrics Express Middleware
This middleware for NodeJS Express framework hydrates req.metrics property with an instance of MetricLogger. This instance can be used to easily generate custom metrics from your Express server without requiring custom batching code, making blocking network requests or relying on 3rd party software.
By default, the middleware will also automatically publish the following request metrics (See Options for more metrics):
RequestDurationin milliseconds.StatusCodeXXXfor each response HTTP status code.ClientErrorif response HTTP status code starts with4XX.ServerErrorif response HTTP status code starts with5XX.
Metrics collected with this logger are then available for querying within AWS CloudWatch Log Insights
You can explore all the MetricLogger APIs following aws-embedded-metrics documentation.
Installation
npm install aws-embedded-metrics-express aws-embedded-metricsUsage
// Configure your EMF metrics settings at the start as per https://github.com/awslabs/aws-embedded-metrics-node
const { Configuration, Unit } = require("aws-embedded-metrics");
Configuration.serviceName = "MyApp";
Configuration.serviceType = "NodeJSWebApp";
Configuration.logGroupName = "LogGroupName";
Configuration.namespace = "Namespace";
const { metricsMiddleware } = require("aws-embedded-metrics-express");
const express = require("express");
const app = express();
app.use(metricsMiddleware());
app.get("/", (req, res) => {
// Add your own custom metrics like this
req.metrics.putMetric("DatabaseRequestSuccess", 1, Unit.Count);
res.send("Hello World!");
});
app.listen(3000);Options
You can supply options object to metricsMiddleware method: app.use(metricsMiddleware({options));
statusCodeMetric(boolean) (optional): Defaults totrue. Whether to publish metrics for each HTTP response status code. Metrics will appear in CloudWatch like e.g. 'StatusCode200'.clientErrorMetric(boolean) (optional): Defaults totrue. Whether to publishClientErrormetric based on response HTTP status code. (Useful if you want to calculate your availability metric)serverErrorMetric(boolean) (optional): Defaults totrue. Whether to publishServerErrormetric based on response HTTP status code. (Useful if you want to calculate your availability metric)durationMetric(boolean) (optional): Defaults totrue. Whether to publishRequestDurationmetric based on the time between the request coming intometricsMiddlewareand when the response has finished being written out to the connection, in milliseconds.ipProperty(boolean) (optional): Defaults tofalse. Whether to setRemoteAddressproperty to EMF logs.userAgentProperty(boolean) (optional): Defaults tofalse. Whether to setUserAgentproperty to EMF logs.
Development
Building
This project uses Volta to pin the currently supported version of node.
npm i && npm run buildTesting
Unit tests which can be run using the following commands:
npm test
# or
npm run watchFormatting
We use Prettier for auto-formatting. You should install the plugin for your editor-of-choice and enabled format-on-save.
License
This project is licensed under the Apache-2.0 License.
4 years ago