Getting started
Metrics JS is an open source project for isolated instrumentation of independent modules. You can measure the things you feel are important, such as:
- The time it takes to complete an operation.
- The number of incoming requests.
- The CPU load on the server.
Let’s go through an example where we produce and read some metrics.
Install dependencies
First, install these packages:
MetricsJS consists of three main parts:
- A client, which is the interface to produce metrics.
- A guard, which helps avoid excessive metric permutation creation in a stream.
- A consumer, which takes one or more metrics streams and sends data to a service such as Prometheus.
The consumer and guard is something you typically set up once in the root of your application, while the client is used throughout your codebase (or that of your dependencies).
Generate metrics
The client supports 4 types of metric creation use cases:
For instance, to create a counter:
See the client reference for API documentation and more examples.
Create a consumer
Create a Prometheus consumer singleton in the root of your application, and add a guard:
Pipe client data to the consumer
MetricsJS works on streams, so you need to pipe the client data to the consumer:
Any number of streams can be piped to the consumer. In other words:
- You can have multiple clients in your application.
- Your dependencies can expose MetricsJS clients.
See the client reference for code examples.
Read the metrics
Finally, expose the metrics on an endpoint:
This endpoint can be scraped by Prometheus.