Skip to content

Commit dc94339

Browse files
tranngoclamLam Tran
authored andcommitted
add new func to avoid breaking changes
1 parent 38af467 commit dc94339

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1515
### Changed
1616

1717
- Jaeger remote sampler's probabilistic strategy now uses the same sampling algorithm as `trace.TraceIDRatioBased` in `go.opentelemetry.io/contrib/samplers/jaegerremote`. (#6892)
18-
- otelgin: use gin.Context in MetricAttributeFn (#6932)
18+
- otelgin: add GinMetricAttributeFn (#6932)
1919

2020
### Removed
2121

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@ import (
1717
)
1818

1919
type config struct {
20-
TracerProvider oteltrace.TracerProvider
21-
Propagators propagation.TextMapPropagator
22-
Filters []Filter
23-
GinFilters []GinFilter
24-
SpanNameFormatter SpanNameFormatter
25-
MeterProvider metric.MeterProvider
26-
MetricAttributeFn MetricAttributeFn
20+
TracerProvider oteltrace.TracerProvider
21+
Propagators propagation.TextMapPropagator
22+
Filters []Filter
23+
GinFilters []GinFilter
24+
SpanNameFormatter SpanNameFormatter
25+
MeterProvider metric.MeterProvider
26+
MetricAttributeFn MetricAttributeFn
27+
GinMetricAttributeFn GinMetricAttributeFn
2728
}
2829

2930
// Filter is a predicate used to determine whether a given http.request should
@@ -37,9 +38,13 @@ type GinFilter func(*gin.Context) bool
3738
// SpanNameFormatter is used to set span name by http.request.
3839
type SpanNameFormatter func(r *http.Request) string
3940

40-
// MetricAttributeFn is used to extract additional attributes from the gin.Context
41+
// MetricAttributeFn is used to extract additional attributes from the http.Request
4142
// and return them as a slice of attribute.KeyValue.
42-
type MetricAttributeFn func(c *gin.Context) []attribute.KeyValue
43+
type MetricAttributeFn func(*http.Request) []attribute.KeyValue
44+
45+
// GinMetricAttributeFn is used to extract additional attributes from the gin.Context
46+
// and return them as a slice of attribute.KeyValue.
47+
type GinMetricAttributeFn func(*gin.Context) []attribute.KeyValue
4348

4449
// Option specifies instrumentation configuration options.
4550
type Option interface {
@@ -115,3 +120,11 @@ func WithMetricAttributeFn(f MetricAttributeFn) Option {
115120
c.MetricAttributeFn = f
116121
})
117122
}
123+
124+
// WithGinMetricAttributeFn specifies a function that extracts additional attributes from the gin.Context
125+
// and returns them as a slice of attribute.KeyValue.
126+
func WithGinMetricAttributeFn(f GinMetricAttributeFn) Option {
127+
return optionFunc(func(c *config) {
128+
c.GinMetricAttributeFn = f
129+
})
130+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ func Middleware(service string, opts ...Option) gin.HandlerFunc {
118118
// Record the server-side attributes.
119119
var additionalAttributes []attribute.KeyValue
120120
if cfg.MetricAttributeFn != nil {
121-
additionalAttributes = cfg.MetricAttributeFn(c)
121+
additionalAttributes = append(additionalAttributes, cfg.MetricAttributeFn(c.Request)...)
122+
}
123+
if cfg.GinMetricAttributeFn != nil {
124+
additionalAttributes = append(additionalAttributes, cfg.GinMetricAttributeFn(c)...)
122125
}
123126

124127
sc.RecordMetrics(ctx, semconv.ServerMetricData{

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

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -439,17 +439,30 @@ func TestWithGinFilter(t *testing.T) {
439439

440440
func TestMetrics(t *testing.T) {
441441
tests := []struct {
442-
name string
443-
metricAttributeExtractor func(*gin.Context) []attribute.KeyValue
442+
name string
443+
metricAttributeExtractor func(*http.Request) []attribute.KeyValue
444+
ginMetricAttributeExtractor func(*gin.Context) []attribute.KeyValue
444445
}{
445-
{"default", nil},
446-
{"with metric attributes callback", func(c *gin.Context) []attribute.KeyValue {
447-
return []attribute.KeyValue{
448-
attribute.String("key1", "value1"),
449-
attribute.String("key2", "value"),
450-
attribute.String("method", strings.ToUpper(c.Request.Method)),
451-
}
452-
}},
446+
{
447+
name: "default",
448+
metricAttributeExtractor: nil,
449+
ginMetricAttributeExtractor: nil,
450+
},
451+
{
452+
name: "with metric attributes callback",
453+
metricAttributeExtractor: func(r *http.Request) []attribute.KeyValue {
454+
return []attribute.KeyValue{
455+
attribute.String("key1", "value1"),
456+
attribute.String("key2", "value"),
457+
attribute.String("method", strings.ToUpper(r.Method)),
458+
}
459+
},
460+
ginMetricAttributeExtractor: func(c *gin.Context) []attribute.KeyValue {
461+
return []attribute.KeyValue{
462+
attribute.String("key3", "value3"),
463+
}
464+
},
465+
},
453466
}
454467

455468
for _, tt := range tests {
@@ -461,6 +474,7 @@ func TestMetrics(t *testing.T) {
461474
router.Use(otelgin.Middleware("foobar",
462475
otelgin.WithMeterProvider(meterProvider),
463476
otelgin.WithMetricAttributeFn(tt.metricAttributeExtractor),
477+
otelgin.WithGinMetricAttributeFn(tt.ginMetricAttributeExtractor),
464478
))
465479
router.GET("/user/:id", func(c *gin.Context) {
466480
id := c.Param("id")
@@ -493,7 +507,10 @@ func TestMetrics(t *testing.T) {
493507
}
494508

495509
if tt.metricAttributeExtractor != nil {
496-
attrs = append(attrs, tt.metricAttributeExtractor(c)...)
510+
attrs = append(attrs, tt.metricAttributeExtractor(c.Request)...)
511+
}
512+
if tt.ginMetricAttributeExtractor != nil {
513+
attrs = append(attrs, tt.ginMetricAttributeExtractor(c)...)
497514
}
498515

499516
metricdatatest.AssertEqual(t, metricdata.Metrics{

0 commit comments

Comments
 (0)