From 9e23b52d2b6761215fb083e2ba470c33e2238e7b Mon Sep 17 00:00:00 2001 From: Hannah Kim Date: Mon, 28 Oct 2024 10:49:39 -0400 Subject: [PATCH] move common func to options --- .../julienschmidt/httprouter/internal/tracing/config.go | 4 ++-- contrib/net/http/option.go | 5 +++-- instrumentation/httptrace/httptrace.go | 8 -------- instrumentation/options/options.go | 9 +++++++++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/contrib/julienschmidt/httprouter/internal/tracing/config.go b/contrib/julienschmidt/httprouter/internal/tracing/config.go index 7d2646e5e0..7628f25ed7 100644 --- a/contrib/julienschmidt/httprouter/internal/tracing/config.go +++ b/contrib/julienschmidt/httprouter/internal/tracing/config.go @@ -11,7 +11,7 @@ import ( "github.com/DataDog/dd-trace-go/v2/ddtrace/ext" "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" "github.com/DataDog/dd-trace-go/v2/instrumentation" - "github.com/DataDog/dd-trace-go/v2/instrumentation/httptrace" + "github.com/DataDog/dd-trace-go/v2/instrumentation/options" ) type Config struct { @@ -23,7 +23,7 @@ type Config struct { func NewConfig(opts ...Option) *Config { cfg := new(Config) - if httptrace.GetBoolEnv("DD_TRACE_HTTPROUTER_ANALYTICS_ENABLED", false) { + if options.GetBoolEnv("DD_TRACE_HTTPROUTER_ANALYTICS_ENABLED", false) { cfg.analyticsRate = 1.0 } else { cfg.analyticsRate = instr.AnalyticsRate(true) diff --git a/contrib/net/http/option.go b/contrib/net/http/option.go index 30c4017c01..42513e6dd5 100644 --- a/contrib/net/http/option.go +++ b/contrib/net/http/option.go @@ -14,6 +14,7 @@ import ( "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" "github.com/DataDog/dd-trace-go/v2/instrumentation" "github.com/DataDog/dd-trace-go/v2/instrumentation/httptrace" + "github.com/DataDog/dd-trace-go/v2/instrumentation/options" ) type commonConfig struct { @@ -62,7 +63,7 @@ func (o HandlerOptionFn) apply(cfg *config) { } func defaults(cfg *config) { - if httptrace.GetBoolEnv("DD_TRACE_HTTP_ANALYTICS_ENABLED", false) { + if options.GetBoolEnv("DD_TRACE_HTTP_ANALYTICS_ENABLED", false) { cfg.analyticsRate = 1.0 } else { cfg.analyticsRate = instr.AnalyticsRate(true) @@ -200,7 +201,7 @@ func newRoundTripperConfig() *roundTripperConfig { commonConfig: sharedCfg, propagation: true, spanNamer: defaultSpanNamer, - queryString: httptrace.GetBoolEnv(envClientQueryStringEnabled, true), + queryString: options.GetBoolEnv(envClientQueryStringEnabled, true), isStatusError: isClientError, } v := os.Getenv(envClientErrorStatuses) diff --git a/instrumentation/httptrace/httptrace.go b/instrumentation/httptrace/httptrace.go index 20076451b7..c12d18d841 100644 --- a/instrumentation/httptrace/httptrace.go +++ b/instrumentation/httptrace/httptrace.go @@ -17,7 +17,6 @@ import ( "github.com/DataDog/dd-trace-go/v2/ddtrace/ext" "github.com/DataDog/dd-trace-go/v2/ddtrace/tracer" "github.com/DataDog/dd-trace-go/v2/instrumentation" - "github.com/DataDog/dd-trace-go/v2/internal" "github.com/DataDog/dd-trace-go/v2/internal/appsec/listener/httpsec" ) @@ -144,10 +143,3 @@ func HeaderTagsFromRequest(req *http.Request, headerTags instrumentation.HeaderT } } } - -// This is a workaround needed because of v2 changes that prevents contribs from accessing -// the internal directory. This function should not be used if the internal directory -// can be -func GetBoolEnv(key string, def bool) bool { - return internal.BoolEnv(key, def) -} diff --git a/instrumentation/options/options.go b/instrumentation/options/options.go index 4be38a67df..ab2cb8c296 100644 --- a/instrumentation/options/options.go +++ b/instrumentation/options/options.go @@ -5,6 +5,8 @@ package options +import "github.com/DataDog/dd-trace-go/v2/internal" + // Copy should be used any time existing options are copied into // a new locally scoped set of options. This is to avoid data races and // accidental side effects. @@ -29,3 +31,10 @@ func Expand[T any](opts []T, initialPosition, trailCapacity int) []T { copy(dup[initialPosition:], opts) return dup } + +// This is a workaround needed because of v2 changes that prevents contribs from accessing +// the internal directory. This function should not be used if the internal directory +// can be +func GetBoolEnv(key string, def bool) bool { + return internal.BoolEnv(key, def) +}