github.com/kaydxh/golang@v0.0.131/pkg/monitor/prometheus/metric.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package prometheus
    23  
    24  import (
    25  	"github.com/prometheus/client_golang/prometheus"
    26  )
    27  
    28  // M is the Metrics instance
    29  var M = newMetrics()
    30  
    31  const (
    32  	MetircLabelMethod      = "method"
    33  	MetircLabelClientIP    = "client_ip"
    34  	MetircLabelCodeMessage = "code_message"
    35  )
    36  
    37  type Metrics struct {
    38  	CalledTotal           *prometheus.CounterVec
    39  	DurationCostHistogram *prometheus.HistogramVec
    40  	DurationCost          *prometheus.SummaryVec
    41  }
    42  
    43  func newMetrics() *Metrics {
    44  
    45  	return &Metrics{
    46  		CalledTotal: newCounterVec(
    47  			"http_method_called_total",
    48  			"the total number of method called.",
    49  			MetircLabelMethod,
    50  			MetircLabelClientIP,
    51  			MetircLabelCodeMessage,
    52  		),
    53  
    54  		DurationCost: newSummaryVec(
    55  			"http_method_cost_duration_milliseconds",
    56  			"the duration of method cost in milliseconds.",
    57  			MetircLabelMethod,
    58  		),
    59  	}
    60  }
    61  
    62  //var (
    63  /*
    64  	cost = promauto.NewGaugeVec(
    65  		prometheus.GaugeOpts{
    66  			Name: "method_cost_duration_milliseconds",
    67  			Help: "the duration of method now cost in milliseconds.",
    68  		},
    69  		[]string{MetircLabelMethod},
    70  	)
    71  */
    72  
    73  /*
    74  	durationCostHistogram = prometheus.NewHistogramVec(
    75  		prometheus.HistogramOpts{
    76  			Name: "method_cost_duration_histogram_milliseconds",
    77  			Help: "Histogram of request duration",
    78  			Buckets: []float64{
    79  				.001,
    80  				.002,
    81  				.004,
    82  				.006,
    83  				.008,
    84  				.01,
    85  				.02,
    86  				.04,
    87  				.06,
    88  				.08,
    89  				.1,
    90  				.2,
    91  				.4,
    92  				.6,
    93  				.8,
    94  				1,
    95  			},
    96  		},
    97  		[]string{MetircLabelMethod},
    98  	)
    99  */
   100  
   101  //)
   102  
   103  func newCounterVec(name, help string, labels ...string) *prometheus.CounterVec {
   104  	vec := prometheus.NewCounterVec(
   105  		prometheus.CounterOpts{
   106  			Name: name,
   107  			Help: help,
   108  		}, labels)
   109  	prometheus.MustRegister(vec)
   110  	return vec
   111  }
   112  
   113  func newGaugeVec(name, help string, labels ...string) *prometheus.GaugeVec {
   114  	vec := prometheus.NewGaugeVec(
   115  		prometheus.GaugeOpts{
   116  			Name: name,
   117  			Help: help,
   118  		}, labels)
   119  	prometheus.MustRegister(vec)
   120  	return vec
   121  }
   122  
   123  func newSummaryVec(name, help string, labels ...string) *prometheus.SummaryVec {
   124  	vec := prometheus.NewSummaryVec(
   125  		prometheus.SummaryOpts{
   126  			Name:       name,
   127  			Help:       help,
   128  			Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
   129  		}, labels)
   130  	prometheus.MustRegister(vec)
   131  	return vec
   132  }