-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
João Almeida
committed
Apr 9, 2019
1 parent
d493bfc
commit 6992dcd
Showing
3 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,5 @@ | |
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/opentracing/opentracing-go" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
|
||
type Middleware struct { | ||
tracer opentracing.Tracer | ||
|
||
beforeHook func(opentracing.Span, *gin.Context) | ||
afterHook func(opentracing.Span, *gin.Context) | ||
|
||
operationNameFn func(*gin.Context) string | ||
errorFn func(*gin.Context) bool | ||
resourceNameFn func(*gin.Context) string | ||
} | ||
|
||
func New(tracer opentracing.Tracer, options ...OptionFunc) *Middleware { | ||
m := &Middleware{tracer: tracer} | ||
|
||
for _, option := range options { | ||
option(m) | ||
} | ||
|
||
m.handleDefaultOptions() | ||
|
||
return m | ||
} | ||
|
||
func (m *Middleware) RequestTracer() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
carrier := opentracing.HTTPHeadersCarrier(c.Request.Header) | ||
ctx, _ := m.tracer.Extract(opentracing.HTTPHeaders, carrier) | ||
|
||
span := m.tracer.StartSpan(m.operationNameFn(c), ext.RPCServerOption(ctx)) | ||
|
||
ext.HTTPMethod.Set(span, c.Request.Method) | ||
ext.HTTPUrl.Set(span, c.Request.URL.String()) | ||
span.SetTag("resource.name", m.resourceNameFn(c)) | ||
|
||
c.Request = c.Request.WithContext(opentracing.ContextWithSpan(c.Request.Context(), span)) | ||
|
||
m.beforeHook(span, c) | ||
c.Next() | ||
m.afterHook(span, c) | ||
|
||
ext.Error.Set(span, m.errorFn(c)) | ||
ext.HTTPStatusCode.Set(span, uint16(c.Writer.Status())) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/opentracing/opentracing-go" | ||
) | ||
|
||
type OptionFunc func(*Middleware) | ||
|
||
func SetOperationNameFn(fn func(*gin.Context) string) OptionFunc { | ||
return func(m *Middleware) { | ||
m.operationNameFn = fn | ||
} | ||
} | ||
|
||
func SetErrorFn(fn func(*gin.Context) bool) OptionFunc { | ||
return func(m *Middleware) { | ||
m.errorFn = fn | ||
} | ||
} | ||
|
||
func SetResourceNameFn(fn func(*gin.Context) string) OptionFunc { | ||
return func(m *Middleware) { | ||
m.resourceNameFn = fn | ||
} | ||
} | ||
|
||
func SetBeforeHook(fn func(opentracing.Span, *gin.Context)) OptionFunc { | ||
return func(m *Middleware) { | ||
m.beforeHook = fn | ||
} | ||
} | ||
|
||
func SetAfterHook(fn func(opentracing.Span, *gin.Context)) OptionFunc { | ||
return func(m *Middleware) { | ||
m.afterHook = fn | ||
} | ||
} | ||
|
||
func (m *Middleware) handleDefaultOptions() { | ||
if m.operationNameFn == nil { | ||
m.operationNameFn = func(ctx *gin.Context) string { | ||
return "gin.request" | ||
} | ||
} | ||
|
||
if m.errorFn == nil { | ||
m.errorFn = func(ctx *gin.Context) bool { | ||
return ctx.Writer.Status() >= 400 || len(ctx.Errors) > 0 | ||
} | ||
} | ||
|
||
if m.resourceNameFn == nil { | ||
m.resourceNameFn = func(ctx *gin.Context) string { | ||
return ctx.HandlerName() | ||
} | ||
} | ||
|
||
if m.beforeHook == nil { | ||
m.beforeHook = func(span opentracing.Span, ctx *gin.Context) { | ||
return | ||
} | ||
} | ||
|
||
if m.afterHook == nil { | ||
m.afterHook = func(span opentracing.Span, ctx *gin.Context) { | ||
return | ||
} | ||
} | ||
} |