-
Notifications
You must be signed in to change notification settings - Fork 407
Expand file tree
/
Copy pathcrashtracker.js
More file actions
142 lines (123 loc) · 3.62 KB
/
Copy pathcrashtracker.js
File metadata and controls
142 lines (123 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
'use strict'
const { EOL, platform } = require('node:os')
// Load binding first to not import other modules if it throws
const libdatadog = require('@datadog/libdatadog')
const binding = libdatadog.load('crashtracker')
const { channel } = require('dc-polyfill')
const log = require('../log')
const pkg = require('../../../../package.json')
const processTags = require('../process-tags')
const identityRefreshChannel = channel('datadog:identity:refresh')
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)
}
}
/**
* @param {import('../config/config-base')} config - Tracer configuration
*/
start (config) {
if (this.#started) return this.configure(config)
try {
binding.init(
this.#getConfig(config),
this.#getReceiverConfig(),
this.#getMetadata(config)
)
this.#started = true
this.#trackUnhandledExceptions()
identityRefreshChannel.subscribe((config) => this.configure(config))
} catch (e) {
log.error('Error initializing crashtracker', e)
}
}
#trackUnhandledExceptions () {
process.once('uncaughtExceptionMonitor', (error, origin) => {
try {
binding.reportUncaughtExceptionMonitor(error, origin)
} catch (e) {
process.stderr.write(`Error reporting uncaught exception to crashtracker: ${e.toString()}${EOL}`)
}
})
}
withProfilerSerializing (f) {
binding.beginProfilerSerializing()
try {
return f()
} finally {
binding.endProfilerSerializing()
}
}
// TODO: Send only configured values when defaults are fixed.
/**
* @param {import('../config/config-base')} config - Tracer configuration
*/
#getConfig (config) {
const url = config.url
// Out-of-process symbolication currently works on
// Linux only, does not work on Mac.
const resolveMode = platform() === 'linux'
? 'EnabledWithSymbolsInReceiver'
: 'EnabledWithInprocessSymbols'
return {
additional_files: [],
collect_all_threads: true,
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: { secs: 5, nanos: 0 },
demangle_names: true,
signals: [],
resolve_frames: resolveMode,
}
}
#getMetadata (config) {
const tags = Object.keys(config.tags).map(key => `${key}:${config.tags[key]}`)
// Add process tags to the tags array
for (const [key, value] of processTags.tags) {
if (value !== undefined) {
tags.push(`${key}:${value}`)
}
}
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()