From 46a2e067a4e67095ef121c7975db0180806cc647 Mon Sep 17 00:00:00 2001 From: zhouhaibing089 Date: Sun, 31 Mar 2024 16:44:43 -0700 Subject: [PATCH] webhook: add StatsReporterOptions in webhook.Options There are two ways to customize StatsReporter: 1. Use a whole new StatsReporter implementation. 1. Or pass Option funcs to customize the default StatsReporter. Option 1 is less practical at this time due to the metrics registration conflict. `webhook.RegisterMetrics()` is called regardless which StatsReporter implementation is used (which is a problem by itself). The second option is more practical since it works well without dealing with metrics conflicts. The `webhook.Option` in particular allows people to discard certain metrics tags. --- injection/sharedmain/main.go | 7 ++++++- webhook/webhook.go | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/injection/sharedmain/main.go b/injection/sharedmain/main.go index 25562e965e..79c42c2014 100644 --- a/injection/sharedmain/main.go +++ b/injection/sharedmain/main.go @@ -290,7 +290,12 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto var wh *webhook.Webhook if len(webhooks) > 0 { // Register webhook metrics - webhook.RegisterMetrics() + opts := webhook.GetOptions(ctx) + if opts != nil { + webhook.RegisterMetrics(opts.StatsReporterOptions...) + } else { + webhook.RegisterMetrics() + } wh, err = webhook.New(ctx, webhooks) if err != nil { diff --git a/webhook/webhook.go b/webhook/webhook.go index eff693e80d..d8842df35a 100644 --- a/webhook/webhook.go +++ b/webhook/webhook.go @@ -70,6 +70,9 @@ type Options struct { // only a single port for the service. Port int + // StatsReporterOptions are the options used to initialize the default StatsReporter + StatsReporterOptions []StatsReporterOption + // StatsReporter reports metrics about the webhook. // This will be automatically initialized by the constructor if left uninitialized. StatsReporter StatsReporter @@ -144,7 +147,7 @@ func New( logger := logging.FromContext(ctx) if opts.StatsReporter == nil { - reporter, err := NewStatsReporter() + reporter, err := NewStatsReporter(opts.StatsReporterOptions...) if err != nil { return nil, err }