Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional func to set the span's error tag flag #4

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion ginhttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions ginhttp/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
}
Expand Down