Skip to content

contrib/gin-gonic/gin: refactor Option, deprecate OptionFn #3650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contrib/gin-gonic/gin/gintrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func init() {
func Middleware(service string, opts ...Option) gin.HandlerFunc {
cfg := newConfig(service)
for _, opt := range opts {
opt.apply(cfg)
opt(cfg)
}
instr.Logger().Debug("contrib/gin-gonic/gin: Configuring Middleware: Service: %s, %#v", cfg.serviceName, cfg)
spanOpts := []tracer.StartSpanOption{
Expand Down
27 changes: 13 additions & 14 deletions contrib/gin-gonic/gin/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,19 @@ func newConfig(serviceName string) *config {
}
}

// Option describes options for the Gin integration.
type Option interface {
apply(*config)
}
// Option describes an option for the Gin integration.
type Option = option

// OptionFn represents options applicable to Middleware.
type OptionFn func(*config)
// option is the hidden concrete implementation of [Option] using the functional options pattern.
type option func(*config)

func (fn OptionFn) apply(cfg *config) {
fn(cfg)
}
// OptionFn is an implementation detail of [Option].
//
// Deprecated: use [Option].
type OptionFn = Option

// WithAnalytics enables Trace Analytics for all started spans.
func WithAnalytics(on bool) OptionFn {
func WithAnalytics(on bool) Option {
return func(cfg *config) {
if on {
cfg.analyticsRate = 1.0
Expand All @@ -61,7 +60,7 @@ func WithAnalytics(on bool) OptionFn {

// WithAnalyticsRate sets the sampling rate for Trace Analytics events
// correlated to started spans.
func WithAnalyticsRate(rate float64) OptionFn {
func WithAnalyticsRate(rate float64) Option {
return func(cfg *config) {
if rate >= 0.0 && rate <= 1.0 {
cfg.analyticsRate = rate
Expand All @@ -73,7 +72,7 @@ func WithAnalyticsRate(rate float64) OptionFn {

// WithResourceNamer specifies a function which will be used to obtain a resource name for a given
// gin request, using the request's context.
func WithResourceNamer(namer func(c *gin.Context) string) OptionFn {
func WithResourceNamer(namer func(c *gin.Context) string) Option {
return func(cfg *config) {
cfg.resourceNamer = namer
}
Expand All @@ -83,15 +82,15 @@ func WithResourceNamer(namer func(c *gin.Context) string) OptionFn {
// Warning:
// Using this feature can risk exposing sensitive data such as authorization tokens to Datadog.
// Special headers can not be sub-selected. E.g., an entire Cookie header would be transmitted, without the ability to choose specific Cookies.
func WithHeaderTags(headers []string) OptionFn {
func WithHeaderTags(headers []string) Option {
return func(cfg *config) {
cfg.headerTags = instrumentation.NewHeaderTags(headers)
}
}

// WithIgnoreRequest specifies a function to use for determining if the
// incoming HTTP request tracing should be skipped.
func WithIgnoreRequest(f func(c *gin.Context) bool) OptionFn {
func WithIgnoreRequest(f func(c *gin.Context) bool) Option {
return func(cfg *config) {
cfg.ignoreRequest = f
}
Expand Down