dd-trace
Version:
Datadog APM tracing client for JavaScript
107 lines (91 loc) • 2.62 kB
JavaScript
// Load binding first to not import other modules if it throws
const libdatadog = require('@datadog/libdatadog')
const binding = libdatadog.load('crashtracker')
const log = require('../log')
const { URL } = require('url')
const pkg = require('../../../../package.json')
class Crashtracker {
#started = false
configure (config) {
if (!this.#started) return
try {
binding.updateConfig(this.#getConfig(config))
binding.updateMetadata(this.#getMetadata(config))
} catch (e) {
log.error('Error configuring crashtracker', e)
}
}
start (config) {
if (this.#started) return this.configure(config)
this.#started = true
try {
binding.init(
this.#getConfig(config),
this.#getReceiverConfig(),
this.#getMetadata(config)
)
} catch (e) {
log.error('Error initialising crashtracker', e)
}
}
withProfilerSerializing (f) {
binding.beginProfilerSerializing()
try {
return f()
} finally {
binding.endProfilerSerializing()
}
}
// TODO: Send only configured values when defaults are fixed.
#getConfig (config) {
const { hostname = '127.0.0.1', port = 8126 } = config
const url = config.url || new URL(`http://${hostname}:${port}`)
return {
additional_files: [],
create_alt_stack: true,
use_alt_stack: true,
endpoint: {
// TODO: Use the string directly when deserialization is fixed.
url: {
scheme: url.protocol.slice(0, -1),
authority: url.protocol === 'unix:'
? Buffer.from(url.pathname).toString('hex')
: url.host,
path_and_query: ''
},
timeout_ms: 3000
},
timeout_ms: 5000,
// TODO: Use `EnabledWithSymbolsInReceiver` instead for Linux when fixed.
resolve_frames: 'EnabledWithInprocessSymbols'
}
}
#getMetadata (config) {
const tags = Object.keys(config.tags).map(key => `${key}:${config.tags[key]}`)
return {
library_name: pkg.name,
library_version: pkg.version,
family: 'nodejs',
tags: [
...tags,
'is_crash:true',
'language:javascript',
`library_version:${pkg.version}`,
'runtime:nodejs',
`runtime_version:${process.versions.node}`,
'severity:crash'
]
}
}
#getReceiverConfig () {
return {
args: [],
env: [],
path_to_receiver_binary: libdatadog.find('crashtracker-receiver', true),
stderr_filename: null,
stdout_filename: null
}
}
}
module.exports = new Crashtracker()