Skip to content

Commit

Permalink
fix!: update @libp2p/interface-metrics to v4 (libp2p#12)
Browse files Browse the repository at this point in the history
Update interface metrics to v4

BREAKING CHANGE: requires @libp2p/interface-metrics v4
  • Loading branch information
achingbrain authored Nov 5, 2022
1 parent e58d7df commit 6a97551
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 92 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
37 changes: 11 additions & 26 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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<K, V> extends Map<K, V> {
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()
}

Expand All @@ -43,28 +35,21 @@ class TrackedMap<K, V> extends Map<K, V> {
}

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 <K, V> (config: CreateTrackedMapOptions): Map<K, V> {
const { system, component, metric, metrics } = config
const { name, metrics } = config
let map: Map<K, V>

if (metrics != null) {
map = new TrackedMap<K, V>({ system, component, metric, metrics })
map = new TrackedMap<K, V>({ name, metrics })
} else {
map = new Map<K, V>()
}
Expand Down
92 changes: 28 additions & 64 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
@@ -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<Metrics>

beforeEach(() => {
updateComponentMetricStub = sinon.stub()

metrics = {
updateComponentMetric: updateComponentMetricStub,
getComponentMetrics: sinon.stub()
}
metrics = stubInterface<Metrics>()
})

it('should return a map with metrics', () => {
const system = 'system'
const component = 'component'
const metric = 'metric'
const name = 'system_component_metric'
const metric = stubInterface<Metric>()
// @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<Metric>()
// @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<Metric>()
// @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)
Expand Down

0 comments on commit 6a97551

Please sign in to comment.