github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/pkg/flagutil/instrumentation.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package flagutil
    18  
    19  import (
    20  	"flag"
    21  	"time"
    22  )
    23  
    24  const (
    25  	DefaultMetricsPort = 9090
    26  	DefaultPProfPort   = 6060
    27  	DefaultHealthPort  = 8081
    28  
    29  	DefaultMemoryProfileInterval = 30 * time.Second
    30  )
    31  
    32  // InstrumentationOptions holds common options which are used across Prow components
    33  type InstrumentationOptions struct {
    34  	// MetricsPort is the port which is used to serve metrics
    35  	MetricsPort int
    36  	// PProfPort is the port which is used to serve pprof
    37  	PProfPort int
    38  	// HealthPort is the port which is used to serve liveness and readiness
    39  	HealthPort int
    40  
    41  	// ProfileMemory determines if the process should profile memory
    42  	ProfileMemory bool
    43  	// MemoryProfileInterval is the interval at which memory profiles should be dumped
    44  	MemoryProfileInterval time.Duration
    45  }
    46  
    47  // DefaultInstrumentationOptions returns an initialized options struct, mostly for use in tests.
    48  func DefaultInstrumentationOptions() InstrumentationOptions {
    49  	return InstrumentationOptions{
    50  		MetricsPort:           DefaultMetricsPort,
    51  		PProfPort:             DefaultPProfPort,
    52  		HealthPort:            DefaultHealthPort,
    53  		ProfileMemory:         false,
    54  		MemoryProfileInterval: DefaultMemoryProfileInterval,
    55  	}
    56  }
    57  
    58  // AddFlags injects common options into the given FlagSet.
    59  func (o *InstrumentationOptions) AddFlags(fs *flag.FlagSet) {
    60  	fs.IntVar(&o.MetricsPort, "metrics-port", DefaultMetricsPort, "port to serve metrics")
    61  	fs.IntVar(&o.PProfPort, "pprof-port", DefaultPProfPort, "port to serve pprof")
    62  	fs.IntVar(&o.HealthPort, "health-port", DefaultHealthPort, "port to serve liveness and readiness")
    63  	fs.BoolVar(&o.ProfileMemory, "profile-memory-usage", false, "profile memory usage for analysis")
    64  	fs.DurationVar(&o.MemoryProfileInterval, "memory-profile-interval", DefaultMemoryProfileInterval, "duration at which memory profiles should be dumped")
    65  }
    66  
    67  func (o *InstrumentationOptions) Validate(_ bool) error {
    68  	return nil
    69  }