Skip to content

Commit

Permalink
Merge pull request #848 from ledex/feature/issue-836/propergate-traci…
Browse files Browse the repository at this point in the history
…ng-headers-in-outbound-response

[Feature]: otelfiber - propagate tracing context as headers in outbound response
  • Loading branch information
ReneWerner87 authored Feb 13, 2024
2 parents cc6d7b5 + 20a0315 commit d5a794c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions otelfiber/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ func Middleware(opts ...Option) fiber.Handler {
spanStatus, spanMessage := semconv.SpanStatusFromHTTPStatusCodeAndSpanKind(c.Response().StatusCode(), oteltrace.SpanKindServer)
span.SetStatus(spanStatus, spanMessage)

//Propagate tracing context as headers in outbound response
tracingHeaders := make(propagation.HeaderCarrier)
cfg.Propagators.Inject(c.UserContext(), tracingHeaders)
for _, headerKey := range tracingHeaders.Keys() {
c.Set(headerKey, tracingHeaders.Get(headerKey))
}

return nil
}
}
Expand Down
50 changes: 50 additions & 0 deletions otelfiber/otelfiber_test/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,3 +446,53 @@ func TestCustomAttributes(t *testing.T) {
assert.Contains(t, attr, attribute.String("http.route", "/user/:id"))
assert.Contains(t, attr, attribute.String("http.query_params", "foo=bar"))
}

func TestOutboundTracingPropagation(t *testing.T) {
sr := new(tracetest.SpanRecorder)
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

app := fiber.New()
app.Use(otelfiber.Middleware(
otelfiber.WithTracerProvider(provider),
otelfiber.WithPropagators(b3prop.New(b3prop.WithInjectEncoding(b3prop.B3MultipleHeader))),
))
app.Get("/foo", func(ctx *fiber.Ctx) error {
return ctx.SendStatus(http.StatusNoContent)
})

resp, _ := app.Test(httptest.NewRequest("GET", "/foo", nil), 3000)

assert.Equal(t, "1", resp.Header.Get("X-B3-Sampled"))
assert.NotEmpty(t, resp.Header.Get("X-B3-SpanId"))
assert.NotEmpty(t, resp.Header.Get("X-B3-TraceId"))

}

func TestOutboundTracingPropagationWithInboundContext(t *testing.T) {
const spanId = "619907d88b766fb8"
const traceId = "813dd2766ff711bf02b60e9883014964"

sr := new(tracetest.SpanRecorder)
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

app := fiber.New()
app.Use(otelfiber.Middleware(
otelfiber.WithTracerProvider(provider),
otelfiber.WithPropagators(b3prop.New(b3prop.WithInjectEncoding(b3prop.B3MultipleHeader))),
))
app.Get("/foo", func(ctx *fiber.Ctx) error {
return ctx.SendStatus(http.StatusNoContent)
})

req := httptest.NewRequest("GET", "/foo", nil)

req.Header.Set("X-B3-SpanId", spanId)
req.Header.Set("X-B3-TraceId", traceId)
req.Header.Set("X-B3-Sampled", "1")

resp, _ := app.Test(req, 3000)

assert.NotEmpty(t, resp.Header.Get("X-B3-SpanId"))
assert.Equal(t, traceId, resp.Header.Get("X-B3-TraceId"))
assert.Equal(t, "1", resp.Header.Get("X-B3-Sampled"))
}

0 comments on commit d5a794c

Please sign in to comment.