From 47faae25d7cfabb84e70477d1810c16622c64d40 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Thu, 20 Jul 2023 15:02:07 +0200 Subject: [PATCH] fix: traceability of the middleware plugins --- pkg/server/middleware/middlewares.go | 2 +- pkg/server/middleware/plugins.go | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/server/middleware/middlewares.go b/pkg/server/middleware/middlewares.go index 741c9a4f0..0f666c996 100644 --- a/pkg/server/middleware/middlewares.go +++ b/pkg/server/middleware/middlewares.go @@ -357,7 +357,7 @@ func (b *Builder) buildConstructor(ctx context.Context, middlewareName string) ( } middleware = func(next http.Handler) (http.Handler, error) { - return plug(ctx, next) + return newTraceablePlugin(ctx, middlewareName, plug, next) } } diff --git a/pkg/server/middleware/plugins.go b/pkg/server/middleware/plugins.go index 3a092f140..dfde88468 100644 --- a/pkg/server/middleware/plugins.go +++ b/pkg/server/middleware/plugins.go @@ -1,10 +1,14 @@ package middleware import ( + "context" "errors" + "net/http" + "github.com/opentracing/opentracing-go/ext" "github.com/traefik/traefik/v2/pkg/config/dynamic" "github.com/traefik/traefik/v2/pkg/plugins" + "github.com/traefik/traefik/v2/pkg/tracing" ) // PluginsBuilder the plugin's builder interface. @@ -31,3 +35,25 @@ func findPluginConfig(rawConfig map[string]dynamic.PluginConf) (string, map[stri return pluginType, rawPluginConfig, nil } + +type traceablePlugin struct { + name string + h http.Handler +} + +func newTraceablePlugin(ctx context.Context, name string, plug plugins.Constructor, next http.Handler) (*traceablePlugin, error) { + h, err := plug(ctx, next) + if err != nil { + return nil, err + } + + return &traceablePlugin{name: name, h: h}, nil +} + +func (s *traceablePlugin) ServeHTTP(rw http.ResponseWriter, req *http.Request) { + s.h.ServeHTTP(rw, req) +} + +func (s *traceablePlugin) GetTracingInformation() (string, ext.SpanKindEnum) { + return s.name, tracing.SpanKindNoneEnum +}