Skip to content

Commit 6a97551

Browse files
authored
fix!: update @libp2p/interface-metrics to v4 (libp2p#12)
Update interface metrics to v4 BREAKING CHANGE: requires @libp2p/interface-metrics v4
1 parent e58d7df commit 6a97551

File tree

3 files changed

+42
-92
lines changed

3 files changed

+42
-92
lines changed

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@
138138
"release": "aegir release"
139139
},
140140
"dependencies": {
141-
"@libp2p/interface-metrics": "^3.0.0"
141+
"@libp2p/interface-metrics": "^4.0.0"
142142
},
143143
"devDependencies": {
144144
"aegir": "^37.0.7",
145-
"sinon": "^14.0.0"
145+
"sinon": "^14.0.0",
146+
"sinon-ts": "^1.0.0"
146147
}
147148
}

src/index.ts

+11-26
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
1-
import type { ComponentMetricsTracker } from '@libp2p/interface-metrics'
1+
import type { Metric, Metrics } from '@libp2p/interface-metrics'
22

33
export interface TrackedMapInit {
4-
metrics: ComponentMetricsTracker
5-
system?: string
6-
component: string
7-
metric: string
4+
name: string
5+
metrics: Metrics
86
}
97

108
class TrackedMap<K, V> extends Map<K, V> {
11-
private readonly system: string
12-
private readonly component: string
13-
private readonly metric: string
14-
private readonly metrics: ComponentMetricsTracker
9+
private readonly metric: Metric
1510

1611
constructor (init: TrackedMapInit) {
1712
super()
1813

19-
const { system, component, metric, metrics } = init
20-
this.system = system ?? 'libp2p'
21-
this.component = component
22-
this.metric = metric
23-
this.metrics = metrics
14+
const { name, metrics } = init
2415

16+
this.metric = metrics.registerMetric(name)
2517
this.updateComponentMetric()
2618
}
2719

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

4537
private updateComponentMetric () {
46-
this.metrics.updateComponentMetric({
47-
system: this.system,
48-
component: this.component,
49-
metric: this.metric,
50-
value: this.size
51-
})
38+
this.metric.update(this.size)
5239
}
5340
}
5441

5542
export interface CreateTrackedMapOptions {
56-
metrics?: ComponentMetricsTracker
57-
system?: string
58-
component: string
59-
metric: string
43+
name: string
44+
metrics?: Metrics
6045
}
6146

6247
export function trackedMap <K, V> (config: CreateTrackedMapOptions): Map<K, V> {
63-
const { system, component, metric, metrics } = config
48+
const { name, metrics } = config
6449
let map: Map<K, V>
6550

6651
if (metrics != null) {
67-
map = new TrackedMap<K, V>({ system, component, metric, metrics })
52+
map = new TrackedMap<K, V>({ name, metrics })
6853
} else {
6954
map = new Map<K, V>()
7055
}

test/index.spec.ts

+28-64
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,65 @@
11
import { expect } from 'aegir/chai'
22
import { trackedMap } from '../src/index.js'
3-
import sinon from 'sinon'
4-
import type { ComponentMetricsTracker, ComponentMetricsUpdate } from '@libp2p/interface-metrics'
5-
import type { SinonStub } from 'sinon'
3+
import type { SinonStubbedInstance } from 'sinon'
4+
import type { Metric, Metrics } from '@libp2p/interface-metrics'
5+
import { stubInterface } from 'sinon-ts'
66

77
describe('tracked-map', () => {
8-
let metrics: ComponentMetricsTracker
9-
let updateComponentMetricStub: SinonStub<[ComponentMetricsUpdate], void>
8+
let metrics: SinonStubbedInstance<Metrics>
109

1110
beforeEach(() => {
12-
updateComponentMetricStub = sinon.stub()
13-
14-
metrics = {
15-
updateComponentMetric: updateComponentMetricStub,
16-
getComponentMetrics: sinon.stub()
17-
}
11+
metrics = stubInterface<Metrics>()
1812
})
1913

2014
it('should return a map with metrics', () => {
21-
const system = 'system'
22-
const component = 'component'
23-
const metric = 'metric'
15+
const name = 'system_component_metric'
16+
const metric = stubInterface<Metric>()
17+
// @ts-expect-error the wrong overload is selected
18+
metrics.registerMetric.withArgs(name).returns(metric)
2419

2520
const map = trackedMap({
26-
metrics,
27-
system,
28-
component,
29-
metric
21+
name,
22+
metrics
3023
})
3124

3225
expect(map).to.be.an.instanceOf(Map)
33-
expect(updateComponentMetricStub.calledWith({
34-
system,
35-
component,
36-
metric,
37-
value: 0
38-
})).to.be.true()
26+
expect(metrics.registerMetric.calledWith(name)).to.be.true()
3927
})
4028

4129
it('should return a map without metrics', () => {
42-
const system = 'system'
43-
const component = 'component'
44-
const metric = 'metric'
30+
const name = 'system_component_metric'
31+
const metric = stubInterface<Metric>()
32+
// @ts-expect-error the wrong overload is selected
33+
metrics.registerMetric.withArgs(name).returns(metric)
4534

4635
const map = trackedMap({
47-
system,
48-
component,
49-
metric
36+
name
5037
})
5138

5239
expect(map).to.be.an.instanceOf(Map)
53-
expect(updateComponentMetricStub.called).to.be.false()
54-
})
55-
56-
it('should default system to libp2p', () => {
57-
const component = 'component'
58-
const metric = 'metric'
59-
60-
const map = trackedMap({
61-
metrics,
62-
component,
63-
metric
64-
})
65-
66-
expect(map).to.be.an.instanceOf(Map)
67-
expect(updateComponentMetricStub.calledWith({
68-
system: 'libp2p',
69-
component,
70-
metric,
71-
value: 0
72-
})).to.be.true()
40+
expect(metrics.registerMetric.called).to.be.false()
7341
})
7442

7543
it('should track metrics', () => {
76-
const system = 'system'
77-
const component = 'component'
78-
const metric = 'metric'
44+
const name = 'system_component_metric'
7945
let value = 0
8046
let callCount = 0
8147

82-
metrics.updateComponentMetric = (data) => {
83-
expect(data.system).to.equal(system)
84-
expect(data.component).to.equal(component)
85-
expect(data.metric).to.equal(metric)
48+
const metric = stubInterface<Metric>()
49+
// @ts-expect-error the wrong overload is selected
50+
metrics.registerMetric.withArgs(name).returns(metric)
8651

87-
if (typeof data.value === 'number') {
88-
value = data.value
52+
metric.update.callsFake((v) => {
53+
if (typeof v === 'number') {
54+
value = v
8955
}
9056

9157
callCount++
92-
}
58+
})
9359

9460
const map = trackedMap({
95-
metrics,
96-
system,
97-
component,
98-
metric
61+
name,
62+
metrics
9963
})
10064

10165
expect(map).to.be.an.instanceOf(Map)

0 commit comments

Comments
 (0)