@libp2p/prometheus-metrics - v1.1.5

Collect libp2p metrics for scraping by Prometheus or Graphana.

Example

import { prometheusMetrics } from '@libp2p/prometheus-metrics'

const metrics = prometheusMetrics()()
const myMetric = metrics.registerMetric({
name: 'my_metric',
label: 'my_label',
help: 'my help text'
})

myMetric.update(1)

A metric that is expensive to calculate can be created by passing a calculate function that will only be invoked when metrics are being scraped:

Example

import { prometheusMetrics } from '@libp2p/prometheus-metrics'

const metrics = prometheusMetrics()()
const myMetric = metrics.registerMetric({
name: 'my_metric',
label: 'my_label',
help: 'my help text',
calculate: async () => {
// do something expensive
return 1
}
})

If several metrics should be grouped together (e.g. for graphing purposes) registerMetricGroup can be used instead:

Example

import { prometheusMetrics } from '@libp2p/prometheus-metrics'

const metrics = prometheusMetrics()()
const myMetricGroup = metrics.registerMetricGroup({
name: 'my_metric_group',
label: 'my_label',
help: 'my help text'
})

myMetricGroup.increment({ my_label: 'my_value' })

There are specific metric groups for tracking libp2p connections and streams:

Track a newly opened multiaddr connection:

Example

import { prometheusMetrics } from '@libp2p/prometheus-metrics'
import { createLibp2p } from 'libp2p'


const metrics = prometheusMetrics()()

const libp2p = await createLibp2p({
metrics: metrics,
})
// set up a multiaddr connection
const connection = await libp2p.dial('multiaddr')
const connections = metrics.trackMultiaddrConnection(connection)

Track a newly opened stream:

Example

import { prometheusMetrics } from '@libp2p/prometheus-metrics'
import { createLibp2p } from 'libp2p'

const metrics = prometheusMetrics()()

const libp2p = await createLibp2p({
metrics: metrics,
})

const stream = await connection.newStream('/my/protocol')
const streams = metrics.trackProtocolStream(stream)

Index

Interfaces

Functions