Collect libp2p metrics for scraping by Prometheus or Graphana.
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:
calculate
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:
registerMetricGroup
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:
import { prometheusMetrics } from '@libp2p/prometheus-metrics'import { createLibp2p } from 'libp2p'const metrics = prometheusMetrics()()const libp2p = await createLibp2p({ metrics: metrics, })// set up a multiaddr connectionconst connection = await libp2p.dial('multiaddr')const connections = metrics.trackMultiaddrConnection(connection)
Track a newly opened stream:
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)
Collect libp2p metrics for scraping by Prometheus or Graphana.
Example
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
If several metrics should be grouped together (e.g. for graphing purposes)
registerMetricGroup
can be used instead:Example
There are specific metric groups for tracking libp2p connections and streams:
Track a newly opened multiaddr connection:
Example
Track a newly opened stream:
Example