From 3a4fc5ab5916b9bcade5f8dcff797926405a4406 Mon Sep 17 00:00:00 2001 From: James Lal Date: Wed, 4 Jan 2023 09:38:10 -0700 Subject: [PATCH 01/12] feat: metrics --- package.json | 4 +++- src/maconn.ts | 12 ++++++++++++ src/muxer.ts | 34 ++++++++++++++++++++++++++++++---- src/transport.ts | 26 ++++++++++++++++++++++++-- test/maconn.browser.spec.ts | 10 +++++++++- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index c13a510..0ffb0b0 100644 --- a/package.json +++ b/package.json @@ -137,6 +137,7 @@ "dependencies": { "@chainsafe/libp2p-noise": "^10.0.0", "@libp2p/interface-connection": "^3.0.2", + "@libp2p/interface-metrics": "^4.0.4", "@libp2p/interface-peer-id": "^1.0.5", "@libp2p/interface-stream-muxer": "^3.0.0", "@libp2p/interface-transport": "^2.0.0", @@ -163,6 +164,7 @@ "@protobuf-ts/protoc": "^2.8.0", "aegir": "^37.6.6", "it-first": "^2.0.0", - "libp2p": "^0.41.0" + "libp2p": "^0.41.0", + "sinon-ts": "^1.0.0" } } diff --git a/src/maconn.ts b/src/maconn.ts index 49c67e1..b241c5d 100644 --- a/src/maconn.ts +++ b/src/maconn.ts @@ -1,4 +1,5 @@ import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection' +import type { CounterGroup } from '@libp2p/interface-metrics' import { logger } from '@libp2p/logger' import type { Multiaddr } from '@multiformats/multiaddr' import type { Source, Sink } from 'it-stream-types' @@ -22,6 +23,11 @@ interface WebRTCMultiaddrConnectionInit { * Holds the relevant events timestamps of the connection */ timeline: MultiaddrConnectionTimeline + + /** + * Optional metrics counter group for this connection + */ + metrics?: CounterGroup } export class WebRTCMultiaddrConnection implements MultiaddrConnection { @@ -40,6 +46,11 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection { */ timeline: MultiaddrConnectionTimeline; + /** + * Optional metrics counter group for this connection + */ + metrics?: CounterGroup + /** * The stream source, a no-op as the transport natively supports multiplexing */ @@ -62,6 +73,7 @@ export class WebRTCMultiaddrConnection implements MultiaddrConnection { } this.timeline.close = new Date().getTime() + this.metrics?.increment({ close: true }) this.peerConnection.close() } } diff --git a/src/muxer.ts b/src/muxer.ts index 7b0eb83..6b462dd 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -1,27 +1,42 @@ import type { Stream } from '@libp2p/interface-connection' +import type { CounterGroup } from '@libp2p/interface-metrics' import type { StreamMuxer, StreamMuxerFactory, StreamMuxerInit } from '@libp2p/interface-stream-muxer' import type { Source, Sink } from 'it-stream-types' import { WebRTCStream } from './stream.js' import { nopSink, nopSource } from './util.js' +export interface DataChannelMuxerFactoryInit { + peerConnection: RTCPeerConnection + metrics?: CounterGroup +} + export class DataChannelMuxerFactory implements StreamMuxerFactory { /** * WebRTC Peer Connection */ private readonly peerConnection: RTCPeerConnection + /** + * Optional metrics counter group for all incoming/outgoing mux events. + */ + private readonly metrics?: CounterGroup + /** * The string representation of the protocol, required by `StreamMuxerFactory` */ protocol: string = '/webrtc' - constructor (peerConnection: RTCPeerConnection) { + constructor (init: DataChannelMuxerFactoryInit) { + const { metrics, peerConnection } = init this.peerConnection = peerConnection + if (metrics != null) { + this.metrics = metrics + } } createStreamMuxer (init?: StreamMuxerInit | undefined): StreamMuxer { - return new DataChannelMuxer(this.peerConnection, init) + return new DataChannelMuxer(this.peerConnection, this.metrics, init) } } @@ -33,6 +48,12 @@ export class DataChannelMuxer implements StreamMuxer { * WebRTC Peer Connection */ private readonly peerConnection: RTCPeerConnection + + /** + * Optional metrics for this data channel muxer + */ + private readonly metrics?: CounterGroup + /** * The protocol as represented in the multiaddress */ @@ -63,7 +84,7 @@ export class DataChannelMuxer implements StreamMuxer { */ sink: Sink> = nopSink; - constructor (peerConnection: RTCPeerConnection, init?: StreamMuxerInit) { + constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, init?: StreamMuxerInit) { /** * Initialized stream muxer */ @@ -93,6 +114,7 @@ export class DataChannelMuxer implements StreamMuxer { }) if ((init?.onIncomingStream) != null) { + this.metrics?.increment({ incoming_stream: true }) init.onIncomingStream(stream) } } @@ -104,6 +126,10 @@ export class DataChannelMuxer implements StreamMuxer { */ newStream (name: string = ''): Stream { const channel = this.peerConnection.createDataChannel(name) + const closeCb = (stream: WebRTCStream) => { + this.metrics?.increment({ stream_end: true }) + this.init?.onStreamEnd?.(stream) + } const stream = new WebRTCStream({ channel, stat: { @@ -112,7 +138,7 @@ export class DataChannelMuxer implements StreamMuxer { open: 0 } }, - closeCb: this.init?.onStreamEnd + closeCb }) return stream diff --git a/src/transport.ts b/src/transport.ts index 897f9ca..0fff597 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -13,6 +13,7 @@ import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgumen import { WebRTCMultiaddrConnection } from './maconn.js' import { DataChannelMuxerFactory } from './muxer.js' import type { WebRTCDialOptions } from './options.js' +import type { CounterGroup, Metrics } from '@libp2p/interface-metrics' import * as sdp from './sdp.js' import { WebRTCStream } from './stream.js' import { genUfrag } from './util.js' @@ -44,6 +45,11 @@ export const CERTHASH_CODE: number = 466 // @TODO(ddimaria): seems like an unnessary abstraction, consider removing export interface WebRTCTransportComponents { peerId: PeerId + metrics?: Metrics +} + +export interface WebRTCMetrics { + dialerEvents: CounterGroup } export class WebRTCTransport implements Transport { @@ -51,9 +57,18 @@ export class WebRTCTransport implements Transport { * The peer for this transport */ private readonly components: WebRTCTransportComponents + private readonly metrics?: WebRTCMetrics constructor (components: WebRTCTransportComponents) { this.components = components + if (components.metrics != null) { + this.metrics = { + dialerEvents: components.metrics.registerCounterGroup('libp2p_webrtc_dialer_events_total', { + label: 'event', + help: 'Total count of WebRTC dial events by type' + }) + } + } } /** @@ -124,6 +139,7 @@ export class WebRTCTransport implements Transport { const handhsakeTimeout = setTimeout(() => { const error = `Data channel was never opened: state: ${handshakeDataChannel.readyState}` log.error(error) + this.metrics?.dialerEvents.increment({ openError: true }) dataChannelOpenPromise.reject(dataChannelError('data', error)) }, HANDSHAKE_TIMEOUT_MS) @@ -138,6 +154,8 @@ export class WebRTCTransport implements Transport { const errorTarget = event.target?.toString() ?? 'not specified' const error = `Error opening a data channel for handshaking: ${errorTarget}` log.error(error) + // NOTE: We use unknown error here but this could potentially be considered a reset by some standards. + this.metrics?.dialerEvents.increment({ unknownError: true }) dataChannelOpenPromise.reject(dataChannelError('data', error)) } @@ -189,10 +207,14 @@ export class WebRTCTransport implements Transport { remoteAddr: ma, timeline: { open: (new Date()).getTime() - } + }, + metrics: this.metrics?.dialerEvents }) - const muxerFactory = new DataChannelMuxerFactory(peerConnection) + const muxerFactory = new DataChannelMuxerFactory({ + peerConnection, + metrics: this.metrics?.dialerEvents + }) // For outbound connections, the remote is expected to start the noise handshake. // Therefore, we need to secure an inbound noise connection from the remote. diff --git a/test/maconn.browser.spec.ts b/test/maconn.browser.spec.ts index ffc68f7..d192005 100644 --- a/test/maconn.browser.spec.ts +++ b/test/maconn.browser.spec.ts @@ -2,6 +2,8 @@ import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' +import type { CounterGroup } from '@libp2p/interface-metrics' +import { stubObject } from 'sinon-ts' import { WebRTCMultiaddrConnection } from './../src/maconn.js' describe('Multiaddr Connection', () => { @@ -9,12 +11,17 @@ describe('Multiaddr Connection', () => { const peerConnection = new RTCPeerConnection() peerConnection.createDataChannel('whatever', { negotiated: true, id: 91 }) const remoteAddr = multiaddr('/ip4/1.2.3.4/udp/1234/webrtc/certhash/uEiAUqV7kzvM1wI5DYDc1RbcekYVmXli_Qprlw3IkiEg6tQ') + const metrics = stubObject({ + increment: () => {}, + reset: () => {} + }) const maConn = new WebRTCMultiaddrConnection({ peerConnection: peerConnection, remoteAddr, timeline: { open: (new Date()).getTime() - } + }, + metrics }) expect(maConn.timeline.close).to.be.undefined @@ -22,5 +29,6 @@ describe('Multiaddr Connection', () => { await maConn.close() expect(maConn.timeline.close).to.not.be.undefined + expect(metrics.increment.calledWith({ close: true })).to.be.true }) }) From 0f8bfd34f95033b546e20621425932e8bc6af4c1 Mon Sep 17 00:00:00 2001 From: James Lal Date: Wed, 4 Jan 2023 09:50:51 -0700 Subject: [PATCH 02/12] add metrics mocks for future use cases --- test/transport.browser.spec.ts | 46 +++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/test/transport.browser.spec.ts b/test/transport.browser.spec.ts index e52946c..e22847a 100644 --- a/test/transport.browser.spec.ts +++ b/test/transport.browser.spec.ts @@ -1,12 +1,15 @@ /* eslint-disable @typescript-eslint/no-floating-promises */ +import type { Metrics, Metric, MetricGroup, Counter, CounterGroup } from '@libp2p/interface-metrics' import * as underTest from './../src/transport.js' import { expectError } from './util.js' import { UnimplementedError } from './../src/error.js' +import { stubObject } from 'sinon-ts' import { mockUpgrader } from '@libp2p/interface-mocks' import { CreateListenerOptions, symbol } from '@libp2p/interface-transport' import { multiaddr, Multiaddr } from '@multiformats/multiaddr' import { createEd25519PeerId } from '@libp2p/peer-id-factory' +import * as sinon from 'sinon' import { expect, assert } from 'aegir/chai' function ignoredDialOption (): CreateListenerOptions { @@ -16,10 +19,51 @@ function ignoredDialOption (): CreateListenerOptions { describe('WebRTC Transport', () => { let components: underTest.WebRTCTransportComponents + let metrics: Metrics before(async () => { + metrics = stubObject({ + trackMultiaddrConnection: (maConn) => {}, + trackProtocolStream: (stream, connection) => {}, + registerMetric: (name, options) => { + return stubObject({ + increment: () => {}, + reset: () => {}, + decrement: () => {}, + update: () => {}, + timer: () => { + return sinon.spy() + } + }) + }, + registerMetricGroup: (name, options) => { + return stubObject({ + increment: () => { + }, + reset: () => {}, + decrement: () => {}, + update: () => {}, + timer: () => { + return sinon.spy() + } + }) + }, + registerCounter: (name, options) => { + return stubObject({ + increment: () => {}, + reset: () => {} + }) + }, + registerCounterGroup: (name, options) => { + return stubObject({ + increment: () => {}, + reset: () => {} + }) + } + }) components = { - peerId: await createEd25519PeerId() + peerId: await createEd25519PeerId(), + metrics } }) From 28c9183e28e3e672062f8af4caae588ac52572fc Mon Sep 17 00:00:00 2001 From: David D Date: Fri, 24 Feb 2023 05:10:53 -0700 Subject: [PATCH 03/12] Fix aegir lints --- src/muxer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/muxer.ts b/src/muxer.ts index fd51f71..099eb2c 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -119,11 +119,11 @@ export class DataChannelMuxer implements StreamMuxer { } } } - + newStream (): Stream { // The spec says the label SHOULD be an empty string: https://github.com/libp2p/specs/blob/master/webrtc/README.md#rtcdatachannel-label const channel = this.peerConnection.createDataChannel('') - const closeCb = (stream: WebRTCStream) => { + const closeCb = (stream: WebRTCStream): void => { this.metrics?.increment({ stream_end: true }) this.init?.onStreamEnd?.(stream) } From 29a9269133760ce8688d3b4c26bfe5c130576f24 Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 15 Mar 2023 15:52:53 -0500 Subject: [PATCH 04/12] chore: removed unused dev deps (#64) --- package.json | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/package.json b/package.json index b7325aa..a89d8d2 100644 --- a/package.json +++ b/package.json @@ -160,11 +160,8 @@ "devDependencies": { "@libp2p/interface-mocks": "^8.0.1", "@libp2p/peer-id-factory": "^1.0.19", - "@protobuf-ts/plugin": "^2.8.0", "@protobuf-ts/protoc": "^2.8.0", "@types/uuid": "^8.3.4", - "aegir": "^37.6.6", - "it-first": "^2.0.0", - "libp2p": "^0.40.0" + "aegir": "^37.6.6" } } From 85ac0d67b35daad2b14e70715d289bacf00cad6c Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 15 Mar 2023 16:33:32 -0500 Subject: [PATCH 05/12] chore: fix dep versions (#64) --- package.json | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 890a6ae..dbd340e 100644 --- a/package.json +++ b/package.json @@ -162,6 +162,8 @@ "@libp2p/peer-id-factory": "^1.0.19", "@protobuf-ts/protoc": "^2.8.0", "@types/uuid": "^8.3.4", - "aegir": "^37.6.6" + "aegir": "^38.1.6", + "sinon": "^15.0.2", + "sinon-ts": "^1.0.0" } } From ef1dc6eb2b3e0afab54dd1962510c7a9094d04ef Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 15 Mar 2023 22:58:44 -0500 Subject: [PATCH 06/12] deps: upgraded deps to fix typing issue (#64) --- package.json | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index dbd340e..b043907 100644 --- a/package.json +++ b/package.json @@ -158,11 +158,12 @@ "uint8arrays": "^4.0.2" }, "devDependencies": { - "@libp2p/interface-mocks": "^8.0.1", - "@libp2p/peer-id-factory": "^1.0.19", - "@protobuf-ts/protoc": "^2.8.0", - "@types/uuid": "^8.3.4", - "aegir": "^38.1.6", + "@libp2p/interface-mocks": "^9.2.1", + "@libp2p/peer-id-factory": "^2.0.2", + "@protobuf-ts/plugin": "^2.8.2", + "@protobuf-ts/protoc": "^2.8.2", + "@types/uuid": "^9.0.1", + "aegir": "^38.1.7", "sinon": "^15.0.2", "sinon-ts": "^1.0.0" } From 24f20c897174a3be1996d6218923b38e5c280bc2 Mon Sep 17 00:00:00 2001 From: chad Date: Wed, 15 Mar 2023 22:58:44 -0500 Subject: [PATCH 07/12] deps: upgraded deps to fix typing issue (#64) --- package.json | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index dbd340e..a331228 100644 --- a/package.json +++ b/package.json @@ -158,11 +158,13 @@ "uint8arrays": "^4.0.2" }, "devDependencies": { - "@libp2p/interface-mocks": "^8.0.1", - "@libp2p/peer-id-factory": "^1.0.19", - "@protobuf-ts/protoc": "^2.8.0", - "@types/uuid": "^8.3.4", - "aegir": "^38.1.6", + "@libp2p/interface-mocks": "^9.2.1", + "@libp2p/peer-id-factory": "^2.0.2", + "@protobuf-ts/plugin": "^2.8.2", + "@protobuf-ts/protoc": "^2.8.2", + "@types/uuid": "^9.0.1", + "aegir": "^38.1.7", + "eslint-plugin-etc": "^2.0.2", "sinon": "^15.0.2", "sinon-ts": "^1.0.0" } From 2a40a0d9f50018ae513113085ef95e811de8746c Mon Sep 17 00:00:00 2001 From: chad Date: Sat, 22 Apr 2023 23:13:36 -0500 Subject: [PATCH 08/12] feat: add metrics callbacks + update constructors (#64) --- package.json | 19 ++++++++++--------- src/muxer.ts | 17 +++++++---------- src/transport.ts | 5 +---- test/transport.browser.spec.ts | 12 ++++++------ 4 files changed, 24 insertions(+), 29 deletions(-) diff --git a/package.json b/package.json index 2efd651..d95f596 100644 --- a/package.json +++ b/package.json @@ -138,7 +138,7 @@ }, "dependencies": { "@chainsafe/libp2p-noise": "^11.0.0", - "@libp2p/interface-connection": "^4.0.4", + "@libp2p/interface-connection": "^4.0.0", "@libp2p/interface-peer-id": "^2.0.0", "@libp2p/interface-peer-store": "^1.2.8", "@libp2p/interface-registrar": "^2.0.8", @@ -150,6 +150,7 @@ "@multiformats/multiaddr": "^12.1.1", "@protobuf-ts/runtime": "^2.8.0", "abortable-iterator": "^4.0.2", + "detect-browser": "^5.3.0", "err-code": "^3.0.1", "it-length-prefixed": "^8.0.3", "it-merge": "^2.0.0", @@ -163,19 +164,19 @@ "protons-runtime": "^5.0.0", "timeout-abort-controller": "^3.0.0", "uint8arraylist": "^2.3.3", - "uint8arrays": "^4.0.2", - "detect-browser": "^5.3.0" + "uint8arrays": "^4.0.2" }, "devDependencies": { - "@libp2p/interface-mocks": "^9.2.1", - "@libp2p/peer-id-factory": "^2.0.2", - "@protobuf-ts/plugin": "^2.8.2", - "@protobuf-ts/protoc": "^2.8.2", + "@libp2p/interface-mocks": "^9.0.0", + "@libp2p/peer-id-factory": "^2.0.0", + "@protobuf-ts/plugin": "^2.8.0", + "@protobuf-ts/protoc": "^2.8.0", "@types/uuid": "^9.0.1", - "aegir": "^38.1.7", + "aegir": "^38.1.6", "eslint-plugin-etc": "^2.0.2", "it-pair": "^2.0.3", "protons": "^7.0.2", - "sinon": "^15.0.2" + "sinon": "^15.0.1", + "sinon-ts": "^1.0.0" } } diff --git a/src/muxer.ts b/src/muxer.ts index 60e3122..c2815aa 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -17,8 +17,9 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory { */ private readonly peerConnection: RTCPeerConnection private streamBuffer: WebRTCStream[] = [] + private readonly metrics?: CounterGroup - constructor (peerConnection: RTCPeerConnection, readonly protocol = '/webrtc') { + constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, readonly protocol = '/webrtc') { this.peerConnection = peerConnection // store any datachannels opened before upgrade has been completed this.peerConnection.ondatachannel = ({ channel }) => { @@ -34,10 +35,11 @@ export class DataChannelMuxerFactory implements StreamMuxerFactory { }) this.streamBuffer.push(stream) } + this.metrics = metrics } createStreamMuxer (init?: StreamMuxerInit | undefined): StreamMuxer { - return new DataChannelMuxer(this.peerConnection, this.metrics, this.streamBuffer, this.protocol, init) + return new DataChannelMuxer(this.peerConnection, this.streamBuffer, this.protocol, init, this.metrics) } } @@ -55,11 +57,6 @@ export class DataChannelMuxer implements StreamMuxer { */ private readonly metrics?: CounterGroup - /** - * The protocol as represented in the multiaddress - */ - readonly protocol: string = '/webrtc' - /** * Array of streams in the data channel */ @@ -85,7 +82,7 @@ export class DataChannelMuxer implements StreamMuxer { */ sink: Sink> = nopSink - constructor (peerConnection: RTCPeerConnection, metrics?: CounterGroup, streams: Stream[], readonly protocol = '/webrtc', init?: StreamMuxerInit) { + constructor (peerConnection: RTCPeerConnection, streams: Stream[], readonly protocol: string = '/webrtc', init?: StreamMuxerInit, metrics?: CounterGroup) { /** * Initialized stream muxer */ @@ -137,7 +134,7 @@ export class DataChannelMuxer implements StreamMuxer { newStream (): Stream { // The spec says the label SHOULD be an empty string: https://github.com/libp2p/specs/blob/master/webrtc/README.md#rtcdatachannel-label const channel = this.peerConnection.createDataChannel('') - const closeCb = (stream: WebRTCStream): void => { + const closeCb = (stream: Stream): void => { this.metrics?.increment({ stream_end: true }) this.init?.onStreamEnd?.(stream) } @@ -149,7 +146,7 @@ export class DataChannelMuxer implements StreamMuxer { open: 0 } }, - closeCb: this.wrapStreamEnd(this.init?.onStreamEnd) + closeCb: this.wrapStreamEnd(closeCb) }) this.streams.push(stream) diff --git a/src/transport.ts b/src/transport.ts index 39c5abf..68d36fa 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -212,10 +212,7 @@ export class WebRTCDirectTransport implements Transport { metrics: this.metrics?.dialerEvents }) - const muxerFactory = new DataChannelMuxerFactory({ - peerConnection, - metrics: this.metrics?.dialerEvents - }) + const muxerFactory = new DataChannelMuxerFactory(peerConnection, this.metrics?.dialerEvents) // For outbound connections, the remote is expected to start the noise handshake. // Therefore, we need to secure an inbound noise connection from the remote. diff --git a/test/transport.browser.spec.ts b/test/transport.browser.spec.ts index f990899..7f8c173 100644 --- a/test/transport.browser.spec.ts +++ b/test/transport.browser.spec.ts @@ -23,9 +23,9 @@ describe('WebRTC Transport', () => { before(async () => { metrics = stubObject({ - trackMultiaddrConnection: (maConn) => {}, - trackProtocolStream: (stream, connection) => {}, - registerMetric: (name, options) => { + trackMultiaddrConnection: (_maConn) => {}, + trackProtocolStream: (_stream, _connection) => {}, + registerMetric: (_name, _options) => { return stubObject({ increment: () => {}, reset: () => {}, @@ -36,7 +36,7 @@ describe('WebRTC Transport', () => { } }) }, - registerMetricGroup: (name, options) => { + registerMetricGroup: (_name, _options) => { return stubObject({ increment: () => { }, @@ -48,13 +48,13 @@ describe('WebRTC Transport', () => { } }) }, - registerCounter: (name, options) => { + registerCounter: (_name, _options) => { return stubObject({ increment: () => {}, reset: () => {} }) }, - registerCounterGroup: (name, options) => { + registerCounterGroup: (_name, _options) => { return stubObject({ increment: () => {}, reset: () => {} From 1a883c10f7d7df40d83d60e647586e298447d9cc Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 27 Apr 2023 10:00:29 -0500 Subject: [PATCH 09/12] deps: remove unused deps (#64) --- package.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 5f9d8a1..d62951c 100644 --- a/package.json +++ b/package.json @@ -133,12 +133,13 @@ "lint": "aegir lint", "lint:fix": "aegir lint --fix", "clean": "aegir clean", - "dep-check": "aegir dep-check", + "dep-check": "aegir dep-check -i protons", "release": "aegir release" }, "dependencies": { "@chainsafe/libp2p-noise": "^11.0.0", "@libp2p/interface-connection": "^4.0.0", + "@libp2p/interface-metrics": "^4.0.7", "@libp2p/interface-peer-id": "^2.0.0", "@libp2p/interface-peer-store": "^2.0.0", "@libp2p/interface-registrar": "^2.0.8", @@ -151,7 +152,6 @@ "@protobuf-ts/runtime": "^2.8.0", "abortable-iterator": "^4.0.2", "detect-browser": "^5.3.0", - "err-code": "^3.0.1", "it-length-prefixed": "^8.0.3", "it-merge": "^2.0.0", "it-pb-stream": "^3.2.1", @@ -169,7 +169,6 @@ "devDependencies": { "@libp2p/interface-mocks": "^9.0.0", "@libp2p/peer-id-factory": "^2.0.0", - "@protobuf-ts/plugin": "^2.8.0", "@protobuf-ts/protoc": "^2.8.0", "@types/uuid": "^9.0.1", "aegir": "^38.1.6", From de5caeb52ce39e0ad1f7cc81b7f644268c0fd2ed Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 27 Apr 2023 11:07:24 -0500 Subject: [PATCH 10/12] feat: added tracking for outgoing stream + peer connections, updated to snake case (#64) --- src/muxer.ts | 1 + src/transport.ts | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/muxer.ts b/src/muxer.ts index c2815aa..0ad703e 100644 --- a/src/muxer.ts +++ b/src/muxer.ts @@ -149,6 +149,7 @@ export class DataChannelMuxer implements StreamMuxer { closeCb: this.wrapStreamEnd(closeCb) }) this.streams.push(stream) + this.metrics?.increment({ outgoing_stream: true }) return stream } diff --git a/src/transport.ts b/src/transport.ts index 69a5f6e..08164a6 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -143,7 +143,7 @@ export class WebRTCDirectTransport implements Transport { const handshakeTimeout = setTimeout(() => { const error = `Data channel was never opened: state: ${handshakeDataChannel.readyState}` log.error(error) - this.metrics?.dialerEvents.increment({ openError: true }) + this.metrics?.dialerEvents.increment({ open_error: true }) reject(dataChannelError('data', error)) }, HANDSHAKE_TIMEOUT_MS) @@ -159,7 +159,7 @@ export class WebRTCDirectTransport implements Transport { const error = `Error opening a data channel for handshaking: ${errorTarget}` log.error(error) // NOTE: We use unknown error here but this could potentially be considered a reset by some standards. - this.metrics?.dialerEvents.increment({ unknownError: true }) + this.metrics?.dialerEvents.increment({ unknown_error: true }) reject(dataChannelError('data', error)) } }) @@ -205,7 +205,7 @@ export class WebRTCDirectTransport implements Transport { } } - const eventListeningName = isFirefox as boolean ? 'iceconnectionstatechange' : 'connectionstatechange' + const eventListeningName = isFirefox ? 'iceconnectionstatechange' : 'connectionstatechange' peerConnection.addEventListener(eventListeningName, () => { switch (peerConnection.connectionState) { @@ -235,6 +235,9 @@ export class WebRTCDirectTransport implements Transport { metrics: this.metrics?.dialerEvents }) + // Track opened peer connection + this.metrics?.dialerEvents.increment({ peer_connection: true }) + const muxerFactory = new DataChannelMuxerFactory(peerConnection, this.metrics?.dialerEvents) // For outbound connections, the remote is expected to start the noise handshake. From 8b7b419fe7e3d615366ee7d52cd22fe7eb6c3427 Mon Sep 17 00:00:00 2001 From: chad Date: Thu, 4 May 2023 14:04:28 -0500 Subject: [PATCH 11/12] feat: pr minor adjustments (#64) --- src/transport.ts | 4 --- test/transport.browser.spec.ts | 46 +++------------------------------- 2 files changed, 3 insertions(+), 47 deletions(-) diff --git a/src/transport.ts b/src/transport.ts index 1d602ad..0f0d69e 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -43,7 +43,6 @@ export const CERTHASH_CODE: number = protocols('certhash').code /** * The peer for this transport */ -// @TODO(ddimaria): seems like an unnessary abstraction, consider removing export interface WebRTCDirectTransportComponents { peerId: PeerId metrics?: Metrics @@ -54,9 +53,6 @@ export interface WebRTCMetrics { } export class WebRTCDirectTransport implements Transport { - /** - * The peer for this transport - */ private readonly metrics?: WebRTCMetrics private readonly components: WebRTCDirectTransportComponents diff --git a/test/transport.browser.spec.ts b/test/transport.browser.spec.ts index 7f8c173..118e8c0 100644 --- a/test/transport.browser.spec.ts +++ b/test/transport.browser.spec.ts @@ -1,15 +1,13 @@ /* eslint-disable @typescript-eslint/no-floating-promises */ -import type { Metrics, Metric, MetricGroup, Counter, CounterGroup } from '@libp2p/interface-metrics' +import type { Metrics } from '@libp2p/interface-metrics' import * as underTest from './../src/transport.js' import { expectError } from './util.js' import { UnimplementedError } from './../src/error.js' -import { stubObject } from 'sinon-ts' -import { mockUpgrader } from '@libp2p/interface-mocks' +import { mockMetrics, mockUpgrader } from '@libp2p/interface-mocks' import { CreateListenerOptions, symbol } from '@libp2p/interface-transport' import { multiaddr, Multiaddr } from '@multiformats/multiaddr' import { createEd25519PeerId } from '@libp2p/peer-id-factory' -import * as sinon from 'sinon' import { expect, assert } from 'aegir/chai' function ignoredDialOption (): CreateListenerOptions { @@ -22,45 +20,7 @@ describe('WebRTC Transport', () => { let components: underTest.WebRTCDirectTransportComponents before(async () => { - metrics = stubObject({ - trackMultiaddrConnection: (_maConn) => {}, - trackProtocolStream: (_stream, _connection) => {}, - registerMetric: (_name, _options) => { - return stubObject({ - increment: () => {}, - reset: () => {}, - decrement: () => {}, - update: () => {}, - timer: () => { - return sinon.spy() - } - }) - }, - registerMetricGroup: (_name, _options) => { - return stubObject({ - increment: () => { - }, - reset: () => {}, - decrement: () => {}, - update: () => {}, - timer: () => { - return sinon.spy() - } - }) - }, - registerCounter: (_name, _options) => { - return stubObject({ - increment: () => {}, - reset: () => {} - }) - }, - registerCounterGroup: (_name, _options) => { - return stubObject({ - increment: () => {}, - reset: () => {} - }) - } - }) + metrics = mockMetrics()() components = { peerId: await createEd25519PeerId(), metrics From 4364358333f84a89ba2972602fab7dac8d3b314d Mon Sep 17 00:00:00 2001 From: chad Date: Tue, 9 May 2023 11:16:04 -0500 Subject: [PATCH 12/12] chore: linting fixes --- package.json | 2 +- src/maconn.ts | 3 +-- src/transport.ts | 6 +++--- test/maconn.browser.spec.ts | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 8df47da..d208d14 100644 --- a/package.json +++ b/package.json @@ -170,7 +170,7 @@ "@libp2p/peer-id-factory": "^2.0.3", "@protobuf-ts/protoc": "^2.9.0", "@types/sinon": "^10.0.14", - "aegir": "^39.0.5", + "aegir": "^39.0.6", "eslint-plugin-etc": "^2.0.2", "it-pair": "^2.0.6", "protons": "^7.0.2", diff --git a/src/maconn.ts b/src/maconn.ts index a622fc1..ffd869c 100644 --- a/src/maconn.ts +++ b/src/maconn.ts @@ -1,10 +1,9 @@ import { logger } from '@libp2p/logger' import { nopSink, nopSource } from './util.js' import type { MultiaddrConnection, MultiaddrConnectionTimeline } from '@libp2p/interface-connection' +import type { CounterGroup } from '@libp2p/interface-metrics' import type { Multiaddr } from '@multiformats/multiaddr' import type { Source, Sink } from 'it-stream-types' -import type { CounterGroup } from '@libp2p/interface-metrics' - const log = logger('libp2p:webrtc:connection') diff --git a/src/transport.ts b/src/transport.ts index ec9e1c5..3038909 100644 --- a/src/transport.ts +++ b/src/transport.ts @@ -9,13 +9,13 @@ import { fromString as uint8arrayFromString } from 'uint8arrays/from-string' import { dataChannelError, inappropriateMultiaddr, unimplemented, invalidArgument } from './error.js' import { WebRTCMultiaddrConnection } from './maconn.js' import { DataChannelMuxerFactory } from './muxer.js' -import type { WebRTCDialOptions } from './options.js' -import type { CounterGroup, Metrics } from '@libp2p/interface-metrics' import { isFirefox } from './peer_transport/util.js' import * as sdp from './sdp.js' import { WebRTCStream } from './stream.js' import { genUfrag } from './util.js' +import type { WebRTCDialOptions } from './options.js' import type { Connection } from '@libp2p/interface-connection' +import type { CounterGroup, Metrics } from '@libp2p/interface-metrics' import type { PeerId } from '@libp2p/interface-peer-id' import type { Multiaddr } from '@multiformats/multiaddr' @@ -239,7 +239,7 @@ export class WebRTCDirectTransport implements Transport { // Therefore, we need to secure an inbound noise connection from the remote. await noise.secureInbound(myPeerId, wrappedDuplex, theirPeerId) - return await options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory }) + return options.upgrader.upgradeOutbound(maConn, { skipProtection: true, skipEncryption: true, muxerFactory }) } /** diff --git a/test/maconn.browser.spec.ts b/test/maconn.browser.spec.ts index 4c1e9d1..4cd542f 100644 --- a/test/maconn.browser.spec.ts +++ b/test/maconn.browser.spec.ts @@ -2,9 +2,9 @@ import { multiaddr } from '@multiformats/multiaddr' import { expect } from 'aegir/chai' -import type { CounterGroup } from '@libp2p/interface-metrics' import { stubObject } from 'sinon-ts' import { WebRTCMultiaddrConnection } from './../src/maconn.js' +import type { CounterGroup } from '@libp2p/interface-metrics' describe('Multiaddr Connection', () => { it('can open and close', async () => {