Skip to content

Commit

Permalink
contrib/gin-gonic/gin: set error tag only for 5xx (#449)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgilmour authored and gbbr committed Jun 11, 2019
1 parent 48b1168 commit 8d2998b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 22 deletions.
8 changes: 6 additions & 2 deletions contrib/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gin // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/gin-gonic/gin"

import (
"fmt"
"net/http"
"strconv"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
Expand Down Expand Up @@ -42,11 +43,14 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
// serve the request to the next middleware
c.Next()

span.SetTag(ext.HTTPCode, strconv.Itoa(c.Writer.Status()))
status := c.Writer.Status()
span.SetTag(ext.HTTPCode, strconv.Itoa(status))
if status >= 500 && status < 600 {
span.SetTag(ext.Error, fmt.Errorf("%d: %s", status, http.StatusText(status)))
}

if len(c.Errors) > 0 {
span.SetTag("gin.errors", c.Errors.String())
span.SetTag(ext.Error, c.Errors[0])
}
}
}
Expand Down
75 changes: 55 additions & 20 deletions contrib/gin-gonic/gin/gintrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gin

import (
"errors"
"fmt"
"html/template"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -85,29 +86,63 @@ func TestError(t *testing.T) {
// setup
router := gin.New()
router.Use(Middleware("foobar"))
wantErr := errors.New("oh no")
responseErr := errors.New("oh no")

// a handler with an error and make the requests
router.GET("/err", func(c *gin.Context) {
c.AbortWithError(500, wantErr)
t.Run("server error", func(*testing.T) {
defer mt.Reset()

// configure a handler that returns an error and 5xx status code
router.GET("/server_err", func(c *gin.Context) {
c.AbortWithError(500, responseErr)
})
r := httptest.NewRequest("GET", "/server_err", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(response.StatusCode, 500)

// verify the errors and status are correct
spans := mt.FinishedSpans()
assert.Len(spans, 1)
if len(spans) < 1 {
t.Fatalf("no spans")
}
span := spans[0]
assert.Equal("http.request", span.OperationName())
assert.Equal("foobar", span.Tag(ext.ServiceName))
assert.Equal("500", span.Tag(ext.HTTPCode))
assert.Equal(fmt.Sprintf("Error #01: %s\n", responseErr), span.Tag("gin.errors"))
// server errors set the ext.Error tag
assert.Equal("500: Internal Server Error", span.Tag(ext.Error).(error).Error())
})
r := httptest.NewRequest("GET", "/err", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(response.StatusCode, 500)

// verify the errors and status are correct
spans := mt.FinishedSpans()
assert.Len(spans, 1)
if len(spans) < 1 {
t.Fatalf("no spans")
}
span := spans[0]
assert.Equal("http.request", span.OperationName())
assert.Equal("foobar", span.Tag(ext.ServiceName))
assert.Equal("500", span.Tag(ext.HTTPCode))
assert.Equal(wantErr.Error(), span.Tag(ext.Error).(error).Error())
t.Run("client error", func(*testing.T) {
defer mt.Reset()

// configure a handler that returns an error and 4xx status code
router.GET("/client_err", func(c *gin.Context) {
c.AbortWithError(418, responseErr)
})
r := httptest.NewRequest("GET", "/client_err", nil)
w := httptest.NewRecorder()
router.ServeHTTP(w, r)
response := w.Result()
assert.Equal(response.StatusCode, 418)

// verify the errors and status are correct
spans := mt.FinishedSpans()
assert.Len(spans, 1)
if len(spans) < 1 {
t.Fatalf("no spans")
}
span := spans[0]
assert.Equal("http.request", span.OperationName())
assert.Equal("foobar", span.Tag(ext.ServiceName))
assert.Equal("418", span.Tag(ext.HTTPCode))
assert.Equal(fmt.Sprintf("Error #01: %s\n", responseErr), span.Tag("gin.errors"))
// client errors do not set the ext.Error tag
assert.Equal(nil, span.Tag(ext.Error))
})
}

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

0 comments on commit 8d2998b

Please sign in to comment.