diff --git a/ginhttp/server.go b/ginhttp/server.go index 8f17724..798dd76 100644 --- a/ginhttp/server.go +++ b/ginhttp/server.go @@ -12,7 +12,7 @@ import ( "net/http" "github.com/gin-gonic/gin" - opentracing "github.com/opentracing/opentracing-go" + "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" ) @@ -21,6 +21,7 @@ const defaultComponentName = "net/http" type mwOptions struct { opNameFunc func(r *http.Request) string spanObserver func(span opentracing.Span, r *http.Request) + errorFunc func(ctx *gin.Context) bool componentName string } @@ -51,6 +52,13 @@ func MWSpanObserver(f func(span opentracing.Span, r *http.Request)) MWOption { } } +// MWErrorFunc returns a MWOption that sets the span error tag +func MWErrorFunc(f func(ctx *gin.Context) bool) MWOption { + return func(options *mwOptions) { + options.errorFunc = f + } +} + // Middleware is a gin native version of the equivalent middleware in: // https://github.com/opentracing-contrib/go-stdlib/ func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc { @@ -84,6 +92,9 @@ func Middleware(tr opentracing.Tracer, options ...MWOption) gin.HandlerFunc { c.Next() + if opts.errorFunc != nil { + ext.Error.Set(sp, opts.errorFunc(c)) + } ext.HTTPStatusCode.Set(sp, uint16(c.Writer.Status())) sp.Finish() } diff --git a/ginhttp/server_test.go b/ginhttp/server_test.go index 0fc0ec9..25ea2d9 100644 --- a/ginhttp/server_test.go +++ b/ginhttp/server_test.go @@ -6,7 +6,7 @@ import ( "testing" "github.com/gin-gonic/gin" - opentracing "github.com/opentracing/opentracing-go" + "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/mocktracer" ) @@ -96,7 +96,7 @@ func TestSpanObserverOption(t *testing.T) { t.Fatalf("got %s operation name, expected %s", got, want) } - defaultLength := 5 + defaultLength := 6 if len(spans[0].Tags()) != len(testCase.Tags)+defaultLength { t.Fatalf("got tag length %d, expected %d", len(spans[0].Tags()), len(testCase.Tags)) }