traefik/pkg/middlewares/tracing/entrypoint.go

63 lines
1.9 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"
2018-01-10 16:48:04 +00:00
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/middlewares"
"github.com/traefik/traefik/v2/pkg/tracing"
2018-01-10 16:48:04 +00:00
)
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 {
2019-09-13 17:28:04 +00:00
log.FromContext(middlewares.GetLoggerCtx(ctx, "tracing", entryPointTypeName)).Debug("Creating middleware")
2018-11-14 09:18:03 +00:00
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) {
2019-12-02 13:18:08 +00:00
spanCtx, err := e.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
if err != nil {
log.FromContext(middlewares.GetLoggerCtx(req.Context(), "tracing", entryPointTypeName)).
Debugf("Failed to extract the context: %v", err)
}
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
}
}