1// Unless explicitly stated otherwise all files in this repository are licensed 2// under the Apache License Version 2.0. 3// This product includes software developed at Datadog (https://www.datadoghq.com/). 4// Copyright 2016 Datadog, Inc. 5 6package fiber 7 8import ( 9 "math" 10 11 "gopkg.in/DataDog/dd-trace-go.v1/ddtrace" 12 "gopkg.in/DataDog/dd-trace-go.v1/internal" 13 "gopkg.in/DataDog/dd-trace-go.v1/internal/globalconfig" 14) 15 16type config struct { 17 serviceName string 18 isStatusError func(statusCode int) bool 19 spanOpts []ddtrace.StartSpanOption // additional span options to be applied 20 analyticsRate float64 21} 22 23// Option represents an option that can be passed to NewRouter. 24type Option func(*config) 25 26func defaults(cfg *config) { 27 cfg.serviceName = "fiber" 28 cfg.isStatusError = isServerError 29 30 if svc := globalconfig.ServiceName(); svc != "" { 31 cfg.serviceName = svc 32 } 33 if internal.BoolEnv("DD_TRACE_FIBER_ENABLED", false) { 34 cfg.analyticsRate = 1.0 35 } else { 36 cfg.analyticsRate = globalconfig.AnalyticsRate() 37 } 38} 39 40// WithServiceName sets the given service name for the router. 41func WithServiceName(name string) Option { 42 return func(cfg *config) { 43 cfg.serviceName = name 44 } 45} 46 47// WithSpanOptions applies the given set of options to the spans started 48// by the router. 49func WithSpanOptions(opts ...ddtrace.StartSpanOption) Option { 50 return func(cfg *config) { 51 cfg.spanOpts = opts 52 } 53} 54 55// WithAnalytics enables Trace Analytics for all started spans. 56func WithAnalytics(on bool) Option { 57 return func(cfg *config) { 58 if on { 59 cfg.analyticsRate = 1.0 60 } else { 61 cfg.analyticsRate = math.NaN() 62 } 63 } 64} 65 66// WithAnalyticsRate sets the sampling rate for Trace Analytics events 67// correlated to started spans. 68func WithAnalyticsRate(rate float64) Option { 69 return func(cfg *config) { 70 if rate >= 0.0 && rate <= 1.0 { 71 cfg.analyticsRate = rate 72 } else { 73 cfg.analyticsRate = math.NaN() 74 } 75 } 76} 77 78// WithStatusCheck allow setting of a function to tell whether a status code is an error 79func WithStatusCheck(fn func(statusCode int) bool) Option { 80 return func(cfg *config) { 81 cfg.isStatusError = fn 82 } 83} 84 85func isServerError(statusCode int) bool { 86 return statusCode >= 500 && statusCode < 600 87} 88