Skip to content

Commit 317315d

Browse files
committed
refactor: rename GinFilter and GinMetricAttributeFn to Filter and MetricAttributeFn
1 parent 79ae41a commit 317315d

File tree

3 files changed

+29
-29
lines changed

3 files changed

+29
-29
lines changed

instrumentation/github.com/gin-gonic/gin/otelgin/config.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ import (
1919
)
2020

2121
type config struct {
22-
TracerProvider oteltrace.TracerProvider
23-
Propagators propagation.TextMapPropagator
24-
GinFilters []GinFilter
25-
SpanNameFormatter SpanNameFormatter
26-
MeterProvider metric.MeterProvider
27-
GinMetricAttributeFn GinMetricAttributeFn
22+
TracerProvider oteltrace.TracerProvider
23+
Propagators propagation.TextMapPropagator
24+
Filters []Filter
25+
SpanNameFormatter SpanNameFormatter
26+
MeterProvider metric.MeterProvider
27+
MetricAttributeFn MetricAttributeFn
2828
}
2929

3030
// defaultSpanNameFormatter is the default span name formatter.
@@ -47,15 +47,15 @@ var defaultSpanNameFormatter SpanNameFormatter = func(c *gin.Context) string {
4747
return method
4848
}
4949

50-
// GinFilter filters an [net/http.Request] based on content of a [gin.Context].
51-
type GinFilter func(*gin.Context) bool
50+
// Filter filters an [net/http.Request] based on content of a [gin.Context].
51+
type Filter func(*gin.Context) bool
5252

5353
// SpanNameFormatter is used by `WithSpanNameFormatter` to customize the request's span name.
5454
type SpanNameFormatter func(*gin.Context) string
5555

56-
// GinMetricAttributeFn is used to extract additional attributes from the gin.Context
56+
// MetricAttributeFn is used to extract additional attributes from the gin.Context
5757
// and return them as a slice of attribute.KeyValue.
58-
type GinMetricAttributeFn func(*gin.Context) []attribute.KeyValue
58+
type MetricAttributeFn func(*gin.Context) []attribute.KeyValue
5959

6060
// Option specifies instrumentation configuration options.
6161
type Option interface {
@@ -89,10 +89,10 @@ func WithTracerProvider(provider oteltrace.TracerProvider) Option {
8989
})
9090
}
9191

92-
// WithGinFilter adds a gin filter to the list of filters used by the handler.
93-
func WithGinFilter(f ...GinFilter) Option {
92+
// WithFilter adds a gin filter to the list of filters used by the handler.
93+
func WithFilter(f ...Filter) Option {
9494
return optionFunc(func(c *config) {
95-
c.GinFilters = append(c.GinFilters, f...)
95+
c.Filters = append(c.Filters, f...)
9696
})
9797
}
9898

@@ -112,12 +112,12 @@ func WithMeterProvider(mp metric.MeterProvider) Option {
112112
})
113113
}
114114

115-
// WithGinMetricAttributeFn specifies a function that extracts additional attributes from the gin.Context
115+
// WithMetricAttributeFn specifies a function that extracts additional attributes from the gin.Context
116116
// and returns them as a slice of attribute.KeyValue.
117117
//
118118
// If attributes are duplicated between this method and `WithMetricAttributeFn`, the attributes in this method will be used.
119-
func WithGinMetricAttributeFn(f GinMetricAttributeFn) Option {
119+
func WithMetricAttributeFn(f MetricAttributeFn) Option {
120120
return optionFunc(func(c *config) {
121-
c.GinMetricAttributeFn = f
121+
c.MetricAttributeFn = f
122122
})
123123
}

instrumentation/github.com/gin-gonic/gin/otelgin/gin.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
6161
return func(c *gin.Context) {
6262
requestStartTime := time.Now()
6363

64-
for _, f := range cfg.GinFilters {
64+
for _, f := range cfg.Filters {
6565
if !f(c) {
6666
// Serve the request to the next middleware
6767
// if a filter rejects the request.
@@ -114,8 +114,8 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
114114

115115
// Record the server-side attributes.
116116
var additionalAttributes []attribute.KeyValue
117-
if cfg.GinMetricAttributeFn != nil {
118-
additionalAttributes = append(additionalAttributes, cfg.GinMetricAttributeFn(c)...)
117+
if cfg.MetricAttributeFn != nil {
118+
additionalAttributes = append(additionalAttributes, cfg.MetricAttributeFn(c)...)
119119
}
120120

121121
sc.RecordMetrics(ctx, semconv.ServerMetricData{

instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ func TestWithGinFilter(t *testing.T) {
395395

396396
router := gin.New()
397397
f := func(c *gin.Context) bool { return c.Request.URL.Path != "/healthcheck" }
398-
router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f)))
398+
router.Use(otelgin.Middleware("foobar", otelgin.WithFilter(f)))
399399
router.GET("/healthcheck", func(c *gin.Context) {})
400400

401401
r := httptest.NewRequest("GET", "/healthcheck", nil)
@@ -411,7 +411,7 @@ func TestWithGinFilter(t *testing.T) {
411411

412412
router := gin.New()
413413
f := func(c *gin.Context) bool { return c.Request.URL.Path != "/user/:id" }
414-
router.Use(otelgin.Middleware("foobar", otelgin.WithGinFilter(f)))
414+
router.Use(otelgin.Middleware("foobar", otelgin.WithFilter(f)))
415415
router.GET("/user/:id", func(c *gin.Context) {})
416416

417417
r := httptest.NewRequest("GET", "/user/123", nil)
@@ -424,16 +424,16 @@ func TestWithGinFilter(t *testing.T) {
424424

425425
func TestMetrics(t *testing.T) {
426426
tests := []struct {
427-
name string
428-
ginMetricAttributeExtractor func(*gin.Context) []attribute.KeyValue
427+
name string
428+
metricAttributeExtractor func(*gin.Context) []attribute.KeyValue
429429
}{
430430
{
431-
name: "default",
432-
ginMetricAttributeExtractor: nil,
431+
name: "default",
432+
metricAttributeExtractor: nil,
433433
},
434434
{
435435
name: "with metric attributes callback",
436-
ginMetricAttributeExtractor: func(c *gin.Context) []attribute.KeyValue {
436+
metricAttributeExtractor: func(c *gin.Context) []attribute.KeyValue {
437437
return []attribute.KeyValue{
438438
attribute.String("key1", "value1"),
439439
attribute.String("key2", "value"),
@@ -451,7 +451,7 @@ func TestMetrics(t *testing.T) {
451451
router := gin.New()
452452
router.Use(otelgin.Middleware("foobar",
453453
otelgin.WithMeterProvider(meterProvider),
454-
otelgin.WithGinMetricAttributeFn(tt.ginMetricAttributeExtractor),
454+
otelgin.WithMetricAttributeFn(tt.metricAttributeExtractor),
455455
))
456456
router.GET("/user/:id", func(c *gin.Context) {
457457
id := c.Param("id")
@@ -483,8 +483,8 @@ func TestMetrics(t *testing.T) {
483483
attribute.String("url.scheme", "http"),
484484
}
485485

486-
if tt.ginMetricAttributeExtractor != nil {
487-
attrs = append(attrs, tt.ginMetricAttributeExtractor(c)...)
486+
if tt.metricAttributeExtractor != nil {
487+
attrs = append(attrs, tt.metricAttributeExtractor(c)...)
488488
}
489489

490490
metricdatatest.AssertEqual(t, metricdata.Metrics{

0 commit comments

Comments
 (0)