-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add crashtracking with libdatadog native binding (#4692)
- Loading branch information
Showing
13 changed files
with
385 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
'use strict' | ||
|
||
// 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 { | ||
constructor () { | ||
this._started = false | ||
} | ||
|
||
configure (config) { | ||
if (!this._started) return | ||
|
||
try { | ||
binding.updateConfig(this._getConfig(config)) | ||
binding.updateMetadata(this._getMetadata(config)) | ||
} catch (e) { | ||
log.error(e) | ||
} | ||
} | ||
|
||
start (config) { | ||
if (this._started) return this.configure(config) | ||
|
||
this._started = true | ||
|
||
try { | ||
binding.init( | ||
this._getConfig(config), | ||
this._getReceiverConfig(config), | ||
this._getMetadata(config) | ||
) | ||
} catch (e) { | ||
log.error(e) | ||
} | ||
} | ||
|
||
// 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: 0, | ||
// 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', | ||
'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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
'use strict' | ||
|
||
const { isMainThread } = require('worker_threads') | ||
const log = require('../log') | ||
|
||
if (isMainThread) { | ||
try { | ||
module.exports = require('./crashtracker') | ||
} catch (e) { | ||
log.warn(e.message) | ||
module.exports = require('./noop') | ||
} | ||
} else { | ||
module.exports = require('./noop') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
class NoopCrashtracker { | ||
configure () {} | ||
start () {} | ||
} | ||
|
||
module.exports = new NoopCrashtracker() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
packages/dd-trace/test/crashtracking/crashtracker.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
|
||
require('../setup/tap') | ||
|
||
describe('crashtracking', () => { | ||
describe('crashtracker', () => { | ||
let crashtracker | ||
let binding | ||
let config | ||
let libdatadog | ||
let log | ||
|
||
beforeEach(() => { | ||
libdatadog = require('@datadog/libdatadog') | ||
|
||
binding = libdatadog.load('crashtracker') | ||
|
||
config = { | ||
port: 7357, | ||
tags: { | ||
foo: 'bar' | ||
} | ||
} | ||
|
||
log = { | ||
error: sinon.stub() | ||
} | ||
|
||
sinon.spy(binding, 'init') | ||
sinon.spy(binding, 'updateConfig') | ||
sinon.spy(binding, 'updateMetadata') | ||
|
||
crashtracker = proxyquire('../../src/crashtracking/crashtracker', { | ||
'../log': log | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
binding.init.restore() | ||
binding.updateConfig.restore() | ||
binding.updateMetadata.restore() | ||
}) | ||
|
||
describe('start', () => { | ||
it('should initialize the binding', () => { | ||
crashtracker.start(config) | ||
|
||
expect(binding.init).to.have.been.called | ||
expect(log.error).to.not.have.been.called | ||
}) | ||
|
||
it('should initialize the binding only once', () => { | ||
crashtracker.start(config) | ||
crashtracker.start(config) | ||
|
||
expect(binding.init).to.have.been.calledOnce | ||
}) | ||
|
||
it('should reconfigure when started multiple times', () => { | ||
crashtracker.start(config) | ||
crashtracker.start(config) | ||
|
||
expect(binding.updateConfig).to.have.been.called | ||
expect(binding.updateMetadata).to.have.been.called | ||
}) | ||
|
||
it('should handle errors', () => { | ||
crashtracker.start(null) | ||
|
||
expect(() => crashtracker.start(config)).to.not.throw() | ||
}) | ||
}) | ||
|
||
describe('configure', () => { | ||
it('should reconfigure the binding when started', () => { | ||
crashtracker.start(config) | ||
crashtracker.configure(config) | ||
|
||
expect(binding.updateConfig).to.have.been.called | ||
expect(binding.updateMetadata).to.have.been.called | ||
}) | ||
|
||
it('should reconfigure the binding only when started', () => { | ||
crashtracker.configure(config) | ||
|
||
expect(binding.updateConfig).to.not.have.been.called | ||
expect(binding.updateMetadata).to.not.have.been.called | ||
}) | ||
|
||
it('should handle errors', () => { | ||
crashtracker.start(config) | ||
crashtracker.configure(null) | ||
|
||
expect(() => crashtracker.configure(config)).to.not.throw() | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.