npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aws-embedded-metrics-express

v1.0.0

Published

AWS Embedded Metrics Express Middleware

Downloads

764

Readme

AWS CloudWatch Embedded Metrics Express Middleware

CodeBuild npm

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):

  • RequestDuration in milliseconds.
  • StatusCodeXXX for each response HTTP status code.
  • ClientError if response HTTP status code starts with 4XX.
  • ServerError if response HTTP status code starts with 5XX.

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-metrics

Usage

// 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 to true. Whether to publish metrics for each HTTP response status code. Metrics will appear in CloudWatch like e.g. 'StatusCode200'.
  • clientErrorMetric (boolean) (optional): Defaults to true. Whether to publish ClientError metric based on response HTTP status code. (Useful if you want to calculate your availability metric)
  • serverErrorMetric (boolean) (optional): Defaults to true. Whether to publish ServerError metric based on response HTTP status code. (Useful if you want to calculate your availability metric)
  • durationMetric (boolean) (optional): Defaults to true. Whether to publish RequestDuration metric based on the time between the request coming into metricsMiddleware and when the response has finished being written out to the connection, in milliseconds.
  • ipProperty (boolean) (optional): Defaults to false. Whether to set RemoteAddress property to EMF logs.
  • userAgentProperty (boolean) (optional): Defaults to false. Whether to set UserAgent property to EMF logs.

Development

Building

This project uses Volta to pin the currently supported version of node.

npm i && npm run build

Testing

Unit tests which can be run using the following commands:

npm test
# or
npm run watch

Formatting

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.