Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add memory and cpu collect in telemetry #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,17 @@ metrics:
name: bitswap-elu
description: Bitswap Event Loop Utilization
interval: 500

version: 0.1.0
buildDate: "20220307.1423"
memory:
name: bitswap-memory
description: Bitswap Memory Usage
interval: 1000
cpu:
name: bitswap-cpu
description: Bitswap CPU Usage %
interval: 1000

version: 0.3.0
buildDate: "20221216.1122"
```

### Utils
Expand Down
69 changes: 62 additions & 7 deletions src/telemetry.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

import { performance } from 'node:perf_hooks'
import { memoryUsage, cpuUsage } from 'node:process'
import { readFileSync } from 'fs'
import { load as ymlLoad } from 'js-yaml'
import promClient from 'prom-client'

const PERCENTILES = [0.1, 1, 10, 25, 50, 75, 90, 97.5, 99]

class Telemetry {
Expand Down Expand Up @@ -61,13 +63,33 @@ class Telemetry {
}))
}

if (metrics.process?.elu) {
this.collectEventLoopUtilization({
name: metrics.process.elu.name,
description: metrics.process.elu.description,
interval: metrics.process.elu.interval,
registers: [this.allRegistry]
})
if (metrics.process) {
if (metrics.process.elu) {
this.collectEventLoopUtilization({
name: metrics.process.elu.name,
description: metrics.process.elu.description,
interval: metrics.process.elu.interval,
registers: [this.allRegistry]
})
}

if (metrics.process.memory) {
this.collectMemoryUsage({
name: metrics.process.memory.name,
description: metrics.process.memory.description,
interval: metrics.process.memory.interval,
registers: [this.allRegistry]
})
}

if (metrics.process.cpu) {
this.collectCpuUsage({
name: metrics.process.cpu.name,
description: metrics.process.cpu.description,
interval: metrics.process.cpu.interval,
registers: [this.allRegistry]
})
}
}
} catch (err) {
logger.error({ err }, 'error in telemetry constructor')
Expand All @@ -92,6 +114,39 @@ class Telemetry {
}, interval).unref()
}

collectMemoryUsage ({ name, description, interval, registers }) {
const metric = new promClient.Gauge({
name: name.replaceAll('-', '_'),
help: description,
registers
})
this.metrics.set(name, metric)

this.collectMemoryInterval = setInterval(() => {
metric.set(memoryUsage.rss())
}, interval).unref()
}

collectCpuUsage ({ name, description, interval, registers }) {
const metric = new promClient.Gauge({
name: name.replaceAll('-', '_'),
help: description,
registers
})
this.metrics.set(name, metric)

let cpu1 = cpuUsage()
let timer1 = process.hrtime.bigint()
this.collectCpuInterval = setInterval(() => {
const cpu2 = cpuUsage(cpu1)
const timer2 = process.hrtime.bigint()
const v = 100 * cpu2.user / Number((timer2 - timer1) / 1000n)
metric.set(v)
cpu1 = cpu2
timer1 = timer2
}, interval).unref()
}

resetAll () {
this.allRegistry.resetMetrics()
}
Expand Down
12 changes: 10 additions & 2 deletions test/fixtures/process-metrics.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ metrics:
name: bitswap-elu
description: Bitswap Event Loop Utilization
interval: 100
memory:
name: bitswap-memory
description: Bitswap Memory Usage
interval: 100
cpu:
name: bitswap-cpu
description: Bitswap CPU Usage %
interval: 100

version: 0.2.0
buildDate: "20220307.1423"
version: 0.3.0
buildDate: "20221215.1122"
22 changes: 22 additions & 0 deletions test/telemetry.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,28 @@ t.test('Telemetry', async t => {
t.ok(telemetry.collectEluInterval)
})

t.test('should collect memory metric when set from config file', async t => {
const telemetry = new Telemetry({ configFile: processMetricConfigFile, logger })

const result = await telemetry.export()

await setTimeoutAsync(500)

t.ok(result.includes('bitswap_memory'))
t.ok(telemetry.collectMemoryInterval)
})

t.test('should collect cpu metric when set from config file', async t => {
const telemetry = new Telemetry({ configFile: processMetricConfigFile, logger })

const result = await telemetry.export()

await setTimeoutAsync(500)

t.ok(result.includes('bitswap_cpu'))
t.ok(telemetry.collectCpuInterval)
})

t.test('should get a gauge metric value', async t => {
const telemetry = new Telemetry({ configFile: defaultConfigFile, logger })

Expand Down