Skip to content

Commit

Permalink
fix: a possibility of crash being not reported
Browse files Browse the repository at this point in the history
  • Loading branch information
dszakallas authored and Dávid Szakállas committed May 17, 2017
1 parent 1c4b043 commit 9a9e994
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 51 deletions.
2 changes: 2 additions & 0 deletions lib/agent/tracer/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ Cache.prototype.flushExpiredChildren = function (path, until) {
return flushed
}

Cache.MAX_TIMESTAMP = 8640000000000000

module.exports = Cache
module.exports.create = function (options) {
return new Cache(options)
Expand Down
9 changes: 9 additions & 0 deletions lib/agent/tracer/cache.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ describe('Cache', function () {
cache.flushExpiredChildren([])
expect(cache.get([])).to.have.members([])
})

it('path should be expired if we flush with a high enough limit', function () {
var cache = new Cache(options)
var event = { type: 'sr' }
cache.merge(['comm-id'], [event], options.mustCollectSeverity)
cache.lock(['comm-id'])
cache.flushExpiredChildren([], Cache.MAX_TIMESTAMP)
expect(cache.get([])).to.have.members([])
})
it('path should not be expired', function () {
var cache = new Cache(options)
var event = { type: 'sr' }
Expand Down
8 changes: 6 additions & 2 deletions lib/agent/tracer/collector.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,17 @@ Collector.prototype.clientRecv = function (payload, duffelBag, briefcase) {
}
}

Collector.prototype.sample = function () {
var content = this.cache.flushExpiredChildren([])
Collector.prototype.sample = function (until) {
var content = this.cache.flushExpiredChildren([], until)
if (content.length) {
this.sampler.add(content)
}
}

Collector.prototype.sampleAll = function () {
this.sample(Cache.MAX_TIMESTAMP)
}

Collector.prototype.flush = function () {
var buffers = this.sampler.flush()
var duplicates = new Set()
Expand Down
4 changes: 4 additions & 0 deletions lib/agent/tracer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ Tracer.prototype.sample = function () {
this.collector.sample()
}

Tracer.prototype.sampleAll = function () {
this.collector.sampleAll()
}

Tracer.prototype.send = function (isSync, callback) {
callback = callback || function () {}
var events = this.collector.flush()
Expand Down
1 change: 1 addition & 0 deletions lib/instrumentations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ module.exports.create = function (options) {
shimmer.wrap(process, '_fatalException', function (original) {
return function (error) {
agent.tracer.collector.systemError(error)
agent.tracer.sampleAll()
var sync = true
agent.tracer.send(sync)
return original.apply(this, arguments)
Expand Down
97 changes: 51 additions & 46 deletions test/e2e/crash/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@
'use strict'
// 'use strict'
//
// var express = require('express')
// var bodyParser = require('body-parser')
// var test = require('tape')
// var spawnSync = require('spawn-sync')
// var defaultsDeep = require('lodash').defaultsDeep
// var path = require('path')
// // var semver = require('semver')
//
// var TRACE_API_KEY = 'headers.payload.signature'
// var TRACE_SERVICE_NAME = 'service-name'
// var TEST_WEB_SERVER_PORT = process.env.TEST_WEBSERVER_PORT || 44332
// var TEST_TIMEOUT = 10000
//
// var env = {
// TRACE_API_KEY: TRACE_API_KEY,
// TRACE_SERVICE_NAME: TRACE_SERVICE_NAME,
// TRACE_COLLECT_INTERVAL: 100,
// TRACE_UPDATE_INTERVAL: 100000,
// TRACE_COLLECTOR_API_URL: 'http://127.0.0.1:' + TEST_WEB_SERVER_PORT
// }
//
// test('should report crash', {
// timeout: TEST_TIMEOUT,
// skip: true // !semver.satisfies(process.version, '>= 6')
// }, function (t) {
// var app = express()
// app.use(bodyParser.json())
// app.post('/transaction-events', function (req, res) {
// var event = req.body.e.find(function (e) {
// return e.t === 'err' && e.d.t === 'system-error'
// })
// t.ok(event != null, 'Error event exists')
// t.end()
// process.exit(0)
// })
// app.listen(TEST_WEB_SERVER_PORT, function (err) {
// t.error(err, 'server starts listening at ' + TEST_WEB_SERVER_PORT)
//
// spawnSync('node', [path.join(__dirname, 'testee.js')], {
//
// env: defaultsDeep({}, env, process.env)
// })
// })
// })

var express = require('express')
var bodyParser = require('body-parser')
var test = require('tape')
var spawnSync = require('spawn-sync')
var defaultsDeep = require('lodash').defaultsDeep
var path = require('path')
var find = require('lodash.find')
// var semver = require('semver')

var TRACE_API_KEY = 'headers.payload.signature'
var TRACE_SERVICE_NAME = 'service-name'
var TEST_WEB_SERVER_PORT = process.env.TEST_WEBSERVER_PORT || 44333 + Math.trunc(Math.random() * 100)
var TEST_TIMEOUT = 10000

var env = {
TRACE_API_KEY: TRACE_API_KEY,
TRACE_SERVICE_NAME: TRACE_SERVICE_NAME,
TRACE_COLLECT_INTERVAL: 100,
TRACE_UPDATE_INTERVAL: 100000,
TRACE_COLLECTOR_API_URL: 'http://127.0.0.1:' + TEST_WEB_SERVER_PORT
}

test('should report crash', {
timeout: TEST_TIMEOUT
}, function (t) {
var server
var app = express()
app.use(bodyParser.json())
app.post('/transaction-events', function (req, res) {
try {
t.ok(req.body.e, 'Events are reported')
console.log(req.body.e)
var event = find(req.body.e, function (e) {
return e.t === 'err' && e.d.t === 'system-error'
})
t.ok(event != null, 'Error event exists')
t.end()
} finally {
server.close()
}
})
server = app.listen(TEST_WEB_SERVER_PORT, function (err) {
t.error(err, 'server starts listening at ' + TEST_WEB_SERVER_PORT)

spawnSync('node', [path.join(__dirname, 'testee.js')], {
stdio: [0, 1, 2],
env: defaultsDeep({}, env, process.env)
})
})
})
4 changes: 1 addition & 3 deletions test/e2e/utils/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ function childProcessTest (name_, opts_, cb_, args, fn) {

function test (name_, opts_, cb_) {
var args = getTestArgs(name_, opts_, cb_)
if (args.skip) {
if (args.opts.skip) {
test.skip(name_, opts_, cb_)
} else if (args.only) {
test.only(name_, opts_, cb_)
} else if (process.env.TEST_ISOLATE === 'child-process') {
childProcessTest(name_ + ' (child process running in ' + process.pid + ')', opts_, cb_, args, tape.only)
} else {
Expand Down

0 comments on commit 9a9e994

Please sign in to comment.