UNPKG

@libp2p/prometheus-metrics

Version:

Collect libp2p metrics for scraping by Prometheus or Graphana

113 lines 3.27 kB
/** * @packageDocumentation * * Configure your libp2p node with Prometheus metrics: * * ```typescript * import { createLibp2p } from 'libp2p' * import { prometheusMetrics } from '@libp2p/prometheus-metrics' * * const node = await createLibp2p({ * metrics: prometheusMetrics() * }) * ``` * * Then use the `prom-client` module to supply metrics to the Prometheus/Graphana client using your http framework: * * ```JavaScript * import client from 'prom-client' * * async function handler (request, h) { * return h.response(await client.register.metrics()) * .type(client.register.contentType) * } * ``` * * All Prometheus metrics are global so there's no other work required to extract them. * * ## Queries * * Some useful queries are: * * ### Data sent/received * * ``` * rate(libp2p_data_transfer_bytes_total[30s]) * ``` * * ### CPU usage * * ``` * rate(process_cpu_user_seconds_total[30s]) * 100 * ``` * * ### Memory usage * * ``` * nodejs_memory_usage_bytes * ``` * * ### DHT query time * * ``` * libp2p_kad_dht_wan_query_time_seconds * ``` * * or * * ``` * libp2p_kad_dht_lan_query_time_seconds * ``` * * ### TCP transport dialer errors * * ``` * rate(libp2p_tcp_dialer_errors_total[30s]) * ``` */ import type { ComponentLogger, CalculatedMetricOptions, Metrics, CalculatedHistogramOptions, CalculatedSummaryOptions } from '@libp2p/interface'; import type { DefaultMetricsCollectorConfiguration, Registry, RegistryContentType } from 'prom-client'; export { linearBuckets, exponentialBuckets } from 'prom-client'; export interface PrometheusMetricsInit { /** * Use a custom registry to register metrics. * By default, the global registry is used to register metrics. */ registry?: Registry; /** * By default we collect default metrics - CPU, memory etc, to not do * this, pass true here */ collectDefaultMetrics?: boolean; /** * prom-client options to pass to the `collectDefaultMetrics` function */ defaultMetrics?: DefaultMetricsCollectorConfiguration<RegistryContentType>; /** * All metrics in prometheus are global so to prevent clashes in naming * we reset the global metrics registry on creation - to not do this, * pass true here */ preserveExistingMetrics?: boolean; /** * The current filesystem usage is reported as the metric * `nodejs_fs_usage_bytes` using the `statfs` function from `node:fs` - the * default location to stat is the current working directory, configured this * location here */ statfsLocation?: string; } export interface PrometheusCalculatedMetricOptions<T = number> extends CalculatedMetricOptions<T> { registry?: Registry; } export interface PrometheusCalculatedHistogramOptions<T = number> extends CalculatedHistogramOptions<T> { registry?: Registry; } export interface PrometheusCalculatedSummaryOptions<T = number> extends CalculatedSummaryOptions<T> { registry?: Registry; } export interface PrometheusMetricsComponents { logger: ComponentLogger; } export declare function prometheusMetrics(init?: Partial<PrometheusMetricsInit>): (components: PrometheusMetricsComponents) => Metrics; //# sourceMappingURL=index.d.ts.map