Skip to content
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

contrib/go-chi/chi: Added option to set custom error status check #773

Merged
merged 8 commits into from
Dec 11, 2020

Conversation

Hunrik
Copy link
Contributor

@Hunrik Hunrik commented Nov 9, 2020

In this change I introduced a new option for the chi tracing package, called WithIsErrorCheck which can be used to provide custom status code checking logic (For example, in our case we'd like 4xx status codes to be displayed as errors)

I hope the naming is acceptable, if not I'll happily fix it.

@knusbaum knusbaum added this to the 1.28.0 milestone Nov 9, 2020
Copy link
Contributor

@knusbaum knusbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @Hunrik!
Only a few naming and formatting things. Otherwise the change looks good.


// WithIsErrorCheck sets the function which is used to decide whther the
// status should be marked as an error
func WithIsErrorCheck(isError func(statusCode int) bool) Option {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm more inclined towards something like WithStatusCheck or WithErrorCheck. WithIsErrorCheck is a little cumbersome.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithStatusCheck Sounds better to me too.

}
}

func defaultIsError(statusCode int) bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be clearer to call this isServerError to indicate it checks for 5xx error codes.


// setup
router := chi.NewRouter()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's eliminate this whitespace as it doesn't make the test easier to read.

return statusCode >= 400
}),
))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here with whitespace. I think we can remove it.

@knusbaum
Copy link
Contributor

@Hunrik Thanks for the changes. Let me say sorry. The code looks good so I made the mistake of saying the change was ready to go except for the nits.

Before that, I want to make sure I understand your use case for this change.

We consider 5xx errors to be server errors (something went wrong in the server) and so we mark those spans with an error. Other errors, 4xx for instance, indicate that no error happened in the server, but that the client sent some kind of bad request (client error). Those we don't mark as an error since an error did not occur in the server. This is what our various trace clients and the UI itself assume to be the case.

In your case, you've said you want 4xx statuses to be marked as errors. Would you be able to give a little more detail about why you want to configure it this way?

A second thing to consider is that we may want to introduce this change at a different level. There are many other http integrations and if we decide this type of callback is good, they could all benefit from it. Having to put this in each individual integration would result in code duplication that we can hopefully avoid.

@Hunrik
Copy link
Contributor Author

Hunrik commented Nov 10, 2020

@knusbaum We are perfectly fine with the 5xx status marked as error in most cases, but sometimes we'd like to count 4xx status as errors too. If it is an outside facing endpoint, in our case it is for webhooks, even 4xx counts as errors, because it doesn't matter if it is a client error, this is probably us who did something wrong, or didn't adapt a change.

For example we recently made a change in a webhook endpoint's validation which returns 403 if the validation failed. And we realised we can't really use APM here to check whether everything works or not, because it does not differentiate between 2xx and 4xx.

We use/used something similar in the js lib which could be passed in the plugin config as validateStatus.
I'm not sure if it would belong to globalconfig, if thats what you mean. I think this is perfectly fine to configure on integration level.

Copy link
Contributor

@gbbr gbbr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR Henrik! And thank you for spending the time to describe your usecase. Would you mind making a couple more small changes please?

contrib/go-chi/chi/chi_test.go Outdated Show resolved Hide resolved
contrib/go-chi/chi/chi_test.go Show resolved Hide resolved
contrib/go-chi/chi/chi_test.go Outdated Show resolved Hide resolved
@@ -32,6 +33,8 @@ func defaults(cfg *config) {
} else {
cfg.analyticsRate = globalconfig.AnalyticsRate()
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the empty line here.

contrib/go-chi/chi/option.go Outdated Show resolved Hide resolved
contrib/go-chi/chi/option.go Outdated Show resolved Hide resolved
Hunrik and others added 2 commits November 11, 2020 11:20
Co-authored-by: Gabriel Aszalos <gabriel.aszalos@datadoghq.com>
@@ -17,6 +17,7 @@ type config struct {
serviceName string
spanOpts []ddtrace.StartSpanOption // additional span options to be applied
analyticsRate float64
statusCheck func(statusCode int) bool
Copy link
Contributor

@gbbr gbbr Nov 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this isn't clear enough, perhaps isErrorStatus or isStatusError would be more specific.

@knusbaum knusbaum modified the milestones: 1.28.0, 1.29.0 Dec 10, 2020
@knusbaum knusbaum modified the milestones: 1.29.0, 1.28.0 Dec 11, 2020
@knusbaum knusbaum merged commit 45c7d74 into DataDog:v1 Dec 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants