From e6f646ed361d231ec2306fee40fcb87c18af0c41 Mon Sep 17 00:00:00 2001 From: Alex Potsides Date: Fri, 1 Jul 2022 18:33:17 +0200 Subject: [PATCH] chore: update deps (#1285) --- package.json | 8 +-- src/connection-manager/index.ts | 97 ++++++++++++++++++++++++++++++--- src/metrics/index.ts | 12 ++-- test/metrics/index.spec.ts | 2 +- 4 files changed, 103 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 611e90ee..dbb88013 100644 --- a/package.json +++ b/package.json @@ -98,7 +98,7 @@ }, "dependencies": { "@achingbrain/nat-port-mapper": "^1.0.3", - "@libp2p/components": "^2.0.0", + "@libp2p/components": "^2.0.1", "@libp2p/connection": "^4.0.0", "@libp2p/crypto": "^1.0.0", "@libp2p/interface-address-manager": "^1.0.1", @@ -106,7 +106,7 @@ "@libp2p/interface-connection-encrypter": "^1.0.2", "@libp2p/interface-content-routing": "^1.0.1", "@libp2p/interface-dht": "^1.0.0", - "@libp2p/interface-metrics": "^1.0.2", + "@libp2p/interface-metrics": "^2.0.0", "@libp2p/interface-peer-discovery": "^1.0.0", "@libp2p/interface-peer-id": "^1.0.2", "@libp2p/interface-peer-info": "^1.0.1", @@ -124,7 +124,7 @@ "@libp2p/peer-id-factory": "^1.0.9", "@libp2p/peer-record": "^3.0.0", "@libp2p/peer-store": "^3.0.0", - "@libp2p/tracked-map": "^2.0.0", + "@libp2p/tracked-map": "^2.0.1", "@libp2p/utils": "^3.0.0", "@multiformats/mafmt": "^11.0.2", "@multiformats/multiaddr": "^10.1.8", @@ -178,7 +178,7 @@ "@libp2p/interface-mocks": "^3.0.1", "@libp2p/interop": "^2.0.0", "@libp2p/kad-dht": "^3.0.0", - "@libp2p/mdns": "^2.0.0", + "@libp2p/mdns": "^3.0.0", "@libp2p/mplex": "^4.0.0", "@libp2p/pubsub": "^3.0.1", "@libp2p/tcp": "^3.0.0", diff --git a/src/connection-manager/index.ts b/src/connection-manager/index.ts index 92637a38..0b32c02e 100644 --- a/src/connection-manager/index.ts +++ b/src/connection-manager/index.ts @@ -7,7 +7,6 @@ import retimer from 'retimer' import type { AbortOptions } from '@libp2p/interfaces' import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events' import type { Startable } from '@libp2p/interfaces/startable' -import { trackedMap } from '@libp2p/tracked-map' import { codes } from '../errors.js' import { isPeerId, PeerId } from '@libp2p/interface-peer-id' import { setMaxListeners } from 'events' @@ -36,8 +35,8 @@ const defaultOptions: Partial = { movingAverageInterval: 60000 } +const METRICS_SYSTEM = 'libp2p' const METRICS_COMPONENT = 'connection-manager' -const METRICS_PEER_CONNECTIONS = 'peer-connections' const STARTUP_RECONNECT_TIMEOUT = 60000 export interface ConnectionManagerInit { @@ -162,11 +161,7 @@ export class DefaultConnectionManager extends EventEmitter { + const metric = { + inbound: 0, + outbound: 0 + } + + for (const conns of this.connections.values()) { + for (const conn of conns) { + if (conn.stat.direction === 'inbound') { + metric.inbound++ + } else { + metric.outbound++ + } + } + } + + return metric + } + }) + + // track total number of streams per protocol + this.components.getMetrics()?.updateComponentMetric({ + system: METRICS_SYSTEM, + component: METRICS_COMPONENT, + metric: 'protocol-streams-total', + label: 'protocol', + value: () => { + const metric: Record = {} + + for (const conns of this.connections.values()) { + for (const conn of conns) { + for (const stream of conn.streams) { + const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}` + + metric[key] = (metric[key] ?? 0) + 1 + } + } + } + + return metric + } + }) + + // track 90th percentile of streams per protocol + this.components.getMetrics()?.updateComponentMetric({ + system: METRICS_SYSTEM, + component: METRICS_COMPONENT, + metric: 'protocol-streams-per-connection-90th-percentile', + label: 'protocol', + value: () => { + const allStreams: Record = {} + + for (const conns of this.connections.values()) { + for (const conn of conns) { + const streams: Record = {} + + for (const stream of conn.streams) { + const key = `${stream.stat.direction} ${stream.stat.protocol ?? 'unnegotiated'}` + + streams[key] = (streams[key] ?? 0) + 1 + } + + for (const [protocol, count] of Object.entries(streams)) { + allStreams[protocol] = allStreams[protocol] ?? [] + allStreams[protocol].push(count) + } + } + } + + const metric: Record = {} + + for (let [protocol, counts] of Object.entries(allStreams)) { + counts = counts.sort((a, b) => a - b) + + const index = Math.floor(counts.length * 0.9) + metric[protocol] = counts[index] + } + + return metric + } + }) } isStarted () { diff --git a/src/metrics/index.ts b/src/metrics/index.ts index d5e93a38..6c4429bb 100644 --- a/src/metrics/index.ts +++ b/src/metrics/index.ts @@ -3,7 +3,7 @@ import each from 'it-foreach' import LRU from 'hashlru' import { METRICS as defaultOptions } from '../constants.js' import { DefaultStats, StatsInit } from './stats.js' -import type { ComponentMetricsUpdate, Metrics, Stats, TrackStreamOptions } from '@libp2p/interface-metrics' +import type { ComponentMetricsUpdate, Metrics, Stats, TrackedMetric, TrackStreamOptions } from '@libp2p/interface-metrics' import type { PeerId } from '@libp2p/interface-peer-id' import type { Startable } from '@libp2p/interfaces/startable' import type { Duplex } from 'it-stream-types' @@ -41,7 +41,7 @@ export class DefaultMetrics implements Metrics, Startable { private readonly protocolStats: Map private readonly oldPeers: ReturnType private running: boolean - private readonly systems: Map>> + private readonly systems: Map>> private readonly statsInit: StatsInit constructor (init: MetricsInit) { @@ -115,7 +115,7 @@ export class DefaultMetrics implements Metrics, Startable { } updateComponentMetric (update: ComponentMetricsUpdate) { - const { system = 'libp2p', component, metric, value } = update + const { system = 'libp2p', component, metric, value, label, help } = update if (!this.systems.has(system)) { this.systems.set(system, new Map()) @@ -137,7 +137,11 @@ export class DefaultMetrics implements Metrics, Startable { throw new Error('Unknown metric component') } - componentMetrics.set(metric, value) + componentMetrics.set(metric, { + label, + help, + calculate: typeof value !== 'function' ? () => value : value + }) } /** diff --git a/test/metrics/index.spec.ts b/test/metrics/index.spec.ts index 7bb0062f..b4ef8b01 100644 --- a/test/metrics/index.spec.ts +++ b/test/metrics/index.spec.ts @@ -296,6 +296,6 @@ describe('Metrics', () => { expect(metrics.getComponentMetrics()).to.have.lengthOf(1) expect(metrics.getComponentMetrics().get('libp2p')?.get(component)).to.have.lengthOf(1) - expect(metrics.getComponentMetrics().get('libp2p')?.get(component)?.get(metric)).to.equal(value) + expect(metrics.getComponentMetrics().get('libp2p')?.get(component)?.get(metric)?.calculate()).to.equal(value) }) })