Move code to pkg
This commit is contained in:
parent
bd4c822670
commit
f1b085fa36
465 changed files with 656 additions and 680 deletions
68
pkg/middlewares/tracing/wrapper.go
Normal file
68
pkg/middlewares/tracing/wrapper.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package tracing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/containous/alice"
|
||||
"github.com/containous/traefik/pkg/log"
|
||||
"github.com/containous/traefik/pkg/tracing"
|
||||
"github.com/opentracing/opentracing-go/ext"
|
||||
)
|
||||
|
||||
// Tracable embeds tracing information.
|
||||
type Tracable interface {
|
||||
GetTracingInformation() (name string, spanKind ext.SpanKindEnum)
|
||||
}
|
||||
|
||||
// Wrap adds tracability to an alice.Constructor.
|
||||
func Wrap(ctx context.Context, constructor alice.Constructor) alice.Constructor {
|
||||
return func(next http.Handler) (http.Handler, error) {
|
||||
if constructor == nil {
|
||||
return nil, nil
|
||||
}
|
||||
handler, err := constructor(next)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tracableHandler, ok := handler.(Tracable); ok {
|
||||
name, spanKind := tracableHandler.GetTracingInformation()
|
||||
log.FromContext(ctx).WithField(log.MiddlewareName, name).Debug("Adding tracing to middleware")
|
||||
return NewWrapper(handler, name, spanKind), nil
|
||||
}
|
||||
return handler, nil
|
||||
}
|
||||
}
|
||||
|
||||
// NewWrapper returns a http.Handler struct
|
||||
func NewWrapper(next http.Handler, name string, spanKind ext.SpanKindEnum) http.Handler {
|
||||
return &Wrapper{
|
||||
next: next,
|
||||
name: name,
|
||||
spanKind: spanKind,
|
||||
}
|
||||
}
|
||||
|
||||
// Wrapper is used to wrap http handler middleware.
|
||||
type Wrapper struct {
|
||||
next http.Handler
|
||||
name string
|
||||
spanKind ext.SpanKindEnum
|
||||
}
|
||||
|
||||
func (w *Wrapper) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
||||
_, err := tracing.FromContext(req.Context())
|
||||
if err != nil {
|
||||
w.next.ServeHTTP(rw, req)
|
||||
return
|
||||
}
|
||||
|
||||
var finish func()
|
||||
_, req, finish = tracing.StartSpan(req, w.name, w.spanKind)
|
||||
defer finish()
|
||||
|
||||
if w.next != nil {
|
||||
w.next.ServeHTTP(rw, req)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue