Skip to content

Commit fd98e83

Browse files
committed
contrib/gin-gonic/gin: refactor Option, deprecate OptionFn
Refactor Options handling. Deprecate OptionFn which was an implementation detail that is not needed anymore. This change is fully source compatible with existing client code. Also, as implementation details are now hidden behind the private 'option' type, the API is more future-proof. Full rationale for this change: #3612 Note: golangci-lint 2.1.6 reports lint error "unexported-return: exported func WithAnalytics returns unexported type gin.Option, which can be annoying to use (revive)" However this is false positive that I have reported in mgechev/revive#1406.
1 parent 809550c commit fd98e83

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

contrib/gin-gonic/gin/gintrace.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func init() {
3232
func Middleware(service string, opts ...Option) gin.HandlerFunc {
3333
cfg := newConfig(service)
3434
for _, opt := range opts {
35-
opt.apply(cfg)
35+
opt(cfg)
3636
}
3737
instr.Logger().Debug("contrib/gin-gonic/gin: Configuring Middleware: Service: %s, %#v", cfg.serviceName, cfg)
3838
spanOpts := []tracer.StartSpanOption{

contrib/gin-gonic/gin/option.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,19 @@ func newConfig(serviceName string) *config {
3636
}
3737
}
3838

39-
// Option describes options for the Gin integration.
40-
type Option interface {
41-
apply(*config)
42-
}
39+
// Option describes an option for the Gin integration.
40+
type Option = option
4341

44-
// OptionFn represents options applicable to Middleware.
45-
type OptionFn func(*config)
42+
// option is the hidden concrete implementation of [Option] using the functional options pattern.
43+
type option func(*config)
4644

47-
func (fn OptionFn) apply(cfg *config) {
48-
fn(cfg)
49-
}
45+
// OptionFn is an implementation detail of [Option].
46+
//
47+
// Deprecated: use [Option].
48+
type OptionFn = Option
5049

5150
// WithAnalytics enables Trace Analytics for all started spans.
52-
func WithAnalytics(on bool) OptionFn {
51+
func WithAnalytics(on bool) Option {
5352
return func(cfg *config) {
5453
if on {
5554
cfg.analyticsRate = 1.0
@@ -61,7 +60,7 @@ func WithAnalytics(on bool) OptionFn {
6160

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

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

9291
// WithIgnoreRequest specifies a function to use for determining if the
9392
// incoming HTTP request tracing should be skipped.
94-
func WithIgnoreRequest(f func(c *gin.Context) bool) OptionFn {
93+
func WithIgnoreRequest(f func(c *gin.Context) bool) Option {
9594
return func(cfg *config) {
9695
cfg.ignoreRequest = f
9796
}

0 commit comments

Comments
 (0)