@metrics/client
A streaming metric producer. Allows producing counters, gauges, time series in a way that is independent of your metrics system so that you can produce metrics and let consumers decide how to consume them.
Additionally, you can pipe together different metrics streams before finally consuming them all in a single location.
Usage
First, instantiate a new client:
Next, use the client for instrumentation:
The client supports 4 types of metric creation use cases.
- Counters are supported via the
client.counter
method - Gauges are supported via the
client.gauge
method - Histograms are supported via the
client.histogram
method - Summaries are supported via the
client.summary
method
Lastly, the metrics stream needs to be piped to a consumer:
If you’re writing a library, you skip this step. Instead, expose the client
so your users can pipe the metrics to their chosen consumer
.
API
constructor(options)
Creates a new instance of the metrics client.
The Metrics instance inherit from Transform Stream. Due to this the instance also take all config parameters which the Transform Stream does. Please see the documentation of Transform Streams for further documentation.
options
name | description | type | default |
---|---|---|---|
id | A optional unique identifier of the instance of the Object. | string | hash |
Example
return: Duplex Stream
instance methods
.counter(options)
Creates an instance of a Counter
class which can be used to populate the metrics stream with counter metrics.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
labels | Allowed labels and their default values | object | null | false |
return: Counter
Example
counter.inc(value|options, options)
Method that when called will populate the metrics stream with a counter increment.
name | description | type | default | required |
---|---|---|---|---|
value | Value to increment the counter by | integer | 1 | false |
options | Labels and their values | object | {} | false |
Example
.gauge(options)
Creates an instance of a Gauge
class which can be used to populate the metrics stream with gauge metrics.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
labels | Allowed labels and their default values | object | null | false |
Example
gauge.set(value, options)
Method that when called will populate the metrics stream with a gauge value.
name | description | type | default | required |
---|---|---|---|---|
value | Value to set the gauge to | integer | null | true |
options | Allowed labels and their default values | object | {} | false |
Example
.histogram(options)
Creates an instance of a Histogram
class which can be used to populate the metrics stream with histogram metrics.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
buckets | Set custom buckets | number[] | null | false |
labels | Allowed labels and their default values | object | null | false |
Example
histogram.observe(value, options)
Method that when called will populate the metrics stream with a histogram value.
name | description | type | default | required |
---|---|---|---|---|
value | Value to set the gauge to | integer | null | true |
options | Allowed labels and their default values, and buckets | object | {} | false |
Example
histogram.timer(options)
Method that when called will return an end function for use in measuring the time between 2 points
name | description | type | default | required |
---|---|---|---|---|
options | Allowed labels and their default values, and buckets | object | {} | false |
Examples
.summary(options)
Creates an instance of a Summary
class which can be used to populate the metrics stream with summary metrics.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
quantiles | Set custom quantiles | number[] | null | false |
labels | Allowed labels and their default values | object | null | false |
Example
summary.observe(value, options)
Method that when called will populate the metrics stream with a summary value.
name | description | type | default | required |
---|---|---|---|---|
value | Value to set the summary to | integer | null | true |
options | Allowed labels and their default values, and quantiles | object | {} | false |
Example
summary.timer(options)
Method that when called will return an end function for use in measuring the time between 2 points
name | description | type | default | required |
---|---|---|---|---|
options | Allowed labels and their default values, and quantiles | object | {} | false |
Examples
.metric(options)
Collects a generic metric. As a minimum, a name and description for the metric must be provided.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
value | Arbitrary value for the metric (used for gauges) | string|number | null | false |
meta | Available to be used to hold any misc data. | object | null | false |
labels | Allowed labels and their default values | array[object] | null | false |
return: void
meta
meta
can be used to hold any additional information you might wish to pass on to a consumer.
It should be an object of keys and values.
labels
labels
can be used to pass on specific label metadata to a consumer. Examples of labels are the URL or method when
timing HTTP requests.
Labels should be defined as an array of objects where each object has a name
and value
property.
The name
property describes the labels name and the value
property describes the label’s actual value.
.timer(options)
Starts a metric timer and returns and end function to be called when the measurement should be considered finished.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
value | Arbitrary value for the metric (used for gauges) | string|number | null | false |
meta | Available to be used to hold any misc data | object | null | false |
return: function
Returns an end function (see below) to be used to indicate that the timer measurement is finished.
Example
.end(options)
Stops a previously started timer, merges timers options
with end options
and and sets the measured time
value.
options
name | description | type | default | required |
---|---|---|---|---|
name | Metric name. valid characters: a-z,A-Z,0-9,_ | string | null | true |
description | Metric description | string | null | true |
value | Arbitrary value for the metric (used for gauges) | string|number | null | false |
meta | Available to be used to hold any misc data | object | null | false |
return: void
Example
instance events
drop
Emitted when the client starts dropping metrics. Will emit the dropped metric.
Example
Examples
Counter
Gauge
Summary
Summary using a timer
Histogram
Histogram using a timer
Composing metric streams
One of the goals of @metrics/client
is to allow any number of modules to produce their own metrics, not know about
where they might be consumed.
This can be achieved by including and instantiating a @metrics/client
client in each module, using it to create metrics and then exposing the client for consumption elsewhere.
Example
Metrics consumption
In order to consume metrics produced by @metrics/client
you need to listen for data and use your favourite metrics client to convert our data format into something usable by your system of choice.
Example: Prometheus using prom-client