traefik/pkg/middlewares/tracing/entrypoint.go

58 lines
1.7 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"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/middlewares"
"github.com/containous/traefik/v2/pkg/tracing"
2018-01-10 16:48:04 +00:00
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
)
2018-11-14 09:18:03 +00:00
const (
entryPointTypeName = "TracingEntryPoint"
)
// NewEntryPoint creates a new middleware that the incoming request.
func NewEntryPoint(ctx context.Context, t *tracing.Tracing, entryPointName string, next http.Handler) http.Handler {
middlewares.GetLogger(ctx, "tracing", entryPointTypeName).Debug("Creating middleware")
return &entryPointMiddleware{
entryPoint: entryPointName,
Tracing: t,
next: next,
}
}
2018-01-10 16:48:04 +00:00
type entryPointMiddleware struct {
2018-11-14 09:18:03 +00:00
*tracing.Tracing
2018-01-10 16:48:04 +00:00
entryPoint string
2018-11-14 09:18:03 +00:00
next http.Handler
2018-01-10 16:48:04 +00:00
}
2018-11-14 09:18:03 +00:00
func (e *entryPointMiddleware) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
spanCtx, _ := e.Extract(opentracing.HTTPHeaders, tracing.HTTPHeadersCarrier(req.Header))
2018-01-10 16:48:04 +00:00
2018-11-14 09:18:03 +00:00
span, req, finish := e.StartSpanf(req, ext.SpanKindRPCServerEnum, "EntryPoint", []string{e.entryPoint, req.Host}, " ", ext.RPCServerOption(spanCtx))
defer finish()
2018-01-10 16:48:04 +00:00
ext.Component.Set(span, e.ServiceName)
2018-11-14 09:18:03 +00:00
tracing.LogRequest(span, req)
2018-01-10 16:48:04 +00:00
2018-11-14 09:18:03 +00:00
req = req.WithContext(tracing.WithTracing(req.Context(), e.Tracing))
2018-01-10 16:48:04 +00:00
2018-11-14 09:18:03 +00:00
recorder := newStatusCodeRecoder(rw, http.StatusOK)
e.next.ServeHTTP(recorder, req)
2018-01-10 16:48:04 +00:00
2018-11-14 09:18:03 +00:00
tracing.LogResponseCode(span, recorder.Status())
2018-01-10 16:48:04 +00:00
}
2018-11-14 09:18:03 +00:00
// WrapEntryPointHandler Wraps tracing to alice.Constructor.
func WrapEntryPointHandler(ctx context.Context, tracer *tracing.Tracing, entryPointName string) alice.Constructor {
return func(next http.Handler) (http.Handler, error) {
return NewEntryPoint(ctx, tracer, entryPointName, next), nil
}
}