Skip to content

Commit

Permalink
webhook: register metrics only with default stats reporter
Browse files Browse the repository at this point in the history
This is a followup from [knative#2931][1]. RegisterMetrics may
register unwanted metrics if the default stats reporter is not used.

[1]: knative#2931
  • Loading branch information
zhouhaibing089 committed Apr 2, 2024
1 parent 03bf3de commit 220f45b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
8 changes: 0 additions & 8 deletions injection/sharedmain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,6 @@ func MainWithConfig(ctx context.Context, component string, cfg *rest.Config, cto
// and pass them in.
var wh *webhook.Webhook
if len(webhooks) > 0 {
// Register webhook metrics
opts := webhook.GetOptions(ctx)
if opts != nil {
webhook.RegisterMetrics(opts.StatsReporterOptions...)
} else {
webhook.RegisterMetrics()
}

wh, err = webhook.New(ctx, webhooks)
if err != nil {
logger.Fatalw("Failed to create webhook", zap.Error(err))
Expand Down
7 changes: 7 additions & 0 deletions webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"log"
"net"
"net/http"
"sync"
"time"

// Injection stuff
Expand All @@ -40,6 +41,10 @@ import (
"knative.dev/pkg/system"
)

var (
metricsOnce sync.Once
)

// Options contains the configuration for the webhook
type Options struct {
// TLSMinVersion contains the minimum TLS version that is acceptable to communicate with the API server.
Expand Down Expand Up @@ -147,6 +152,8 @@ func New(
logger := logging.FromContext(ctx)

if opts.StatsReporter == nil {
// Register webhook metrics
metricsOnce.Do(func() { RegisterMetrics(opts.StatsReporterOptions...) })
reporter, err := NewStatsReporter(opts.StatsReporterOptions...)
if err != nil {
return nil, err
Expand Down

0 comments on commit 220f45b

Please sign in to comment.