Skip to content

Commit

Permalink
fix(wrapQuery): fix when no readStream or promise is returned
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadowbeetle committed Jan 18, 2017
1 parent 858e73a commit 1b0738c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 20 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ Desktop.ini
.idea
*.iml
.vscode

# RCs
.nvmrc
.npmrc
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ Desktop.ini
.idea
*.iml
.vscode

#RCs
.nvmrc
.npmrc
48 changes: 29 additions & 19 deletions lib/instrumentations/utils/wrapQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ function wrapQuery (original, args, agent, params) {
} else if (continuationMethod === 'readStream') {
return wrapReadStream.call(this, original, args, reporter)
} else if (continuationMethod === 'callback') { // uses callback
return wrapCallback.call(this, original, _params, args, reporter)
return wrapCallback.call(this, original, args, reporter)
} else {
return original.apply(this, args) // we might not want to instrument the method
}
}

function wrapCallback (original, params, args, reporter) {
function wrapCallback (original, args, reporter) {
var wrappedCallback = function (original) {
return function (err) {
reporter.reportReceive(err)
Expand All @@ -78,33 +78,43 @@ function wrapCallback (original, params, args, reporter) {
}

function wrapPromise (original, args, reporter) {
reporter.reportSend()
var originalPromise = original.apply(this, args)
return originalPromise.then(
function (v) { reporter.reportReceive(); return v },
function (err) { reporter.reportReceive(err); throw err }
)
if (originalPromise) {
reporter.reportSend()
return originalPromise.then(
function (v) {
reporter.reportReceive()
return v
},
function (err) {
reporter.reportReceive(err)
throw err
}
)
}
}

function wrapReadStream (original, args, reporter) {
reporter.reportSend()
var originalStream = original.apply(this, args)

originalStream.on('end', function () {
reporter.reportReceive()
})
if (originalStream) {
reporter.reportSend()
originalStream.on('end', function () {
reporter.reportReceive()
})

originalStream.on('error', function (err) {
reporter.reportReceive(err)
originalStream.on('error', function (err) {
reporter.reportReceive(err)

if (typeof originalStream.listenerCount === 'function') {
if (originalStream.listenerCount('error') < 2) {
if (typeof originalStream.listenerCount === 'function') {
if (originalStream.listenerCount('error') < 2) {
throw err
}
} else if (EventEmitter.listenerCount(originalStream, 'error') < 2) {
throw err
}
} else if (EventEmitter.listenerCount(originalStream, 'error') < 2) {
throw err
}
})
})
}

return originalStream
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@risingstack/trace",
"version": "2.33.0",
"version": "3.0.2",
"author": "RisingStack, Inc.",
"license": "SEE LICENSE IN LICENSE",
"contributors": "RisingStack",
Expand Down

0 comments on commit 1b0738c

Please sign in to comment.