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

stop using native metrics for newer node versions #4952

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
109 changes: 93 additions & 16 deletions packages/dd-trace/src/runtime_metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@ const os = require('os')
const { DogStatsDClient } = require('./dogstatsd')
const log = require('./log')
const Histogram = require('./histogram')
const { performance } = require('perf_hooks')
const { performance, monitorEventLoopDelay } = require('perf_hooks')

const { NODE_MAJOR, NODE_MINOR } = require('../../../version')
const INTERVAL = 10 * 1000

const allMetricsAvailable = NODE_MAJOR > 22 ||
(NODE_MAJOR === 22 && NODE_MAJOR >= 8) ||
rochdev marked this conversation as resolved.
Show resolved Hide resolved
(NODE_MAJOR === 20 && NODE_MINOR >= 18)

let nativeMetrics = null
let loopDelayHistogram = null
let gcProfiler = null

let interval
let client
Expand All @@ -21,40 +28,59 @@ let gauges
let counters
let histograms
let elu
let lastLoopCount

reset()

module.exports = {
start (config) {
const clientConfig = DogStatsDClient.generateClientConfig(config)

try {
nativeMetrics = require('@datadog/native-metrics')
nativeMetrics.start()
} catch (e) {
log.error(e)
nativeMetrics = null
}

client = new DogStatsDClient(clientConfig)

time = process.hrtime()

if (nativeMetrics) {
interval = setInterval(() => {
captureCommonMetrics()
captureNativeMetrics()
client.flush()
}, INTERVAL)
} else {
if (allMetricsAvailable) { // All metrics are available from Node, skip native.
cpuUsage = process.cpuUsage()
lastLoopCount = performance.nodeTiming.uvMetricsInfo.loopCount
loopDelayHistogram = monitorEventLoopDelay()
loopDelayHistogram.enable()
gcProfiler = new v8.GCProfiler()
gcProfiler.start()

interval = setInterval(() => {
captureCommonMetrics()
captureCpuUsage()
captureHeapSpace()
captureGCMetrics()
captureEventLoopMetrics()
client.flush()
}, INTERVAL)
} else {
try {
nativeMetrics = require('@datadog/native-metrics')
nativeMetrics.start()
} catch (e) {
log.error(e)
nativeMetrics = null
}

if (nativeMetrics) {
interval = setInterval(() => {
captureCommonMetrics()
captureNativeMetrics()
client.flush()
}, INTERVAL)
} else {
cpuUsage = process.cpuUsage()

interval = setInterval(() => {
captureCommonMetrics()
captureCpuUsage()
captureHeapSpace()
client.flush()
}, INTERVAL)
}
}

interval.unref()
Expand Down Expand Up @@ -138,6 +164,11 @@ function reset () {
counters = {}
histograms = {}
nativeMetrics = null
lastLoopCount = null
loopDelayHistogram && loopDelayHistogram.disable()
loopDelayHistogram = null
gcProfiler && gcProfiler.stop()
gcProfiler = null
}

function captureCpuUsage () {
Expand Down Expand Up @@ -203,6 +234,47 @@ function captureHeapSpace () {
}
}

function captureEventLoopMetrics () {
const totalLoopCount = performance.nodeTiming.uvMetricsInfo.loopCount
const loopCount = totalLoopCount - lastLoopCount

histogram('runtime.node.event_loop.delay', {
max: loopDelayHistogram.max,
min: loopDelayHistogram.min,
sum: loopDelayHistogram.mean * loopCount,
avg: loopDelayHistogram.mean,
median: loopDelayHistogram.percentile(50),
p95: loopDelayHistogram.percentile(95),
count: loopCount
})

lastLoopCount = totalLoopCount

loopDelayHistogram.reset()
}

function captureGCMetrics () {
const profile = gcProfiler.stop()
const pauseAll = new Histogram()
const pause = {}

for (const stat of profile.statistics) {
const type = stat.gcType.replace(/([a-z])([A-Z])/g, '$1_$2').toLowerCase()

pause[type] = pause[type] || new Histogram()
pause[type].record(stat.cost)
pauseAll.record(stat.cost)
}

histogram('runtime.node.gc.pause', pauseAll)

for (const type in pause) {
histogram('runtime.node.gc.pause.by.type', pause[type], [`gc_type:${type}`])
}

gcProfiler.start()
}

function captureGauges () {
Object.keys(gauges).forEach(name => {
gauges[name].forEach((value, tag) => {
Expand Down Expand Up @@ -297,6 +369,11 @@ function captureNativeMetrics () {
function histogram (name, stats, tags) {
tags = [].concat(tags)

// Stats can contain garbage data when a value was never recorded.
if (stats.count === 0) {
stats = { max: 0, min: 0, sum: 0, avg: 0, median: 0, p95: 0, count: 0 }
}

client.gauge(`${name}.min`, stats.min, tags)
client.gauge(`${name}.max`, stats.max, tags)
client.increment(`${name}.sum`, stats.sum, tags)
Expand Down
Loading