Skip to content

Commit

Permalink
chore(otelgin): refine error recording in gintrace
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Nov 23, 2024
1 parent 23949c7 commit 5f403b8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
5 changes: 3 additions & 2 deletions instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package otelgin // import "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"

import (
"errors"
"fmt"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -97,7 +96,9 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
span.SetAttributes(semconv.HTTPStatusCode(status))
}
if len(c.Errors) > 0 {
span.RecordError(errors.New(c.Errors.String()))
for _, err := range c.Errors {
span.RecordError(err.Err)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ func TestError(t *testing.T) {
// configure a handler that returns an error and 5xx status
// code
router.GET("/server_err", func(c *gin.Context) {
_ = c.AbortWithError(http.StatusInternalServerError, errors.New("oh no"))
_ = c.Error(errors.New("oh no one"))
_ = c.AbortWithError(http.StatusInternalServerError, errors.New("oh no two"))
})
r := httptest.NewRequest("GET", "/server_err", nil)
w := httptest.NewRecorder()
Expand All @@ -124,10 +125,14 @@ func TestError(t *testing.T) {

// verify the error event
events := span.Events()
assert.Len(t, events, 1)
assert.Len(t, events, 2)
assert.Equal(t, "exception", events[0].Name)
assert.Contains(t, events[0].Attributes, attribute.String("exception.type", "*errors.errorString"))
assert.Contains(t, events[0].Attributes, attribute.String("exception.message", "Error #01: oh no\n"))
assert.Contains(t, events[0].Attributes, attribute.String("exception.message", "oh no one"))
assert.Equal(t, "exception", events[1].Name)
assert.Contains(t, events[1].Attributes, attribute.String("exception.type", "*errors.errorString"))
assert.Contains(t, events[1].Attributes, attribute.String("exception.message", "oh no two"))

// server errors set the status
assert.Equal(t, codes.Error, span.Status().Code)
}
Expand Down

0 comments on commit 5f403b8

Please sign in to comment.