Skip to content

Commit

Permalink
contrib/gofiber: add gofiber trace propagation (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
mckeown-dd committed Nov 21, 2022
1 parent 9b50352 commit 2d50f9c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
13 changes: 10 additions & 3 deletions contrib/gofiber/fiber.v2/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ import (
"net/http"
"strconv"

"github.com/gofiber/fiber/v2"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"

"github.com/gofiber/fiber/v2"
)

// Middleware returns middleware that will trace incoming requests.
Expand All @@ -39,7 +39,14 @@ func Middleware(opts ...Option) func(c *fiber.Ctx) error {
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}

// Create a http.Header object so that a parent trace can be extracted. Fiber uses a non-standard header carrier
h := http.Header{}
for k, v := range c.GetReqHeaders() {
h.Add(k, v)
}
if spanctx, err := tracer.Extract(tracer.HTTPHeadersCarrier(h)); err == nil {
opts = append(opts, tracer.ChildOf(spanctx))
}
opts = append(opts, cfg.spanOpts...)
span, ctx := tracer.StartSpanFromContext(c.Context(), "http.request", opts...)

Expand Down
25 changes: 18 additions & 7 deletions contrib/gofiber/fiber.v2/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,30 @@ func TestPropagation(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()

r := httptest.NewRequest("GET", "/user/123", nil)

requestWithSpan := httptest.NewRequest("GET", "/span/exists/true", nil)
pspan := tracer.StartSpan("test")
tracer.Inject(pspan.Context(), tracer.HTTPHeadersCarrier(r.Header))
tracer.Inject(pspan.Context(), tracer.HTTPHeadersCarrier(requestWithSpan.Header))

requestWithoutSpan := httptest.NewRequest("GET", "/span/exists/false", nil)

router := fiber.New()
router.Use(Middleware(WithServiceName("foobar")))
router.Get("/user/:id", func(c *fiber.Ctx) error {
return c.SendString(c.Params("id"))
router.Get("/span/exists/true", func(c *fiber.Ctx) error {
s, _ := tracer.SpanFromContext(c.UserContext())
assert.Equal(s.Context().TraceID() == pspan.Context().TraceID(), true)
return c.SendString(c.Params("span exists"))
})
router.Get("/span/exists/false", func(c *fiber.Ctx) error {
s, _ := tracer.SpanFromContext(c.UserContext())
assert.Equal(s.Context().TraceID() == pspan.Context().TraceID(), false)
return c.SendString(c.Params("span does not exist"))
})

_, err := router.Test(r)
assert.Equal(nil, err)
_, withoutErr := router.Test(requestWithoutSpan)
assert.Equal(nil, withoutErr)

_, withErr := router.Test(requestWithSpan)
assert.Equal(nil, withErr)
}

func TestAnalyticsSettings(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
github.com/go-redis/redis/v8 v8.0.0
github.com/go-sql-driver/mysql v1.6.0
github.com/gocql/gocql v0.0.0-20220224095938-0eacd3183625
github.com/gofiber/fiber/v2 v2.11.0
github.com/gofiber/fiber/v2 v2.24.0
github.com/golang/protobuf v1.5.2
github.com/gomodule/redigo v1.8.9
github.com/google/pprof v0.0.0-20210423192551-a2663126120b
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ github.com/go-test/deep v1.0.2 h1:onZX1rnHT3Wv6cqNgYyFOOlgVKJrksuCMCRvJStbMYw=
github.com/go-test/deep v1.0.2/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gocql/gocql v0.0.0-20220224095938-0eacd3183625 h1:6ImvI6U901e1ezn/8u2z3bh1DZIvMOia0yTSBxhy4Ao=
github.com/gocql/gocql v0.0.0-20220224095938-0eacd3183625/go.mod h1:3gM2c4D3AnkISwBxGnMMsS8Oy4y2lhbPRsH4xnJrHG8=
github.com/gofiber/fiber/v2 v2.11.0 h1:97PoVZI3JLlJyfMHFhKZoEHQEfTwOXvhQs2+YoLr9jk=
github.com/gofiber/fiber/v2 v2.11.0/go.mod h1:oZTLWqYnqpMMuF922SjGbsYZsdpE1MCfh416HNdweIM=
github.com/gofiber/fiber/v2 v2.24.0 h1:18rpLoQMJBVlLtX/PwgHj3hIxPSeWfN1YeDJ2lEnzjU=
github.com/gofiber/fiber/v2 v2.24.0/go.mod h1:MR1usVH3JHYRyQwMe2eZXRSZHRX38fkV+A7CPB+DlDQ=
github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.0.0+incompatible h1:1SD/1F5pU8p29ybwgQSwpQk+mwdRrXCYuPhW6m+TnJw=
github.com/gofrs/uuid v4.0.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
Expand Down Expand Up @@ -516,7 +516,7 @@ github.com/kevinmbeaulieu/eq-go v1.0.0/go.mod h1:G3S8ajA56gKBZm4UB9AOyoOS37JO3ro
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.12.2/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.15.0 h1:xqfchp4whNFxn5A4XFyyYtitiWI8Hy5EW59jEwcyL6U=
Expand Down Expand Up @@ -768,7 +768,7 @@ github.com/urfave/negroni v1.0.0 h1:kIimOitoypq34K7TG7DUaJ9kq/N4Ofuwi1sjz0KipXc=
github.com/urfave/negroni v1.0.0/go.mod h1:Meg73S6kFm/4PpbYdq35yYWoCZ9mS/YSx+lKnmiohz4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.26.0/go.mod h1:cmWIqlu99AO/RKcp1HWaViTqc57FswJOfYYdPJBl8BA=
github.com/valyala/fasthttp v1.31.0/go.mod h1:2rsYD01CKFrjjsvFxx75KlEUNpWNBY9JWD3K/7o2Cus=
github.com/valyala/fasthttp v1.34.0 h1:d3AAQJ2DRcxJYHm7OXNXtXt2as1vMDfxeIcFvhmGGm4=
github.com/valyala/fasthttp v1.34.0/go.mod h1:epZA5N+7pY6ZaEKRmstzOuYJx9HI8DI1oaCGZpdH4h0=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
Expand Down

0 comments on commit 2d50f9c

Please sign in to comment.