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

Commit 7dbd895

Browse files
authored
docs: added examples for package documentation for methods (#31)
Closes #30
1 parent 4cdae25 commit 7dbd895

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed

src/index.ts

+97
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,100 @@
1+
/**
2+
* @packageDocumentation
3+
*
4+
* Collect libp2p metrics for scraping by Prometheus or Graphana.
5+
* @module libp2p-prometheus-metrics
6+
*
7+
* A tracked metric can be created by calling either `registerMetric` on the metrics object
8+
*
9+
* @example
10+
*
11+
* ```typescript
12+
* import { prometheusMetrics } from '@libp2p/prometheus-metrics'
13+
*
14+
* const metrics = prometheusMetrics()()
15+
* const myMetric = metrics.registerMetric({
16+
* name: 'my_metric',
17+
* label: 'my_label',
18+
* help: 'my help text'
19+
* })
20+
*
21+
* myMetric.update(1)
22+
* ```
23+
* 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:
24+
*
25+
* @example
26+
*
27+
* ```typescript
28+
* import { prometheusMetrics } from '@libp2p/prometheus-metrics'
29+
*
30+
* const metrics = prometheusMetrics()()
31+
* const myMetric = metrics.registerMetric({
32+
* name: 'my_metric',
33+
* label: 'my_label',
34+
* help: 'my help text',
35+
* calculate: async () => {
36+
* // do something expensive
37+
* return 1
38+
* }
39+
* })
40+
* ```
41+
*
42+
* If several metrics should be grouped together (e.g. for graphing purposes) `registerMetricGroup` can be used instead:
43+
*
44+
* @example
45+
*
46+
* ```typescript
47+
* import { prometheusMetrics } from '@libp2p/prometheus-metrics'
48+
*
49+
* const metrics = prometheusMetrics()()
50+
* const myMetricGroup = metrics.registerMetricGroup({
51+
* name: 'my_metric_group',
52+
* label: 'my_label',
53+
* help: 'my help text'
54+
* })
55+
*
56+
* myMetricGroup.increment({ my_label: 'my_value' })
57+
* ```
58+
*
59+
* There are specific metric groups for tracking libp2p connections and streams:
60+
*
61+
* Track a newly opened multiaddr connection:
62+
* @example
63+
*
64+
* ```typescript
65+
* import { prometheusMetrics } from '@libp2p/prometheus-metrics'
66+
* import { createLibp2p } from 'libp2p'
67+
*
68+
*
69+
* const metrics = prometheusMetrics()()
70+
*
71+
* const libp2p = await createLibp2p({
72+
* metrics: metrics,
73+
* })
74+
* // set up a multiaddr connection
75+
* const connection = await libp2p.dial('multiaddr')
76+
* const connections = metrics.trackMultiaddrConnection(connection)
77+
* ```
78+
*
79+
* Track a newly opened stream:
80+
* @example
81+
*
82+
* ```typescript
83+
* import { prometheusMetrics } from '@libp2p/prometheus-metrics'
84+
* import { createLibp2p } from 'libp2p'
85+
*
86+
* const metrics = prometheusMetrics()()
87+
*
88+
* const libp2p = await createLibp2p({
89+
* metrics: metrics,
90+
* })
91+
*
92+
* const stream = await connection.newStream('/my/protocol')
93+
* const streams = metrics.trackProtocolStream(stream)
94+
* ```
95+
*
96+
*/
97+
198
import type { CalculatedMetricOptions, Counter, CounterGroup, Metric, MetricGroup, MetricOptions, Metrics } from '@libp2p/interface-metrics'
299
import { collectDefaultMetrics, DefaultMetricsCollectorConfiguration, register, Registry } from 'prom-client'
3100
import type { MultiaddrConnection, Stream, Connection } from '@libp2p/interface-connection'

0 commit comments

Comments
 (0)