-
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.
- Loading branch information
Showing
12 changed files
with
417 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use strict' | ||
|
||
const log = require('../log') | ||
const { URL } = require('url') | ||
const libdatadog = require('@datadog/libdatadog') | ||
const binding = libdatadog.load('crashtracker') | ||
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.initWithReceiver( | ||
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: false, | ||
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 | ||
}, | ||
// TODO: Use `EnabledWithSymbolsInReceiver` instead for Linux when fixed. | ||
resolve_frames: 'EnabledWithInprocessSymbols', | ||
wait_for_receiver: false | ||
} | ||
} | ||
|
||
_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
134 changes: 134 additions & 0 deletions
134
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,134 @@ | ||
'use strict' | ||
|
||
const { expect } = require('chai') | ||
const sinon = require('sinon') | ||
const pkg = require('../../../../package.json') | ||
const proxyquire = require('proxyquire').noCallThru() | ||
|
||
require('../setup/tap') | ||
|
||
describe('crashtracking', () => { | ||
describe('crashtracker', () => { | ||
let crashtracker | ||
let binding | ||
let config | ||
let crashtrackerConfig | ||
let crashtrackerMetadata | ||
let crashtrackerReceiverConfig | ||
let libdatadog | ||
|
||
beforeEach(() => { | ||
config = { | ||
port: 7357, | ||
tags: { | ||
foo: 'bar' | ||
} | ||
} | ||
|
||
crashtrackerConfig = { | ||
endpoint: { | ||
url: { | ||
scheme: 'http', | ||
authority: '127.0.0.1:7357', | ||
path_and_query: '' | ||
} | ||
}, | ||
resolve_frames: 'EnabledWithInprocessSymbols' | ||
} | ||
|
||
crashtrackerReceiverConfig = { | ||
path_to_receiver_binary: '/test/receiver' | ||
} | ||
|
||
crashtrackerMetadata = { | ||
tags: [ | ||
'foo:bar', | ||
'is_crash:true', | ||
'language:javascript', | ||
`library_version:${pkg.version}`, | ||
'runtime:nodejs', | ||
'severity:crash' | ||
] | ||
} | ||
|
||
binding = { | ||
initWithReceiver: sinon.stub(), | ||
updateConfig: sinon.stub(), | ||
updateMetadata: sinon.stub() | ||
} | ||
|
||
libdatadog = { | ||
find: sinon.stub(), | ||
load: sinon.stub() | ||
} | ||
libdatadog.find.withArgs('crashtracker-receiver', true).returns('/test/receiver') | ||
libdatadog.load.withArgs('crashtracker').returns(binding) | ||
|
||
crashtracker = proxyquire('../../src/crashtracking/crashtracker', { | ||
'@datadog/libdatadog': libdatadog | ||
}) | ||
}) | ||
|
||
describe('start', () => { | ||
it('should initialize the binding', () => { | ||
crashtracker.start(config) | ||
|
||
expect(binding.initWithReceiver).to.have.been.calledWithMatch( | ||
crashtrackerConfig, | ||
crashtrackerReceiverConfig, | ||
crashtrackerMetadata | ||
) | ||
}) | ||
|
||
it('should initialize the binding only once', () => { | ||
crashtracker.start(config) | ||
crashtracker.start(config) | ||
|
||
expect(binding.initWithReceiver).to.have.been.calledOnce | ||
}) | ||
|
||
it('should reconfigure when started multiple times', () => { | ||
crashtracker.start(config) | ||
crashtracker.start(config) | ||
|
||
expect(binding.updateConfig).to.have.been.calledWithMatch(crashtrackerConfig) | ||
expect(binding.updateMetadata).to.have.been.calledWithMatch(crashtrackerMetadata) | ||
}) | ||
|
||
it('should handle errors', () => { | ||
binding.initWithReceiver.throws(new Error('boom')) | ||
|
||
crashtracker.start(config) | ||
|
||
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.calledWithMatch(crashtrackerConfig) | ||
expect(binding.updateMetadata).to.have.been.calledWithMatch(crashtrackerMetadata) | ||
}) | ||
|
||
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', () => { | ||
binding.updateConfig.throws(new Error('boom')) | ||
binding.updateMetadata.throws(new Error('boom')) | ||
|
||
crashtracker.start(config) | ||
crashtracker.configure(config) | ||
|
||
expect(() => crashtracker.configure(config)).to.not.throw() | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.