From 305712928b97b2ce22945b2580861807235ad8e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Czig=C3=A1ny?= Date: Sat, 18 Sep 2021 09:30:51 +0200 Subject: [PATCH 1/2] rename TransacationName to OperationName as that is how opentracing refers to it --- README.md | 14 +++++++------- config.go | 14 +++++++------- example/example.go | 2 +- trace.go | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 48b8660..5ae23de 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ fiber-opentraing middleware support opentracing for [Fiber](https://github.com/g go get -u github.com/gofiber/v2 go get -u github.com/aschenmaker/fiber-opentracing ``` -default use +default use ```go import ( "github.com/gofiber/fiber/v2" @@ -20,10 +20,10 @@ app.Use(fibertracing.New()) Middleware has 4 configs. ```go type Config struct { - Tracer opentracing.Tracer - TransacationName func(*fiber.Ctx) string - Filter func(*fiber.Ctx) bool - Modify func(*fiber.Ctx, opentracing.Span) + Tracer opentracing.Tracer + OperationName func(*fiber.Ctx) string + Filter func(*fiber.Ctx) bool + Modify func(*fiber.Ctx, opentracing.Span) } ``` @@ -46,13 +46,13 @@ import ( func main() { app := *fiber.New() // Use jaeger default config. - // You can use Jaeger-all-in-one + // You can use Jaeger-all-in-one // and then check trace in JaegerUI fjaeger.New(fjaeger.Config{}) app.Use(fibertracing.New(fibertracing.Config{ Tracer: opentracing.GlobalTracer(), - TransacationName: func(ctx *fiber.Ctx) string { + OperationName: func(ctx *fiber.Ctx) string { return "TEST: HTTP " + ctx.Method() + " URL: " + ctx.Path() }, })) diff --git a/config.go b/config.go index 48cb4a0..5dac993 100644 --- a/config.go +++ b/config.go @@ -7,10 +7,10 @@ import ( // Config defines the config of middlewares type Config struct { - Tracer opentracing.Tracer - TransacationName func(*fiber.Ctx) string - Filter func(*fiber.Ctx) bool - Modify func(*fiber.Ctx, opentracing.Span) + Tracer opentracing.Tracer + OperationName func(*fiber.Ctx) string + Filter func(*fiber.Ctx) bool + Modify func(*fiber.Ctx, opentracing.Span) } // ConfigDefault is the default config @@ -23,7 +23,7 @@ var ConfigDefault = Config{ span.SetTag("http.host", ctx.Hostname()) span.SetTag("http.url", ctx.OriginalURL()) }, - TransacationName: func(ctx *fiber.Ctx) string { + OperationName: func(ctx *fiber.Ctx) string { return "HTTP " + ctx.Method() + " URL: " + ctx.Path() }, } @@ -40,8 +40,8 @@ func configDefault(config ...Config) Config { cfg.Tracer = ConfigDefault.Tracer } - if cfg.TransacationName == nil { - cfg.TransacationName = ConfigDefault.TransacationName + if cfg.OperationName == nil { + cfg.OperationName = ConfigDefault.OperationName } if cfg.Modify == nil { diff --git a/example/example.go b/example/example.go index 6f08ef3..9e0dc91 100644 --- a/example/example.go +++ b/example/example.go @@ -17,7 +17,7 @@ func main() { app.Use(fibertracing.New(fibertracing.Config{ Tracer: opentracing.GlobalTracer(), - TransacationName: func(ctx *fiber.Ctx) string { + OperationName: func(ctx *fiber.Ctx) string { return "TEST: HTTP " + ctx.Method() + " URL: " + ctx.Path() }, })) diff --git a/trace.go b/trace.go index a30a21f..325f351 100644 --- a/trace.go +++ b/trace.go @@ -19,7 +19,7 @@ func New(config Config) fiber.Handler { } var span opentracing.Span - TransacationName := cfg.TransacationName(c) + operationName := cfg.OperationName(c) tracer := cfg.Tracer header := make(http.Header) @@ -33,9 +33,9 @@ func New(config Config) fiber.Handler { // Extract trace-id from header sc, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(header)) if err == nil { - span = tracer.StartSpan(TransacationName, opentracing.ChildOf(sc)) + span = tracer.StartSpan(operationName, opentracing.ChildOf(sc)) } else { - span = tracer.StartSpan(TransacationName) + span = tracer.StartSpan(operationName) } cfg.Modify(c, span) From e4955becbab8f3cf10d86d173a75e03fdb456ece Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20Czig=C3=A1ny?= Date: Sat, 18 Sep 2021 09:44:33 +0200 Subject: [PATCH 2/2] config.go: add SkipSpanWithoutParent option --- README.md | 11 ++++++----- config.go | 9 +++++---- trace.go | 4 +++- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 5ae23de..f5e4e8c 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,14 @@ app.Use(fibertracing.New()) ``` ## Config -Middleware has 4 configs. +Middleware has 5 configs. ```go type Config struct { - Tracer opentracing.Tracer - OperationName func(*fiber.Ctx) string - Filter func(*fiber.Ctx) bool - Modify func(*fiber.Ctx, opentracing.Span) + Tracer opentracing.Tracer + OperationName func(*fiber.Ctx) string + Filter func(*fiber.Ctx) bool + Modify func(*fiber.Ctx, opentracing.Span) + SkipSpanWithoutParent bool } ``` diff --git a/config.go b/config.go index 5dac993..984f1ab 100644 --- a/config.go +++ b/config.go @@ -7,10 +7,11 @@ import ( // Config defines the config of middlewares type Config struct { - Tracer opentracing.Tracer - OperationName func(*fiber.Ctx) string - Filter func(*fiber.Ctx) bool - Modify func(*fiber.Ctx, opentracing.Span) + Tracer opentracing.Tracer + OperationName func(*fiber.Ctx) string + Filter func(*fiber.Ctx) bool + Modify func(*fiber.Ctx, opentracing.Span) + SkipSpanWithoutParent bool } // ConfigDefault is the default config diff --git a/trace.go b/trace.go index 325f351..8f7d635 100644 --- a/trace.go +++ b/trace.go @@ -34,8 +34,10 @@ func New(config Config) fiber.Handler { sc, err := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(header)) if err == nil { span = tracer.StartSpan(operationName, opentracing.ChildOf(sc)) - } else { + } else if !cfg.SkipSpanWithoutParent { span = tracer.StartSpan(operationName) + } else { + return c.Next() } cfg.Modify(c, span)