From 33cc541a8d5c2f2059c04087f4dcd6f870abe26b Mon Sep 17 00:00:00 2001 From: nsakharenko Date: Wed, 28 Feb 2024 14:42:24 +0200 Subject: [PATCH] feat(): add possibility to avoid spans creation for specific requests; --- contrib/gofiber/fiber.v2/fiber.go | 4 ++++ contrib/gofiber/fiber.v2/fiber_test.go | 30 ++++++++++++++++++++++++++ contrib/gofiber/fiber.v2/option.go | 14 ++++++++++++ 3 files changed, 48 insertions(+) diff --git a/contrib/gofiber/fiber.v2/fiber.go b/contrib/gofiber/fiber.v2/fiber.go index f60a4b46bb..7d9ef223fe 100644 --- a/contrib/gofiber/fiber.v2/fiber.go +++ b/contrib/gofiber/fiber.v2/fiber.go @@ -37,6 +37,10 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error { } log.Debug("gofiber/fiber.v2: Middleware: %#v", cfg) return func(c *fiber.Ctx) error { + if cfg.ignoreRequest(c) { + return c.Next() + } + opts := []ddtrace.StartSpanOption{ tracer.SpanType(ext.SpanTypeWeb), tracer.ServiceName(cfg.serviceName), diff --git a/contrib/gofiber/fiber.v2/fiber_test.go b/contrib/gofiber/fiber.v2/fiber_test.go index c48a4f6391..6bd0fb786b 100644 --- a/contrib/gofiber/fiber.v2/fiber_test.go +++ b/contrib/gofiber/fiber.v2/fiber_test.go @@ -332,3 +332,33 @@ func TestNamingSchema(t *testing.T) { }) namingschematest.NewHTTPServerTest(genSpans, "fiber")(t) } + +func TestIgnoreRequest(t *testing.T) { + assert := assert.New(t) + mt := mocktracer.Start() + defer mt.Stop() + + router := fiber.New() + router.Use( + Middleware( + WithIgnoreRequest(func(ctx *fiber.Ctx) bool { + return ctx.Method() == "GET" && ctx.Path() == "/ignore" + }), + ), + ) + router.Get("/ignore", func(c *fiber.Ctx) error { + return c.SendString("IAMALIVE") + }) + + r := httptest.NewRequest("GET", "/ignore", nil) + + // do and verify the request + resp, err := router.Test(r) + assert.Equal(nil, err) + defer resp.Body.Close() + assert.Equal(resp.StatusCode, 200) + + spans := mt.FinishedSpans() + + assert.Len(spans, 0) +} diff --git a/contrib/gofiber/fiber.v2/option.go b/contrib/gofiber/fiber.v2/option.go index d6b4e7c94c..5af4689cf9 100644 --- a/contrib/gofiber/fiber.v2/option.go +++ b/contrib/gofiber/fiber.v2/option.go @@ -25,6 +25,7 @@ type config struct { spanOpts []ddtrace.StartSpanOption // additional span options to be applied analyticsRate float64 resourceNamer func(*fiber.Ctx) string + ignoreRequest func(*fiber.Ctx) bool } // Option represents an option that can be passed to NewRouter. @@ -35,6 +36,7 @@ func defaults(cfg *config) { cfg.spanName = namingschema.OpName(namingschema.HTTPServer) cfg.isStatusError = isServerError cfg.resourceNamer = defaultResourceNamer + cfg.ignoreRequest = defaultIgnoreRequest if internal.BoolEnv("DD_TRACE_FIBER_ENABLED", false) { cfg.analyticsRate = 1.0 @@ -97,11 +99,23 @@ func WithResourceNamer(fn func(*fiber.Ctx) string) Option { } } +// WithIgnoreRequest specifies a function which will be used to +// determining if the incoming HTTP request tracing should be skipped. +func WithIgnoreRequest(fn func(*fiber.Ctx) bool) Option { + return func(cfg *config) { + cfg.ignoreRequest = fn + } +} + func defaultResourceNamer(c *fiber.Ctx) string { r := c.Route() return r.Method + " " + r.Path } +func defaultIgnoreRequest(*fiber.Ctx) bool { + return false +} + func isServerError(statusCode int) bool { return statusCode >= 500 && statusCode < 600 }