@libp2p/interface 1.1.2-08f6f607d → 1.1.2-1cb2408ac

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,6 +128,144 @@ export interface CounterGroup {
128
128
  * The libp2p metrics tracking object. This interface is only concerned
129
129
  * with the collection of metrics, please see the individual implementations
130
130
  * for how to extract metrics for viewing.
131
+ *
132
+ * @example How to register a simple metric
133
+ *
134
+ * ```typescript
135
+ * import { Metrics, Metric } from '@libp2p/interface/metrics
136
+ *
137
+ * interface MyServiceComponents {
138
+ * metrics: Metrics
139
+ * }
140
+ *
141
+ * class MyService {
142
+ * private readonly myMetric: Metric
143
+ *
144
+ * constructor (components: MyServiceComponents) {
145
+ * this.myMetric = components.metrics.registerMetric({
146
+ * name: 'my_metric',
147
+ * label: 'my_label',
148
+ * help: 'my help text'
149
+ * })
150
+ * }
151
+ *
152
+ * // later
153
+ * doSomething () {
154
+ * this.myMetric.update(1)
155
+ * }
156
+ * }
157
+ * ```
158
+ *
159
+ * @example How to register a dynamically calculated metric
160
+ *
161
+ * 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:
162
+ *
163
+ * ```typescript
164
+ * import { Metrics, Metric } from '@libp2p/interface/metrics
165
+ *
166
+ * interface MyServiceComponents {
167
+ * metrics: Metrics
168
+ * }
169
+ *
170
+ * class MyService {
171
+ * private readonly myMetric: Metric
172
+ *
173
+ * constructor (components: MyServiceComponents) {
174
+ * this.myMetric = components.metrics.registerMetric({
175
+ * name: 'my_metric',
176
+ * label: 'my_label',
177
+ * help: 'my help text',
178
+ * calculate: async () => {
179
+ * // do something expensive
180
+ * return 1
181
+ * }
182
+ * })
183
+ * }
184
+ * }
185
+ * ```
186
+ *
187
+ * @example How to register a group of metrics
188
+ *
189
+ * If several metrics should be grouped together (e.g. for graphing purposes) `registerMetricGroup` can be used instead:
190
+ *
191
+ * ```typescript
192
+ * import { Metrics, MetricGroup } from '@libp2p/interface/metrics
193
+ *
194
+ * interface MyServiceComponents {
195
+ * metrics: Metrics
196
+ * }
197
+ *
198
+ * class MyService {
199
+ * private readonly myMetricGroup: MetricGroup
200
+ *
201
+ * constructor (components: MyServiceComponents) {
202
+ * this.myMetricGroup = components.metrics.registerMetricGroup({
203
+ * name: 'my_metric_group',
204
+ * label: 'my_label',
205
+ * help: 'my help text'
206
+ * })
207
+ * }
208
+ *
209
+ * // later
210
+ * doSomething () {
211
+ * this.myMetricGroup.increment({ my_label: 'my_value' })
212
+ * }
213
+ * }
214
+ * ```
215
+ *
216
+ * There are specific metric groups for tracking libp2p connections and streams:
217
+ *
218
+ * @example How to track multiaddr connections
219
+ *
220
+ * This is something only libp2p transports need to do.
221
+ *
222
+ * ```typescript
223
+ * import { Metrics } from '@libp2p/interface/metrics
224
+ *
225
+ * interface MyServiceComponents {
226
+ * metrics: Metrics
227
+ * }
228
+ *
229
+ * class MyService {
230
+ * private readonly metrics: Metrics
231
+ *
232
+ * constructor (components: MyServiceComponents) {
233
+ * this.metrics = components.metrics
234
+ * }
235
+ *
236
+ * // later
237
+ * doSomething () {
238
+ * const connection = {} // create a connection
239
+ * this.metrics.trackMultiaddrConnection(connection)
240
+ * }
241
+ * }
242
+ * ```
243
+ *
244
+ * @example How to track protocol streams
245
+ *
246
+ * This is something only libp2p connections need to do.
247
+ *
248
+ * ```typescript
249
+ * import { Metrics } from '@libp2p/interface/metrics
250
+ *
251
+ * interface MyServiceComponents {
252
+ * metrics: Metrics
253
+ * }
254
+ *
255
+ * class MyService {
256
+ * private readonly metrics: Metrics
257
+ *
258
+ * constructor (components: MyServiceComponents) {
259
+ * this.metrics = components.metrics
260
+ * }
261
+ *
262
+ * // later
263
+ * doSomething () {
264
+ * const stream = {} // create a stream
265
+ * this.metrics.trackProtocolStream(stream)
266
+ * }
267
+ * }
268
+ * ```
131
269
  */
132
270
  export interface Metrics {
133
271
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":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B;;OAEG;IACH,wBAAwB,CAAC,MAAM,EAAE,mBAAmB,GAAG,IAAI,CAAA;IAE3D;;OAEG;IACH,mBAAmB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,GAAG,IAAI,CAAA;IAEjE;;;;OAIG;IACH,cAAc,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,KAAK,IAAI,CAAC,CAAA;IAEhI;;;;OAIG;IACH,mBAAmB,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,WAAW,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,KAAK,IAAI,CAAC,CAAA;IAElK;;;OAGG;IACH,eAAe,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,KAAK,IAAI,CAAC,CAAA;IAElI;;;;OAIG;IACH,oBAAoB,EAAE,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,KAAK,YAAY,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,uBAAuB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,KAAK,IAAI,CAAC,CAAA;CACrK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@libp2p/interface",
3
- "version": "1.1.2-08f6f607d",
3
+ "version": "1.1.2-1cb2408ac",
4
4
  "description": "The interface implemented by a libp2p node",
5
5
  "license": "Apache-2.0 OR MIT",
6
6
  "homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/interface#readme",
@@ -146,6 +146,144 @@ export interface CounterGroup {
146
146
  * The libp2p metrics tracking object. This interface is only concerned
147
147
  * with the collection of metrics, please see the individual implementations
148
148
  * for how to extract metrics for viewing.
149
+ *
150
+ * @example How to register a simple metric
151
+ *
152
+ * ```typescript
153
+ * import { Metrics, Metric } from '@libp2p/interface/metrics
154
+ *
155
+ * interface MyServiceComponents {
156
+ * metrics: Metrics
157
+ * }
158
+ *
159
+ * class MyService {
160
+ * private readonly myMetric: Metric
161
+ *
162
+ * constructor (components: MyServiceComponents) {
163
+ * this.myMetric = components.metrics.registerMetric({
164
+ * name: 'my_metric',
165
+ * label: 'my_label',
166
+ * help: 'my help text'
167
+ * })
168
+ * }
169
+ *
170
+ * // later
171
+ * doSomething () {
172
+ * this.myMetric.update(1)
173
+ * }
174
+ * }
175
+ * ```
176
+ *
177
+ * @example How to register a dynamically calculated metric
178
+ *
179
+ * 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:
180
+ *
181
+ * ```typescript
182
+ * import { Metrics, Metric } from '@libp2p/interface/metrics
183
+ *
184
+ * interface MyServiceComponents {
185
+ * metrics: Metrics
186
+ * }
187
+ *
188
+ * class MyService {
189
+ * private readonly myMetric: Metric
190
+ *
191
+ * constructor (components: MyServiceComponents) {
192
+ * this.myMetric = components.metrics.registerMetric({
193
+ * name: 'my_metric',
194
+ * label: 'my_label',
195
+ * help: 'my help text',
196
+ * calculate: async () => {
197
+ * // do something expensive
198
+ * return 1
199
+ * }
200
+ * })
201
+ * }
202
+ * }
203
+ * ```
204
+ *
205
+ * @example How to register a group of metrics
206
+ *
207
+ * If several metrics should be grouped together (e.g. for graphing purposes) `registerMetricGroup` can be used instead:
208
+ *
209
+ * ```typescript
210
+ * import { Metrics, MetricGroup } from '@libp2p/interface/metrics
211
+ *
212
+ * interface MyServiceComponents {
213
+ * metrics: Metrics
214
+ * }
215
+ *
216
+ * class MyService {
217
+ * private readonly myMetricGroup: MetricGroup
218
+ *
219
+ * constructor (components: MyServiceComponents) {
220
+ * this.myMetricGroup = components.metrics.registerMetricGroup({
221
+ * name: 'my_metric_group',
222
+ * label: 'my_label',
223
+ * help: 'my help text'
224
+ * })
225
+ * }
226
+ *
227
+ * // later
228
+ * doSomething () {
229
+ * this.myMetricGroup.increment({ my_label: 'my_value' })
230
+ * }
231
+ * }
232
+ * ```
233
+ *
234
+ * There are specific metric groups for tracking libp2p connections and streams:
235
+ *
236
+ * @example How to track multiaddr connections
237
+ *
238
+ * This is something only libp2p transports need to do.
239
+ *
240
+ * ```typescript
241
+ * import { Metrics } from '@libp2p/interface/metrics
242
+ *
243
+ * interface MyServiceComponents {
244
+ * metrics: Metrics
245
+ * }
246
+ *
247
+ * class MyService {
248
+ * private readonly metrics: Metrics
249
+ *
250
+ * constructor (components: MyServiceComponents) {
251
+ * this.metrics = components.metrics
252
+ * }
253
+ *
254
+ * // later
255
+ * doSomething () {
256
+ * const connection = {} // create a connection
257
+ * this.metrics.trackMultiaddrConnection(connection)
258
+ * }
259
+ * }
260
+ * ```
261
+ *
262
+ * @example How to track protocol streams
263
+ *
264
+ * This is something only libp2p connections need to do.
265
+ *
266
+ * ```typescript
267
+ * import { Metrics } from '@libp2p/interface/metrics
268
+ *
269
+ * interface MyServiceComponents {
270
+ * metrics: Metrics
271
+ * }
272
+ *
273
+ * class MyService {
274
+ * private readonly metrics: Metrics
275
+ *
276
+ * constructor (components: MyServiceComponents) {
277
+ * this.metrics = components.metrics
278
+ * }
279
+ *
280
+ * // later
281
+ * doSomething () {
282
+ * const stream = {} // create a stream
283
+ * this.metrics.trackProtocolStream(stream)
284
+ * }
285
+ * }
286
+ * ```
149
287
  */
150
288
  export interface Metrics {
151
289
  /**