feat: allow per-component metrics to be collected (#1061)

Implements the idea from #1060 - allows us to get some insight into what's happening in a libp2p node out side of just bandwidth stats.

Configures a few default metrics if metrics are enabled - current connections, the state of the dial queue, etc.

Also makes the `Metrics` class not depend on the `ConnectionManager` class, otherwise we can't collect simple metrics from the connection manager class due to the circular dependency.
This commit is contained in:
Alex Potsides
2021-12-15 08:03:09 +00:00
committed by GitHub
parent d172d0d952
commit 2f0b311df7
6 changed files with 78 additions and 26 deletions

View File

@ -3,9 +3,6 @@
const { expect } = require('aegir/utils/chai')
const sinon = require('sinon')
const { EventEmitter } = require('events')
const { randomBytes } = require('libp2p-crypto')
const duplexPair = require('it-pair/duplex')
const pipe = require('it-pipe')
@ -34,8 +31,7 @@ describe('Metrics', () => {
const [local, remote] = duplexPair()
const metrics = new Metrics({
computeThrottleMaxQueueSize: 1, // compute after every message
movingAverageIntervals: [10, 100, 1000],
connectionManager: new EventEmitter()
movingAverageIntervals: [10, 100, 1000]
})
metrics.trackStream({
@ -70,8 +66,7 @@ describe('Metrics', () => {
const [local, remote] = duplexPair()
const metrics = new Metrics({
computeThrottleMaxQueueSize: 1, // compute after every message
movingAverageIntervals: [10, 100, 1000],
connectionManager: new EventEmitter()
movingAverageIntervals: [10, 100, 1000]
})
metrics.trackStream({
@ -119,8 +114,7 @@ describe('Metrics', () => {
const [local2, remote2] = duplexPair()
const metrics = new Metrics({
computeThrottleMaxQueueSize: 1, // compute after every message
movingAverageIntervals: [10, 100, 1000],
connectionManager: new EventEmitter()
movingAverageIntervals: [10, 100, 1000]
})
const protocol = '/echo/1.0.0'
metrics.start()
@ -175,8 +169,7 @@ describe('Metrics', () => {
const [local, remote] = duplexPair()
const metrics = new Metrics({
computeThrottleMaxQueueSize: 1, // compute after every message
movingAverageIntervals: [10, 100, 1000],
connectionManager: new EventEmitter()
movingAverageIntervals: [10, 100, 1000]
})
metrics.start()
@ -231,8 +224,7 @@ describe('Metrics', () => {
}))
const metrics = new Metrics({
maxOldPeersRetention: 5, // Only keep track of 5
connectionManager: new EventEmitter()
maxOldPeersRetention: 5 // Only keep track of 5
})
// Clone so trackedPeers isn't modified
@ -262,4 +254,22 @@ describe('Metrics', () => {
expect(spy).to.have.property('callCount', 1)
}
})
it('should allow components to track metrics', () => {
const metrics = new Metrics({
maxOldPeersRetention: 5 // Only keep track of 5
})
expect(metrics.getComponentMetrics()).to.be.empty()
const component = 'my-component'
const metric = 'some-metric'
const value = 1
metrics.updateComponentMetric(component, metric, value)
expect(metrics.getComponentMetrics()).to.have.lengthOf(1)
expect(metrics.getComponentMetrics().get(component)).to.have.lengthOf(1)
expect(metrics.getComponentMetrics().get(component).get(metric)).to.equal(value)
})
})