Skip to content

Commit

Permalink
feat(otelgin): enhance error handling and test coverage in gintrace
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Nov 25, 2024
1 parent d280f2e commit 3ab2b2c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
span.SetAttributes(semconv.HTTPStatusCode(status))
}
if len(c.Errors) > 0 {
span.SetStatus(codes.Error, "gin error")
for _, err := range c.Errors {
span.RecordError(err.Err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,27 @@ func TestSpanStatus(t *testing.T) {
assert.Equal(t, tc.wantSpanStatus, sr.Ended()[0].Status().Code, "should only set Error status for HTTP statuses >= 500")
})
}

t.Run("The status code is 200, but an error is returned", func(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(
sdktrace.WithSpanProcessor(sr),
)

router := gin.New()
router.Use(otelgin.Middleware("foobar", otelgin.WithTracerProvider(provider)))
router.GET("/", func(c *gin.Context) {
_ = c.Error(errors.New("something went wrong"))
c.JSON(http.StatusOK, nil)
})

router.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest("GET", "/", nil))

require.Len(t, sr.Ended(), 1)
assert.Equal(t, codes.Error, sr.Ended()[0].Status().Code)
require.Len(t, sr.Ended()[0].Events(), 1)
assert.Contains(t, sr.Ended()[0].Events()[0].Attributes, attribute.String("exception.message", "something went wrong"))
})
}

func TestSpanName(t *testing.T) {
Expand Down

0 comments on commit 3ab2b2c

Please sign in to comment.