Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

fix!: update @libp2p/interface-metrics to v4 #12

Merged
merged 1 commit into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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