diff --git a/README.md b/README.md index 48b8660..f5e4e8c 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" @@ -17,13 +17,14 @@ app.Use(fibertracing.New()) ``` ## Config -Middleware has 4 configs. +Middleware has 5 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) + SkipSpanWithoutParent bool } ``` @@ -46,13 +47,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..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 - 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) + SkipSpanWithoutParent bool } // ConfigDefault is the default config @@ -23,7 +24,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 +41,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..8f7d635 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,11 @@ 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 if !cfg.SkipSpanWithoutParent { + span = tracer.StartSpan(operationName) } else { - span = tracer.StartSpan(TransacationName) + return c.Next() } cfg.Modify(c, span)