Skip to content
This repository has been archived by the owner on Aug 24, 2023. It is now read-only.

docs: added examples for package documentation for methods #31

Merged
merged 1 commit into from
May 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,100 @@
/**
* @packageDocumentation
*
* Collect libp2p metrics for scraping by Prometheus or Graphana.
* @module libp2p-prometheus-metrics
*
* A tracked metric can be created by calling either `registerMetric` on the metrics object
*
* @example
*
* ```typescript
* 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
*
* ```typescript
* 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
*
* ```typescript
* 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
*
* ```typescript
* 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
*
* ```typescript
* 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)
* ```
*
*/

import type { CalculatedMetricOptions, Counter, CounterGroup, Metric, MetricGroup, MetricOptions, Metrics } from '@libp2p/interface-metrics'
import { collectDefaultMetrics, DefaultMetricsCollectorConfiguration, register, Registry } from 'prom-client'
import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface-connection'
Expand Down