From 3c6d0d44331ca46a57f29495f249ec4b65f0f410 Mon Sep 17 00:00:00 2001 From: Bharat <96172898+bharat-gadde-dev@users.noreply.github.com> Date: Tue, 29 Mar 2022 19:12:49 +0530 Subject: [PATCH] Added option to ommit recording --- otelginmetrics/config.go | 4 ++++ otelginmetrics/middleware.go | 5 ++++- otelginmetrics/option.go | 10 ++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/otelginmetrics/config.go b/otelginmetrics/config.go index 2ef4f22..f33fe38 100644 --- a/otelginmetrics/config.go +++ b/otelginmetrics/config.go @@ -14,6 +14,7 @@ type config struct { groupedStatus bool recorder Recorder attributes func(serverName, route string, request *http.Request) []attribute.KeyValue + shouldRecord func(serverName, route string, request *http.Request) bool } func defaultConfig() *config { @@ -23,6 +24,9 @@ func defaultConfig() *config { recordSize: true, groupedStatus: true, attributes: DefaultAttributes, + shouldRecord: func(_, _ string, _ *http.Request) bool { + return true + }, } } diff --git a/otelginmetrics/middleware.go b/otelginmetrics/middleware.go index 6e61862..85479c7 100644 --- a/otelginmetrics/middleware.go +++ b/otelginmetrics/middleware.go @@ -28,9 +28,12 @@ func Middleware(service string, options ...Option) gin.HandlerFunc { if len(route) <= 0 { route = "nonconfigured" } + if !cfg.shouldRecord(service, route, ginCtx.Request) { + ginCtx.Next() + return + } start := time.Now() - reqAttributes := cfg.attributes(service, route, ginCtx.Request) if cfg.recordInFlight { diff --git a/otelginmetrics/option.go b/otelginmetrics/option.go index 6ba33e4..ec3d485 100644 --- a/otelginmetrics/option.go +++ b/otelginmetrics/option.go @@ -57,8 +57,18 @@ func WithGroupedStatusDisabled() Option { }) } +// WithRecorder sets a recorder for recording requests +// By default the open telemetry recorder is used func WithRecorder(recorder Recorder) Option { return optionFunc(func(cfg *config) { cfg.recorder = recorder }) } + +// WithShouldRecordFunc sets a func using which whether a record should be recorded +// By default the all api calls are recorded +func WithShouldRecordFunc(shouldRecord func(serverName, route string, request *http.Request) bool) Option { + return optionFunc(func(cfg *config) { + cfg.shouldRecord = shouldRecord + }) +}