Code Monkey home page Code Monkey logo

cloudwatch-embedded-metrics's Introduction

npm verison npm bundle size npm downloads

CloudWatch Embedded Metrics

CloudWatch embedded metric format (EMF) is an opinionated logging format used CloudWatch to enable automatic creation of CloudWatch Metrics from log events. This library provides a utility funcion emf to create EMF-compliant objects.

For more information about EMF see the specification.

Usage

The emf utility can be used to create EMF-compliant objects for structured logging. Note that emf is only a formatting tool and does not send events to AWS.

Example: report metrics when adding an item to a shopping cart

Metrics can be published to multiple namespaces and across multiple dimensions.

Additional data can be attached to the EMF event via the properties parameter. This provides a simple way to collocate high-cardinality data with metric data in log events.

import { emf, Unit } from "cloudwatch-embedded-metrics";

const requestParams = {
  userId: "user-abc123",
  cartId: "cart-012345",
  items: [{ itemId: "item-000123", quanitity: 1 }],
};

const response = {
  duration: 123,
  success: true,
}

const event = emf({
  namespaces: ["ns-perf", "ns-err"],
  dimensions: {
    "ns-perf": [["serviceName", "endpointName"]],
    "ns-err": [["serviceName"], ["serviceName", "endpointName"]],
  },
  dimensionTargets: {
    serviceName: "shopping-cart-service",
    endpointName: "add-items",
  },
  metrics: {
    "ns-perf": [["duration", Unit.Milliseconds]],
    "ns-err": [["errors", Unit.Count]],
  },
  metricTargets: {
    duration: response.duration,
    errors: response.success ? 0 : 1,
  },
  properties: {
    userId: requestParams.userId,
    cartId: requestParams.cartId,
    itemIds: requestParams.items.map(({itemId}) => itemId),
  },
});

console.log(event);

In this example:

  • a duration metric is added to the ns-perf namespace and by association to the ["serviceName", "endpointName"] dimension set
  • an errors metric is added to the ns-err namespace and by association to both the ["serviceName"] and ["serviceName", "endpointName"] dimension sets

This event would therefore lead to the creation of 3 new metrics in CloudWatch.

In general, the number of metrics added to a namespace $n$ is given by the number of metrics associated with that namespace $n_M$ multiplied by the number of dimension sets associated with that namespace $n_D$.

The total number of metrics $N$ is the sum over all namespaces.

$$N = \sum_i n_i = \sum_i (n_M)_i (n_D)_i$$

Example: creating metric generators

You can also create metric generators using the createMetricGenerator function. Generators can be constructed once and imported across your application, providing a standard set of utilities to generate metrics with the correct dimensions and namespaces.

// metric-generator.ts

import { createMetricGenerator, Unit } from "cloudwatch-embedded-metrics";

export const generator = createMetricGenerator({
  namespaces: ["ns"],
  dimensions: { ns: [["dim"]] },
  metrics: { ns: [["met", Unit.Bits]] },
});


// some-other-file.ts

import { generator } from "./path/to/metric-generator";

const event = generator({
  dimensionTargets: { dim: "hello" },
  metricTargets: { met: 10 },
});

console.log(event);

cloudwatch-embedded-metrics's People

Contributors

jxdp avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.