New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validateStatus
option to HTTP plugin
#301
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about this a bit more and discussing with the team, 4xx responses should also be errors by default since it means the error is from the client. 5xx should still not be errors by default since the error comes from the server.
docs/API.md
Outdated
@@ -216,6 +216,7 @@ query HelloWorld { | |||
|------------------|------------------|----------------------------------------| | |||
| service | http-client | The service name for this integration. | | |||
| splitByDomain | false | Use the remote endpoint host as the service name instead of the default. | | |||
| validateStatus | `code => true` | Callback function to determine if an HTTP response should be recorded as an error. It should take a status code as its only parameter and return `true` for success or `false` for errors. Requests where no response is received (e.g. connection failures, timeouts) will always be considered to be errors. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requests where no response is received (e.g. connection failures, timeouts) will always be considered to be errors.
Since this configuration focuses on the status code, network errors are our of its scope, so this sentence should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Happy to remove this, but I think this is a useful point of clarification to make it clear that this doesn't configure how "no response" is handled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's redundant since validateStatus implies a response from the remote endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough! I'll ✂️ this.
Perhaps a better place for this would be in some more general documentation for the HTTP plugin? I've found that I need to delve into the code to answer questions about the specifics.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely feel free to add any information you think is relevant to the top of the plugin documentation :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll do this in a separate PR.
src/plugins/http.js
Outdated
@@ -53,6 +53,12 @@ function patch (http, methodName, tracer, config) { | |||
req.on('response', res => { | |||
span.setTag(Tags.HTTP_STATUS_CODE, res.statusCode) | |||
|
|||
if (config.validateStatus && !config.validateStatus(res.statusCode)) { | |||
span.addTags({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit: use setTag
to add a single tag.
@rochdev Thanks! I'll make those tweaks later. |
The [OpenTracing spec][1] states that a span should be tagged as an error "if and only if the application considers the operation represented by the Span to have failed". The `dd-trace-js` HTTP plugin allows you to automatically instrument outgoing HTTP requests. Currently, it only tags a span as an error when the request emits an `error` event, which only happens in the case of "hard failures" like a timeout or connection problem. https://github.com/DataDog/dd-trace-js/blob/34c168555f44a961a61ede95a39341d7c527701a/src/plugins/http.js#L59-L67 This means that requests that return responses never are considered errors, even if a 4XX or 5XX response is received. Having spans tagged as errors is useful, because it means they are flagged as such in the Datadog APM interface, useful for reporting. This commit adds support for a new `validateStatus` option for the HTTP plugin which allows the user to supply a `validateStatus` function which, when a response is received, is passed the response status and expected to return `false` if the response should be considered an error, or `true` if not. If it returns `false`, the span will be marked as an error. By default, we will consider requests that return `4XX` responses to be errors, since a `4XX` indicates a failure which is within the user's control. Fixes DataDog#297. [1]: https://github.com/opentracing/specification/blob/master/semantic_conventions.md
dac46cd
to
b2bfaff
Compare
@rochdev Updated! |
Hi @rochdev! Me again :) I updated to 0.7.0 - happy this has been released. :) However, I'm not getting my 4xx status codes marked as errors in APM. From this thread it sounded like they would be considered errors by default, but maybe I am misunderstanding something. Do I need to add the valiadateStatus to my code to mark 4xx as errors? I am using node 10.13.0 with express for my routing. Thanks so much! |
@johnnydimas It actually depends since the For HTTP servers:
For HTTP clients:
Right now the If you need to configure them individually, please let me know and I can provide a workaround. |
@rochdev Thanks so much for the help, I was able to get it working now! |
The OpenTracing spec states that a span should be tagged as an error "if and only if the application considers the operation represented by the Span to have failed".
The
dd-trace-js
HTTP plugin allows you to automatically instrument outgoing HTTP requests.Currently, it only tags a span as an error when the request emits an
error
event, which only happens in the case of "hard failures" like a timeout or connection problem.dd-trace-js/src/plugins/http.js
Lines 59 to 67 in 34c1685
This means that requests that return responses never are considered errors, even if a 4XX or 5XX response is received.
Having spans tagged as errors is useful, because it means they are flagged as such in the Datadog APM interface, useful for reporting.
This commit adds support for a new
validateStatus
option for the HTTP plugin which allows the user to supply avalidateStatus
function which, when a response is received, is passed the response status and expected to returnfalse
if the response should be considered an error, ortrue
if not. If it returnsfalse
, the span will be marked as an error.By default, we will consider requests that return
4XX
responses to be errors, since a4XX
indicates a failure which is within the user's control.Fixes #297.