diff --git a/package.json b/package.json index ba3b2796a9..261e42cf8c 100644 --- a/package.json +++ b/package.json @@ -138,10 +138,11 @@ "release": "aegir release" }, "dependencies": { - "@libp2p/interface-metrics": "^3.0.0" + "@libp2p/interface-metrics": "^4.0.0" }, "devDependencies": { "aegir": "^37.0.7", - "sinon": "^14.0.0" + "sinon": "^14.0.0", + "sinon-ts": "^1.0.0" } } diff --git a/src/index.ts b/src/index.ts index b488b2d3d9..e8c852b9c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,27 +1,19 @@ -import type { ComponentMetricsTracker } from '@libp2p/interface-metrics' +import type { Metric, Metrics } from '@libp2p/interface-metrics' export interface TrackedMapInit { - metrics: ComponentMetricsTracker - system?: string - component: string - metric: string + name: string + metrics: Metrics } class TrackedMap extends Map { - private readonly system: string - private readonly component: string - private readonly metric: string - private readonly metrics: ComponentMetricsTracker + private readonly metric: Metric constructor (init: TrackedMapInit) { super() - const { system, component, metric, metrics } = init - this.system = system ?? 'libp2p' - this.component = component - this.metric = metric - this.metrics = metrics + const { name, metrics } = init + this.metric = metrics.registerMetric(name) this.updateComponentMetric() } @@ -43,28 +35,21 @@ class TrackedMap extends Map { } private updateComponentMetric () { - this.metrics.updateComponentMetric({ - system: this.system, - component: this.component, - metric: this.metric, - value: this.size - }) + this.metric.update(this.size) } } export interface CreateTrackedMapOptions { - metrics?: ComponentMetricsTracker - system?: string - component: string - metric: string + name: string + metrics?: Metrics } export function trackedMap (config: CreateTrackedMapOptions): Map { - const { system, component, metric, metrics } = config + const { name, metrics } = config let map: Map if (metrics != null) { - map = new TrackedMap({ system, component, metric, metrics }) + map = new TrackedMap({ name, metrics }) } else { map = new Map() } diff --git a/test/index.spec.ts b/test/index.spec.ts index 93c36a4eb1..d6f38b56e1 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -1,101 +1,65 @@ import { expect } from 'aegir/chai' import { trackedMap } from '../src/index.js' -import sinon from 'sinon' -import type { ComponentMetricsTracker, ComponentMetricsUpdate } from '@libp2p/interface-metrics' -import type { SinonStub } from 'sinon' +import type { SinonStubbedInstance } from 'sinon' +import type { Metric, Metrics } from '@libp2p/interface-metrics' +import { stubInterface } from 'sinon-ts' describe('tracked-map', () => { - let metrics: ComponentMetricsTracker - let updateComponentMetricStub: SinonStub<[ComponentMetricsUpdate], void> + let metrics: SinonStubbedInstance beforeEach(() => { - updateComponentMetricStub = sinon.stub() - - metrics = { - updateComponentMetric: updateComponentMetricStub, - getComponentMetrics: sinon.stub() - } + metrics = stubInterface() }) it('should return a map with metrics', () => { - const system = 'system' - const component = 'component' - const metric = 'metric' + const name = 'system_component_metric' + const metric = stubInterface() + // @ts-expect-error the wrong overload is selected + metrics.registerMetric.withArgs(name).returns(metric) const map = trackedMap({ - metrics, - system, - component, - metric + name, + metrics }) expect(map).to.be.an.instanceOf(Map) - expect(updateComponentMetricStub.calledWith({ - system, - component, - metric, - value: 0 - })).to.be.true() + expect(metrics.registerMetric.calledWith(name)).to.be.true() }) it('should return a map without metrics', () => { - const system = 'system' - const component = 'component' - const metric = 'metric' + const name = 'system_component_metric' + const metric = stubInterface() + // @ts-expect-error the wrong overload is selected + metrics.registerMetric.withArgs(name).returns(metric) const map = trackedMap({ - system, - component, - metric + name }) expect(map).to.be.an.instanceOf(Map) - expect(updateComponentMetricStub.called).to.be.false() - }) - - it('should default system to libp2p', () => { - const component = 'component' - const metric = 'metric' - - const map = trackedMap({ - metrics, - component, - metric - }) - - expect(map).to.be.an.instanceOf(Map) - expect(updateComponentMetricStub.calledWith({ - system: 'libp2p', - component, - metric, - value: 0 - })).to.be.true() + expect(metrics.registerMetric.called).to.be.false() }) it('should track metrics', () => { - const system = 'system' - const component = 'component' - const metric = 'metric' + const name = 'system_component_metric' let value = 0 let callCount = 0 - metrics.updateComponentMetric = (data) => { - expect(data.system).to.equal(system) - expect(data.component).to.equal(component) - expect(data.metric).to.equal(metric) + const metric = stubInterface() + // @ts-expect-error the wrong overload is selected + metrics.registerMetric.withArgs(name).returns(metric) - if (typeof data.value === 'number') { - value = data.value + metric.update.callsFake((v) => { + if (typeof v === 'number') { + value = v } callCount++ - } + }) const map = trackedMap({ - metrics, - system, - component, - metric + name, + metrics }) expect(map).to.be.an.instanceOf(Map)