diff --git a/index.js b/index.js index ea8472cc..5bb147e0 100644 --- a/index.js +++ b/index.js @@ -14,6 +14,7 @@ const { hash, createKeyPair } = require('./lib/crypto') const RawStreamSet = require('./lib/raw-stream-set') const ConnectionPool = require('./lib/connection-pool') const { STREAM_NOT_CONNECTED } = require('./lib/errors') +const { createTracer } = require('hypertrace') class HyperDHT extends DHT { constructor (opts = {}) { @@ -24,6 +25,7 @@ class HyperDHT extends DHT { const { router, persistent } = defaultCacheOpts(opts) + this.tracer = createTracer(this) this.defaultKeyPair = opts.keyPair || createKeyPair(opts.seed) this.listening = new Set() @@ -51,10 +53,12 @@ class HyperDHT extends DHT { } connect (remotePublicKey, opts) { + this.tracer.trace('connect') return connect(this, remotePublicKey, opts) } createServer (opts, onconnection) { + this.tracer.trace('createServer') if (typeof opts === 'function') return this.createServer({}, opts) if (opts && opts.onconnection) onconnection = opts.onconnection const s = new Server(this, opts) @@ -67,6 +71,7 @@ class HyperDHT extends DHT { } async resume () { + this.tracer.trace('resume') await super.resume() const resuming = [] for (const server of this.listening) resuming.push(server.resume()) @@ -74,6 +79,7 @@ class HyperDHT extends DHT { } async suspend () { + this.tracer.trace('suspend') const suspending = [] for (const server of this.listening) suspending.push(server.suspend()) await Promise.allSettled(suspending) @@ -146,6 +152,7 @@ class HyperDHT extends DHT { } findPeer (publicKey, opts = {}) { + this.tracer.trace('findPeer') const target = opts.hash === false ? publicKey : hash(publicKey) opts = { ...opts, map: mapFindPeer } return this.query({ target, command: COMMANDS.FIND_PEER, value: null }, opts) diff --git a/lib/router.js b/lib/router.js index a5fdb155..17928ec3 100644 --- a/lib/router.js +++ b/lib/router.js @@ -5,6 +5,7 @@ const b4a = require('b4a') const { handshake, holepunch } = require('./messages') const { COMMANDS } = require('./constants') const { BAD_HANDSHAKE_REPLY, BAD_HOLEPUNCH_REPLY } = require('./errors') +const { createTracer } = require('hypertrace') const FROM_CLIENT = 0 const FROM_SERVER = 1 @@ -19,6 +20,7 @@ const REPLY = 4 module.exports = class Router { constructor (dht, opts) { + this.tracer = createTracer(this, { parent: dht.tracer }) this.dht = dht this.forwards = new Cache(opts.forwards) } @@ -44,6 +46,7 @@ module.exports = class Router { } async peerHandshake (target, { noise, peerAddress, relayAddress, socket, session }, to) { + this.tracer.trace('peerHandshake', { peerAddress, relayAddress, socket, session, to }) const dht = this.dht const requestValue = c.encode(handshake, { @@ -131,6 +134,7 @@ module.exports = class Router { } async peerHolepunch (target, { id, payload, peerAddress, socket, session }, to) { + this.tracer.trace('peerHolepunch', { id, peerAddress, socket, session, to }) const dht = this.dht const requestValue = c.encode(holepunch, { mode: FROM_CLIENT, diff --git a/package.json b/package.json index 03657346..45d401e5 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "dht-rpc": "^6.9.0", "events": "^3.3.0", "hypercore-crypto": "^3.3.0", + "hypertrace": "^1.2.1", "noise-curve-ed": "^2.0.0", "noise-handshake": "^3.0.0", "record-cache": "^1.1.1",