-
Notifications
You must be signed in to change notification settings - Fork 488
feat(contrib/gin-gonic/gin): add WithStatusCheck and WithUseGinErrors options #3984
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
Open
theo303
wants to merge
8
commits into
DataDog:main
Choose a base branch
from
theo303:gin-enhance-span-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−8
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e110a8a
contrib/gin-gonic/gin: propagate gin errors to tracing spans
theo303 87e371e
contrib/gin-gonic/gin: error propagation configuration
theo303 92daacb
contrib/gin-gonic/gin: join errors instead of using Last()
theo303 b7f83ac
contrib/gin-gonic/gin: rename errorPropagation to useGinErrors
theo303 23e0479
Merge branch 'main' into gin-enhance-span-error-handling
rarguelloF 67cb81c
Merge branch 'main' into gin-enhance-span-error-handling
rarguelloF 533469b
contrib/gin-gonic/gin: use env var for error statuses by default
theo303 89d98bb
Merge branch 'main' into gin-enhance-span-error-handling
rarguelloF File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,13 @@ import ( | |
| ) | ||
|
|
||
| type config struct { | ||
| analyticsRate float64 | ||
| resourceNamer func(c *gin.Context) string | ||
| serviceName string | ||
| ignoreRequest func(c *gin.Context) bool | ||
| headerTags instrumentation.HeaderTags | ||
| analyticsRate float64 | ||
| resourceNamer func(c *gin.Context) string | ||
| serviceName string | ||
| ignoreRequest func(c *gin.Context) bool | ||
| isStatusError func(statusCode int) bool | ||
| propagateError bool | ||
| headerTags instrumentation.HeaderTags | ||
| } | ||
|
|
||
| func newConfig(serviceName string) *config { | ||
|
|
@@ -28,11 +30,13 @@ func newConfig(serviceName string) *config { | |
| } | ||
| rate := instr.AnalyticsRate(true) | ||
| return &config{ | ||
| analyticsRate: rate, | ||
| resourceNamer: defaultResourceNamer, | ||
| serviceName: serviceName, | ||
| ignoreRequest: func(_ *gin.Context) bool { return false }, | ||
| headerTags: instr.HTTPHeadersAsTags(), | ||
| analyticsRate: rate, | ||
| resourceNamer: defaultResourceNamer, | ||
| serviceName: serviceName, | ||
| ignoreRequest: func(_ *gin.Context) bool { return false }, | ||
| isStatusError: isServerError, | ||
| propagateError: false, | ||
| headerTags: instr.HTTPHeadersAsTags(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -79,6 +83,26 @@ func WithResourceNamer(namer func(c *gin.Context) string) OptionFn { | |
| } | ||
| } | ||
|
|
||
| // WithStatusCheck specifies a function fn which reports whether the passed | ||
| // statusCode should be considered an error. | ||
| func WithStatusCheck(fn func(statusCode int) bool) OptionFn { | ||
| return func(cfg *config) { | ||
| cfg.isStatusError = fn | ||
| } | ||
| } | ||
|
|
||
| func isServerError(statusCode int) bool { | ||
| return statusCode >= 500 && statusCode < 600 | ||
| } | ||
|
|
||
| // WithErrorPropagation enables the propagation of gin's errors to the span. | ||
| // If there are multiple errors in the gin context, they will be all added to the span. | ||
| func WithErrorPropagation() OptionFn { | ||
|
||
| return func(cfg *config) { | ||
| cfg.propagateError = true | ||
| } | ||
| } | ||
|
|
||
| // WithHeaderTags enables the integration to attach HTTP request headers as span tags. | ||
| // Warning: | ||
| // Using this feature can risk exposing sensitive data such as authorization tokens to Datadog. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we are already setting
gin.errorsequal toc.Errors.String()at line 80. Could we use the same strategy here?Additionally, what if
isStatusErrorandpropagateErrorboth return true, but c.Errors has length 0? is that possible?In that case we should fallback to the previous method as well (crafting a custom error based on the status code), so you should additionally check in the condition
&& len(c.Errors) > 0There 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.
Ah yes, you are right I missed that.
c.Errors has length 0 is possible, in this case
errwill be nil, and thereforetracer.WithError(err)will do nothing. In this case the custom error based on the status code will be crafted. I think it is exactly what happens in the second test case that I added.Do you think this behavior is not obvious and the condition should be added ?
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.
nevermind, using
c.Errors().String(), the condition should be added