Skip to content

Commit

Permalink
webhook: add StatsReporterOptions in webhook.Options
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zhouhaibing089 committed Mar 31, 2024
1 parent 137fd72 commit 927e9ff
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion injection/sharedmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ 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)
webhook.RegisterMetrics(opts.StatsReporterOptions...)

wh, err = webhook.New(ctx, webhooks)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 927e9ff

Please sign in to comment.