traefik/pkg/middlewares/tracing/wrapper.go

69 lines
1.6 KiB
Go
Raw Normal View History

2018-01-10 16:48:04 +00:00
package tracing
import (
2018-11-14 09:18:03 +00:00
"context"
2018-01-10 16:48:04 +00:00
"net/http"
2018-11-14 09:18:03 +00:00
"github.com/containous/alice"
"github.com/opentracing/opentracing-go/ext"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/tracing"
2018-01-10 16:48:04 +00:00
)
2018-11-14 09:18:03 +00:00
// Tracable embeds tracing information.
type Tracable interface {
GetTracingInformation() (name string, spanKind ext.SpanKindEnum)
2018-01-10 16:48:04 +00:00
}
2018-11-14 09:18:03 +00:00
// 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
2018-01-10 16:48:04 +00:00
}
2018-11-14 09:18:03 +00:00
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
}
2018-06-11 09:36:03 +00:00
}
2020-05-11 10:06:07 +00:00
// NewWrapper returns a http.Handler struct.
2018-11-14 09:18:03 +00:00
func NewWrapper(next http.Handler, name string, spanKind ext.SpanKindEnum) http.Handler {
return &Wrapper{
next: next,
name: name,
spanKind: spanKind,
2018-01-10 16:48:04 +00:00
}
}
2018-11-14 09:18:03 +00:00
// Wrapper is used to wrap http handler middleware.
type Wrapper struct {
next http.Handler
name string
spanKind ext.SpanKindEnum
2018-06-11 09:36:03 +00:00
}
2018-11-14 09:18:03 +00:00
func (w *Wrapper) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
_, err := tracing.FromContext(req.Context())
if err != nil {
w.next.ServeHTTP(rw, req)
return
}
2018-01-10 16:48:04 +00:00
var finish func()
2018-11-14 09:18:03 +00:00
_, req, finish = tracing.StartSpan(req, w.name, w.spanKind)
2018-01-10 16:48:04 +00:00
defer finish()
2018-11-14 09:18:03 +00:00
if w.next != nil {
w.next.ServeHTTP(rw, req)
2018-01-10 16:48:04 +00:00
}
}