Skip to content

Commit 3c6d0d4

Browse files
Added option to ommit recording
1 parent 90e0407 commit 3c6d0d4

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

otelginmetrics/config.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type config struct {
1414
groupedStatus bool
1515
recorder Recorder
1616
attributes func(serverName, route string, request *http.Request) []attribute.KeyValue
17+
shouldRecord func(serverName, route string, request *http.Request) bool
1718
}
1819

1920
func defaultConfig() *config {
@@ -23,6 +24,9 @@ func defaultConfig() *config {
2324
recordSize: true,
2425
groupedStatus: true,
2526
attributes: DefaultAttributes,
27+
shouldRecord: func(_, _ string, _ *http.Request) bool {
28+
return true
29+
},
2630
}
2731
}
2832

otelginmetrics/middleware.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ func Middleware(service string, options ...Option) gin.HandlerFunc {
2828
if len(route) <= 0 {
2929
route = "nonconfigured"
3030
}
31+
if !cfg.shouldRecord(service, route, ginCtx.Request) {
32+
ginCtx.Next()
33+
return
34+
}
3135

3236
start := time.Now()
33-
3437
reqAttributes := cfg.attributes(service, route, ginCtx.Request)
3538

3639
if cfg.recordInFlight {

otelginmetrics/option.go

+10
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ func WithGroupedStatusDisabled() Option {
5757
})
5858
}
5959

60+
// WithRecorder sets a recorder for recording requests
61+
// By default the open telemetry recorder is used
6062
func WithRecorder(recorder Recorder) Option {
6163
return optionFunc(func(cfg *config) {
6264
cfg.recorder = recorder
6365
})
6466
}
67+
68+
// WithShouldRecordFunc sets a func using which whether a record should be recorded
69+
// By default the all api calls are recorded
70+
func WithShouldRecordFunc(shouldRecord func(serverName, route string, request *http.Request) bool) Option {
71+
return optionFunc(func(cfg *config) {
72+
cfg.shouldRecord = shouldRecord
73+
})
74+
}

0 commit comments

Comments
 (0)