github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/metrics/options.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/tickoalcantara12/micro/v3/metrics/options.go
    14  
    15  package metrics
    16  
    17  var (
    18  	// The Prometheus metrics will be made available on this port:
    19  	defaultPrometheusListenAddress = ":9000"
    20  	// This is the endpoint where the Prometheus metrics will be made available ("/metrics" is the default with Prometheus):
    21  	defaultPath = "/metrics"
    22  	// defaultPercentiles is the default spread of percentiles/quantiles we maintain for timings / histogram metrics:
    23  	defaultPercentiles = []float64{0, 0.5, 0.75, 0.90, 0.95, 0.98, 0.99, 1}
    24  )
    25  
    26  // Option powers the configuration for metrics implementations:
    27  type Option func(*Options)
    28  
    29  // Options for metrics implementations:
    30  type Options struct {
    31  	Address     string
    32  	DefaultTags Tags
    33  	Path        string
    34  	Percentiles []float64
    35  }
    36  
    37  // NewOptions prepares a set of options:
    38  func NewOptions(opt ...Option) Options {
    39  	opts := Options{
    40  		Address:     defaultPrometheusListenAddress,
    41  		DefaultTags: make(Tags),
    42  		Path:        defaultPath,
    43  		Percentiles: defaultPercentiles,
    44  	}
    45  
    46  	for _, o := range opt {
    47  		o(&opts)
    48  	}
    49  
    50  	return opts
    51  }
    52  
    53  // Path used to serve metrics over HTTP:
    54  func Path(value string) Option {
    55  	return func(o *Options) {
    56  		o.Path = value
    57  	}
    58  }
    59  
    60  // Address is the listen address to serve metrics on:
    61  func Address(value string) Option {
    62  	return func(o *Options) {
    63  		o.Address = value
    64  	}
    65  }
    66  
    67  // DefaultTags will be added to every metric:
    68  func DefaultTags(value Tags) Option {
    69  	return func(o *Options) {
    70  		o.DefaultTags = value
    71  	}
    72  }
    73  
    74  // Percentiles defines the desired spread of statistics for histogram / timing metrics:
    75  func Percentiles(value []float64) Option {
    76  	return func(o *Options) {
    77  		o.Percentiles = value
    78  	}
    79  }