libp2p
    Preparing search index...

    Interface Metrics

    The libp2p metrics tracking object. This interface is only concerned with the collection of metrics, please see the individual implementations for how to extract metrics for viewing.

    import { Metrics, Metric } from '@libp2p/interface/metrics'

    interface MyServiceComponents {
    metrics: Metrics
    }

    class MyService {
    private readonly myMetric: Metric

    constructor (components: MyServiceComponents) {
    this.myMetric = components.metrics.registerMetric({
    name: 'my_metric',
    label: 'my_label',
    help: 'my help text'
    })
    }

    // later
    doSomething () {
    this.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:

    import { Metrics, Metric } from '@libp2p/interface/metrics'

    interface MyServiceComponents {
    metrics: Metrics
    }

    class MyService {
    private readonly myMetric: Metric

    constructor (components: MyServiceComponents) {
    this.myMetric = components.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:

    import { Metrics, MetricGroup } from '@libp2p/interface/metrics'

    interface MyServiceComponents {
    metrics: Metrics
    }

    class MyService {
    private readonly myMetricGroup: MetricGroup

    constructor (components: MyServiceComponents) {
    this.myMetricGroup = components.metrics.registerMetricGroup({
    name: 'my_metric_group',
    label: 'my_label',
    help: 'my help text'
    })
    }

    // later
    doSomething () {
    this.myMetricGroup.increment({ my_label: 'my_value' })
    }
    }

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

    This is something only libp2p transports need to do.

    import { Metrics } from '@libp2p/interface/metrics'

    interface MyServiceComponents {
    metrics: Metrics
    }

    class MyService {
    private readonly metrics: Metrics

    constructor (components: MyServiceComponents) {
    this.metrics = components.metrics
    }

    // later
    doSomething () {
    const connection = {} // create a connection
    this.metrics.trackMultiaddrConnection(connection)
    }
    }

    This is something only libp2p connections need to do.

    import { Metrics } from '@libp2p/interface/metrics'

    interface MyServiceComponents {
    metrics: Metrics
    }

    class MyService {
    private readonly metrics: Metrics

    constructor (components: MyServiceComponents) {
    this.metrics = components.metrics
    }

    // later
    doSomething () {
    const stream = {} // create a stream
    this.metrics.trackProtocolStream(stream)
    }
    }
    interface Metrics {
        registerCounter: (name: string, options?: MetricOptions) => Counter & (
            name: string,
            options: CalculatedMetricOptions,
        ) => void;
        registerCounterGroup: (
            name: string,
            options?: MetricOptions,
        ) => CounterGroup & (
            name: string,
            options: CalculatedMetricOptions<Record<string, number>>,
        ) => void;
        registerHistogram: (name: string, options?: HistogramOptions) => Histogram & (
            name: string,
            options: CalculatedHistogramOptions,
        ) => void;
        registerHistogramGroup: (
            name: string,
            options?: HistogramOptions,
        ) => HistogramGroup & (
            name: string,
            options: CalculatedHistogramOptions<Record<string, number>>,
        ) => void;
        registerMetric: (name: string, options?: MetricOptions) => Metric & (
            name: string,
            options: CalculatedMetricOptions,
        ) => void;
        registerMetricGroup: (name: string, options?: MetricOptions) => MetricGroup & (
            name: string,
            options: CalculatedMetricOptions<Record<string, number>>,
        ) => void;
        registerSummary: (name: string, options?: SummaryOptions) => Summary & (
            name: string,
            options: CalculatedSummaryOptions,
        ) => void;
        registerSummaryGroup: (
            name: string,
            options?: SummaryOptions,
        ) => SummaryGroup & (
            name: string,
            options: CalculatedSummaryOptions<Record<string, number>>,
        ) => void;
        createTrace(): any;
        traceFunction<F extends (...args: any[]) => AsyncIterator<any>>(
            name: string,
            fn: F,
            options?: TraceGeneratorFunctionOptions<
                Parameters<F>,
                ReturnType<F>,
                YieldType<ReturnType<F>>,
            >,
        ): F;
        traceFunction<F extends (...args: any[]) => Iterator<any>>(
            name: string,
            fn: F,
            options?: TraceGeneratorFunctionOptions<
                Parameters<F>,
                ReturnType<F>,
                YieldType<ReturnType<F>>,
            >,
        ): F;
        traceFunction<F extends (...args: any[]) => any = (...args: any[]) => any>(
            name: string,
            fn: F,
            options?: TraceFunctionOptions<Parameters<F>, ReturnType<F>>,
        ): F;
        trackMultiaddrConnection(maConn: MultiaddrConnection): void;
        trackProtocolStream(stream: Stream, connection: Connection): void;
    }
    Index

    Properties

    registerCounter: (name: string, options?: MetricOptions) => Counter & (
        name: string,
        options: CalculatedMetricOptions,
    ) => void

    Register an arbitrary counter. Call this to set help/labels for counters and increment them by calling methods on the returned counter object

    registerCounterGroup: (name: string, options?: MetricOptions) => CounterGroup & (
        name: string,
        options: CalculatedMetricOptions<Record<string, number>>,
    ) => void

    Register a a group of related counters. Call this to set help/labels for groups of related counters that will be updated with by calling the .increment method on the returned counter group object

    registerHistogram: (name: string, options?: HistogramOptions) => Histogram & (
        name: string,
        options: CalculatedHistogramOptions,
    ) => void

    Register an arbitrary histogram. Call this to set help/labels for histograms and observe them by calling methods on the returned histogram object

    registerHistogramGroup: (
        name: string,
        options?: HistogramOptions,
    ) => HistogramGroup & (
        name: string,
        options: CalculatedHistogramOptions<Record<string, number>>,
    ) => void

    Register a a group of related histograms. Call this to set help/labels for groups of related histograms that will be updated with by calling the .observe method on the returned histogram group object

    registerMetric: (name: string, options?: MetricOptions) => Metric & (
        name: string,
        options: CalculatedMetricOptions,
    ) => void

    Register an arbitrary metric. Call this to set help/labels for metrics and update/increment/decrement/etc them by calling methods on the returned metric object

    registerMetricGroup: (name: string, options?: MetricOptions) => MetricGroup & (
        name: string,
        options: CalculatedMetricOptions<Record<string, number>>,
    ) => void

    Register a a group of related metrics. Call this to set help/labels for groups of related metrics that will be updated with by calling .update, .increment and/or .decrement methods on the returned metric group object

    registerSummary: (name: string, options?: SummaryOptions) => Summary & (
        name: string,
        options: CalculatedSummaryOptions,
    ) => void

    Register an arbitrary summary. Call this to set help/labels for summaries and observe them by calling methods on the returned summary object

    registerSummaryGroup: (name: string, options?: SummaryOptions) => SummaryGroup & (
        name: string,
        options: CalculatedSummaryOptions<Record<string, number>>,
    ) => void

    Register a a group of related summaries. Call this to set help/labels for groups of related summaries that will be updated with by calling the .observe method on the returned summary group object

    Methods