Skip to content

Commit

Permalink
contrib/gofiber/fiber.v2: add possibility to exclude spans generation…
Browse files Browse the repository at this point in the history
… for specific requests (#2583)

Co-authored-by: nsakharenko <[email protected]>
Co-authored-by: Dario Castañé <[email protected]>
  • Loading branch information
3 people authored Mar 5, 2024
1 parent ccc12dc commit 47f3303
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
4 changes: 4 additions & 0 deletions contrib/gofiber/fiber.v2/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
30 changes: 30 additions & 0 deletions contrib/gofiber/fiber.v2/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions contrib/gofiber/fiber.v2/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 47f3303

Please sign in to comment.