Skip to content

Commit

Permalink
Allow for disabling client IP collection in traces
Browse files Browse the repository at this point in the history
  • Loading branch information
gzuidhof committed Mar 18, 2024
1 parent dfea35d commit 026932f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
9 changes: 9 additions & 0 deletions otelfiber/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type config struct {
ServerName *string
SpanNameFormatter func(*fiber.Ctx) string
CustomAttributes func(*fiber.Ctx) []attribute.KeyValue
collectClientIP bool
}

// Option specifies instrumentation configuration options.
Expand Down Expand Up @@ -96,3 +97,11 @@ func WithCustomAttributes(f func(ctx *fiber.Ctx) []attribute.KeyValue) Option {
cfg.CustomAttributes = f
})
}

// WithCollectClientIP specifies whether to collect the client's IP address
// from the request. This is enabled by default.
func WithCollectClientIP(collect bool) Option {
return optionFunc(func(cfg *config) {
cfg.collectClientIP = collect
})
}
4 changes: 3 additions & 1 deletion otelfiber/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ const (

// Middleware returns fiber handler which will trace incoming requests.
func Middleware(opts ...Option) fiber.Handler {
cfg := config{}
cfg := config{
collectClientIP: true,
}
for _, opt := range opts {
opt.apply(&cfg)
}
Expand Down
38 changes: 38 additions & 0 deletions otelfiber/otelfiber_test/fiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,41 @@ func TestOutboundTracingPropagationWithInboundContext(t *testing.T) {
assert.Equal(t, traceId, resp.Header.Get("X-B3-TraceId"))
assert.Equal(t, "1", resp.Header.Get("X-B3-Sampled"))
}

func TestCollectClientIP(t *testing.T) {
t.Parallel()

for _, enabled := range []bool{true, false} {
enabled := enabled
t.Run(fmt.Sprintf("enabled=%t", enabled), func(t *testing.T) {
t.Parallel()

sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
otel.SetTracerProvider(provider)

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

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

spans := sr.Ended()
require.Len(t, spans, 1)

span := spans[0]
attrs := span.Attributes()
if enabled {
assert.Contains(t, attrs, attribute.String("http.client_ip", "0.0.0.0"))
} else {
assert.NotContains(t, attrs, attribute.String("http.client_ip", "0.0.0.0"))
}
})
}
}
8 changes: 5 additions & 3 deletions otelfiber/semconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ func httpServerTraceAttributesFromRequest(c *fiber.Ctx, cfg config) []attribute.
if username, ok := HasBasicAuth(c.Get(fiber.HeaderAuthorization)); ok {
attrs = append(attrs, semconv.EnduserIDKey.String(utils.CopyString(username)))
}
clientIP := c.IP()
if len(clientIP) > 0 {
attrs = append(attrs, semconv.HTTPClientIPKey.String(utils.CopyString(clientIP)))
if cfg.collectClientIP {
clientIP := c.IP()
if len(clientIP) > 0 {
attrs = append(attrs, semconv.HTTPClientIPKey.String(utils.CopyString(clientIP)))
}
}

if cfg.CustomAttributes != nil {
Expand Down

0 comments on commit 026932f

Please sign in to comment.