Skip to content

Commit

Permalink
Implement middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
João Almeida committed Apr 9, 2019
1 parent d493bfc commit 6992dcd
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.idea
52 changes: 52 additions & 0 deletions pkg/middleware/middleware.go
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()))
}
}
70 changes: 70 additions & 0 deletions pkg/middleware/options.go
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
}
}
}

0 comments on commit 6992dcd

Please sign in to comment.