Skip to content

Commit

Permalink
fix(elasticsearch): ensure requests can be aborted
Browse files Browse the repository at this point in the history
  • Loading branch information
watson committed Dec 11, 2019
1 parent fdeccbf commit 24fc8e3
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
14 changes: 11 additions & 3 deletions lib/instrumentation/modules/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,22 @@ module.exports = function (elasticsearch, agent, { enabled }) {
}
return original.apply(this, args)
} else {
return original.apply(this, arguments)
.then(function (originalP) {
const originalPromise = original.apply(this, arguments)

const inspectedPromise = originalPromise
.then(function (value) {
span.end()
return originalP
return value
}, function (err) {
span.end()
throw err
})

const descriptors = Object.getOwnPropertyDescriptors(originalPromise)
delete descriptors.domain
Object.defineProperties(inspectedPromise, descriptors)

return inspectedPromise
}
} else {
agent.logger.debug('could not instrument elasticsearch request %o', { id: id })
Expand Down
60 changes: 58 additions & 2 deletions test/instrumentation/modules/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,32 @@ test('client.ping with promise', function userLandCode (t) {
})
})

test('client.ping with unhandled promise', function userLandCode (t) {
resetAgent(2, doneNoHTTP(t, 'HEAD', '/'))

agent.startTransaction('foo')

const client = new elasticsearch.Client({
host: 'thiswillneverwork',
log: {
type: 'file',
level: 'error',
path: '/dev/null'
}
})

const unhandledPromise = client.ping()

process.once('unhandledRejection', agent._instrumentation.bindFunction(unhandledRejection))

function unhandledRejection (reason, promise) {
t.equal(promise, unhandledPromise)
agent.endTransaction()
agent.flush()
client.close()
}
})

test('client.search with callback', function userLandCode (t) {
resetAgent(done(t, 'POST', '/_search', '{"q":"pants"}'))

Expand Down Expand Up @@ -218,8 +244,38 @@ function done (t, method, path, query) {
}
}

function resetAgent (cb) {
function doneNoHTTP (t, method, path, query) {
return function (data, cb) {
t.equal(data.transactions.length, 1)
t.equal(data.spans.length, 1)

var trans = data.transactions[0]
var span = data.spans[0]

t.equal(trans.name, 'foo')
t.equal(trans.type, 'custom')

t.equal(span.type, 'db')
t.equal(span.subtype, 'elasticsearch')
t.equal(span.action, 'request')
t.equal(span.name, 'Elasticsearch: ' + method + ' ' + path)

t.ok(span.stacktrace.some(function (frame) {
return frame.function === 'userLandCode'
}), 'include user-land code frame')

t.notOk(span.context)

t.end()
}
}

function resetAgent (expected, cb) {
if (typeof expected === 'function') {
cb = expected
expected = 3
}
agent._instrumentation.currentTransaction = null
agent._transport = mockClient(3, cb)
agent._transport = mockClient(expected, cb)
agent.captureError = function (err) { throw err }
}

0 comments on commit 24fc8e3

Please sign in to comment.