Skip to content

Commit fa955af

Browse files
committed
Add hostname tag to V8 probe gauges
Add a tag containing the hostname to the V8 probe gauges. Before this change, hosts would override each others' values. Now each of the hosts' values is given for its own hostname.
1 parent bca89ac commit fa955af

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
bump: "patch"
3+
type: "fix"
4+
---
5+
6+
Add a hostname tag to V8 probe metrics. This fixes an issue where metrics' values
7+
would be overriden between different hosts.

packages/nodejs/src/__tests__/probes.test.ts

+60
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
import { Metrics } from "../interfaces"
12
import { BaseProbes as Probes } from "../probes"
3+
import * as v8 from "../probes/v8"
4+
import os from "os"
5+
import { BaseClient } from "../client"
26

37
describe("Probes", () => {
48
let probes: Probes
@@ -18,4 +22,60 @@ describe("Probes", () => {
1822
jest.runOnlyPendingTimers()
1923
expect(fn).toHaveBeenCalled()
2024
})
25+
26+
describe("v8 probe", () => {
27+
let meterMock: Metrics
28+
let setGaugeMock: jest.Mock
29+
30+
beforeEach(() => {
31+
setGaugeMock = jest.fn()
32+
33+
meterMock = ({
34+
setGauge: setGaugeMock
35+
} as unknown) as Metrics
36+
})
37+
38+
function registerV8Probe(hostname?: string) {
39+
new BaseClient({ hostname: hostname })
40+
41+
let { PROBE_NAME, init } = v8
42+
probes.register(PROBE_NAME, init(meterMock))
43+
}
44+
45+
it("reports v8 heap statistics with hostnames", () => {
46+
registerV8Probe("MyHostname")
47+
48+
jest.runOnlyPendingTimers()
49+
50+
let gaugeNames = [
51+
"nodejs_total_heap_size",
52+
"nodejs_total_heap_size_executable",
53+
"nodejs_total_physical_size",
54+
"nodejs_used_heap_size",
55+
"nodejs_malloced_memory",
56+
"nodejs_number_of_native_contexts",
57+
"nodejs_number_of_detached_contexts"
58+
]
59+
60+
gaugeNames.forEach(gaugeName => {
61+
expect(setGaugeMock).toBeCalledWith(gaugeName, expect.any(Number), {
62+
hostname: "MyHostname"
63+
})
64+
})
65+
})
66+
67+
it("uses the system hostname if none is provided", () => {
68+
registerV8Probe()
69+
70+
jest.runOnlyPendingTimers()
71+
72+
expect(setGaugeMock).toBeCalledWith(
73+
expect.any(String),
74+
expect.any(Number),
75+
{
76+
hostname: os.hostname()
77+
}
78+
)
79+
})
80+
})
2181
})
+14-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import { Metrics } from "../../interfaces"
22
import v8 from "v8"
3+
import os from "os"
4+
import { BaseClient } from "../../client"
35

46
export const PROBE_NAME = "v8_stats"
57

68
export function init(meter: Metrics) {
9+
function setGauge(key: string, value: number) {
10+
let hostname = BaseClient.config.data.hostname || os.hostname()
11+
meter.setGauge(key, value, { hostname })
12+
}
13+
714
return function () {
815
const {
916
total_heap_size,
@@ -15,16 +22,12 @@ export function init(meter: Metrics) {
1522
number_of_detached_contexts
1623
} = v8.getHeapStatistics()
1724

18-
meter
19-
.setGauge("nodejs_total_heap_size", total_heap_size)
20-
.setGauge("nodejs_total_heap_size_executable", total_heap_size_executable)
21-
.setGauge("nodejs_total_physical_size", total_physical_size)
22-
.setGauge("nodejs_used_heap_size", used_heap_size)
23-
.setGauge("nodejs_malloced_memory", malloced_memory)
24-
.setGauge("nodejs_number_of_native_contexts", number_of_native_contexts)
25-
.setGauge(
26-
"nodejs_number_of_detached_contexts",
27-
number_of_detached_contexts
28-
)
25+
setGauge("nodejs_total_heap_size", total_heap_size)
26+
setGauge("nodejs_total_heap_size_executable", total_heap_size_executable)
27+
setGauge("nodejs_total_physical_size", total_physical_size)
28+
setGauge("nodejs_used_heap_size", used_heap_size)
29+
setGauge("nodejs_malloced_memory", malloced_memory)
30+
setGauge("nodejs_number_of_native_contexts", number_of_native_contexts)
31+
setGauge("nodejs_number_of_detached_contexts", number_of_detached_contexts)
2932
}
3033
}

0 commit comments

Comments
 (0)