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"
|
2020-09-16 13:46:04 +00:00
|
|
|
"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 {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(ctx, "tracing", entryPointTypeName).Debug().Msg("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 {
|
2022-11-21 17:36:05 +00:00
|
|
|
middlewares.GetLogger(req.Context(), "tracing", entryPointTypeName).
|
|
|
|
Debug().Err(err).Msg("Failed to extract the context")
|
2019-12-02 13:18:08 +00:00
|
|
|
}
|
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
|
|
|
|
2022-11-16 10:38:07 +00:00
|
|
|
recorder := newStatusCodeRecorder(rw, http.StatusOK)
|
2018-11-14 09:18:03 +00:00
|
|
|
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-07-31 22:16:03 +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
|
2018-07-31 22:16:03 +00:00
|
|
|
}
|
|
|
|
}
|