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

mikrometric

v1.0.11

Published

mikrometric is a Lambda-oriented lightweight wrapper for producing AWS CloudWatch Embedded Metric Format-compatible metric logs.

Downloads

156

Readme

mikrometric

MikroMetric is a Lambda-oriented lightweight wrapper for producing AWS CloudWatch Embedded Metric Format-compatible metric logs.

Build Status

Quality Gate Status

codecov

Maintainability


MikroMetric is the quickest way to get going with AWS metrics (CloudWatch Embedded Metric Format):

  • Adapted out-of-the box for serverless Lambda environments
  • Zero config besides setting a namespace and service name
  • Familiar syntax using methods like putDimension(), putMetric(), setProperty(), and flush()
  • Easier to understand than raw EMF and less chance of breaking it (or your computer, trying to get it right!)
  • Less bloated than the other major libraries
  • Cuts out all the stuff you won't need in cloud/serverless
  • Tiny (~2.2 KB gzipped)
  • Has only one dependency, aws-metadata-utils (for picking out metadata)
  • Has 100% test coverage

Usage

Basic importing and usage

// ES5 format
const { MikroMetric } = require('mikrometric');
// ES6 format
import { MikroMetric } from 'mikrometric';

// Minimal usage
const mikroMetric = MikroMetric.start({ namespace: 'MyNamespace', serviceName: 'MyServiceName' });

// Using the AWS `event` and `context` objects
const mikroMetric = MikroMetric.start({
  namespace: 'MyNamespace',
  serviceName: 'MyServiceName',
  event,
  context
});

mikroMetric.putDimension('user', 'Sam Person');
mikroMetric.putMetric('duration', 83, 'Milliseconds');
mikroMetric.setProperty('correlationId', '8d5a0ba6-05e0-4c9b-bc7c-9164ea1bdedd');

mikroMetric.flush();

Provided full event and context objects, your metric log will look something like this in CloudWatch Logs:

{
  "_aws": {
    "Timestamp": 1668191447669,
    "CloudWatchMetrics": [
      { "Namespace": "MyNamespace", "Dimensions": [["service"]], "Metrics": [] }
    ]
  },
  "accountId": "123412341234",
  "correlationId": "6c933bd2-9535-45a8-b09c-84d00b4f50cc",
  "functionMemorySize": "1024",
  "functionName": "somestack-FunctionName",
  "functionVersion": "$LATEST",
  "id": "3be48135-927a-4f71-ac7e-94bcd30723ba",
  "region": "eu-north-1",
  "resource": "/functionName",
  "runtime": "AWS_Lambda_nodejs16.x",
  "service": "MyService",
  "stage": "shared",
  "timestamp": "2022-11-11T18:30:47.669Z",
  "timestampEpoch": "1668191447669",
  "timestampRequest": "1657389598171",
  "user": "some user",
  "viewerCountry": "SE"
}

MikroMetric will grab as many values as it can from the event and context objects. You will of course get fewer (or none) of the dynamic metadata values if these objects are not provided.

The namespace and serviceName may be passed in either manually at init-time (as above), or be inferred via environment variables (see below). When initializing, some representation of these values must exist or an error will be thrown.

You can now use CloudWatch Logs Insights and CloudWatch Metrics to either search your logs or visualize your metrics.

For more learning resources regarding AWS observability solutions, see the One Observability Workshop, especially the page on EMF.

Setting custom static metadata

import { MikroMetric } from 'mikrometric';

// Use custom metadata
const metadataConfig = {
  version: 1,
  hostPlatform: 'aws',
  owner: 'MyCompany',
  domain: 'MyDomain',
  system: 'MySystem',
  team: 'MyTeam',
  tags: ['backend', 'typescript', 'api', 'serverless', 'my-service'],
  dataSensitivity: 'proprietary'
};

const mikroMetric = MikroMetric.start({
  namespace: 'MyNamespace',
  serviceName: 'MyServiceName',
  metadataConfig
});

Creating metric logs

Put a dimension

mikroMetric.putDimension('user', 'Sam Person');

Put a metric

mikroMetric.putMetric('duration', 83, 'Milliseconds');

Set a property

mikroMetric.setProperty('correlationId', '8d5a0ba6-05e0-4c9b-bc7c-9164ea1bdedd');

Flush

"Flushing", or sending, logs is easy:

mikroMetric.flush();

Because this is assumed to be within an AWS Lambda context, then flushing is simply a case of logging out the metric data.

Flushing will reset the MikroMetric instance. See Reset below.

Other features

Set the correlation ID manually

mikroMetric.setCorrelationId('abc123');

Set the namespace manually

mikroMetric.setNamespace('MyNewNamespace');

Get namespace

mikroMetric.getNamespace();

Get service name

mikroMetric.getServiceName();

Reset

This resets the metric log context and therefore erases any dimensions, metrics, and properties.

mikroMetric.reset();

Configuration

Setting the namespace and/or service name with environment variables

You can set MIKROMETRIC_NAMESPACE and/or MIKROMETRIC_SERVICE_NAME respectively as environment variables to use these at init time.

Any values manually passed in will always take precedence.

Behavior of output

Output fields will be spread (and potentially deduplicating same-named properties) in the following order:

  1. Dynamic metadata (AWS)
  2. Custom static metadata
  3. Properties

A higher number means that fields in that category will persist if any fields have same names in lower-numbered categories.

License

MIT. See LICENSE file.