Skip to content

Commit 300f983

Browse files
committed
contrib/gin-gonic/gin: rename errorPropagation to useGinErrors
1 parent 92daacb commit 300f983

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

contrib/gin-gonic/gin/gintrace.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
5555
span, ctx, finishSpans := httptrace.StartRequestSpan(c.Request, opts...)
5656
defer func() {
5757
status := c.Writer.Status()
58-
if cfg.propagateError && cfg.isStatusError(status) {
59-
var err error
60-
for _, e := range c.Errors {
61-
err = errors.Join(err, e.Err)
62-
}
63-
finishSpans(status, cfg.isStatusError, tracer.WithError(err))
58+
errMsg := c.Errors.String()
59+
if errMsg != "" && cfg.useGinErrors && cfg.isStatusError(status) {
60+
finishSpans(status, cfg.isStatusError, tracer.WithError(errors.New(errMsg)))
6461
}
6562
finishSpans(status, cfg.isStatusError)
6663
}()

contrib/gin-gonic/gin/gintrace_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func TestError(t *testing.T) {
185185
defer mt.Reset()
186186

187187
router := gin.New()
188-
router.Use(Middleware("foobar", WithErrorPropagation()))
188+
router.Use(Middleware("foobar", WithUseGinErrors()))
189189

190190
// configure a handler that returns an error and 5xx status code
191191
router.GET("/server_err", func(c *gin.Context) {
@@ -210,7 +210,7 @@ func TestError(t *testing.T) {
210210
assert.Equal("500", span.Tag(ext.HTTPCode))
211211
assert.Equal(fmt.Sprintf("Error #01: %s\n", responseErr), span.Tag("gin.errors"))
212212
// server errors set the ext.ErrorMsg tag
213-
assert.Equal("oh no", span.Tag(ext.ErrorMsg))
213+
assert.Equal(fmt.Sprintf("Error #01: %s\n", responseErr), span.Tag(ext.ErrorMsg))
214214
assert.Equal(ext.SpanKindServer, span.Tag(ext.SpanKind))
215215
assert.Equal("gin-gonic/gin", span.Tag(ext.Component))
216216
assert.Equal(componentName, span.Integration())
@@ -220,7 +220,7 @@ func TestError(t *testing.T) {
220220
defer mt.Reset()
221221

222222
router := gin.New()
223-
router.Use(Middleware("foobar", WithErrorPropagation()))
223+
router.Use(Middleware("foobar", WithUseGinErrors()))
224224

225225
// configure a handler that returns an error and 5xx status code
226226
router.GET("/server_err", func(c *gin.Context) {

contrib/gin-gonic/gin/option.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import (
1515
)
1616

1717
type config struct {
18-
analyticsRate float64
19-
resourceNamer func(c *gin.Context) string
20-
serviceName string
21-
ignoreRequest func(c *gin.Context) bool
22-
isStatusError func(statusCode int) bool
23-
propagateError bool
24-
headerTags instrumentation.HeaderTags
18+
analyticsRate float64
19+
resourceNamer func(c *gin.Context) string
20+
serviceName string
21+
ignoreRequest func(c *gin.Context) bool
22+
isStatusError func(statusCode int) bool
23+
useGinErrors bool
24+
headerTags instrumentation.HeaderTags
2525
}
2626

2727
func newConfig(serviceName string) *config {
@@ -30,13 +30,13 @@ func newConfig(serviceName string) *config {
3030
}
3131
rate := instr.AnalyticsRate(true)
3232
return &config{
33-
analyticsRate: rate,
34-
resourceNamer: defaultResourceNamer,
35-
serviceName: serviceName,
36-
ignoreRequest: func(_ *gin.Context) bool { return false },
37-
isStatusError: isServerError,
38-
propagateError: false,
39-
headerTags: instr.HTTPHeadersAsTags(),
33+
analyticsRate: rate,
34+
resourceNamer: defaultResourceNamer,
35+
serviceName: serviceName,
36+
ignoreRequest: func(_ *gin.Context) bool { return false },
37+
isStatusError: isServerError,
38+
useGinErrors: false,
39+
headerTags: instr.HTTPHeadersAsTags(),
4040
}
4141
}
4242

@@ -95,11 +95,11 @@ func isServerError(statusCode int) bool {
9595
return statusCode >= 500 && statusCode < 600
9696
}
9797

98-
// WithErrorPropagation enables the propagation of gin's errors to the span.
98+
// WithUseGinErrors enables the usage of gin's errors for the span instead of crafting generic errors from the status code.
9999
// If there are multiple errors in the gin context, they will be all added to the span.
100-
func WithErrorPropagation() OptionFn {
100+
func WithUseGinErrors() OptionFn {
101101
return func(cfg *config) {
102-
cfg.propagateError = true
102+
cfg.useGinErrors = true
103103
}
104104
}
105105

0 commit comments

Comments
 (0)