github.com/enmand/kubernetes@v1.2.0-alpha.0/pkg/tools/metrics/metrics.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     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 metrics
    18  
    19  import (
    20  	"sync"
    21  	"time"
    22  
    23  	"github.com/prometheus/client_golang/prometheus"
    24  )
    25  
    26  var (
    27  	cacheHitCounter = prometheus.NewCounter(
    28  		prometheus.CounterOpts{
    29  			Name: "etcd_helper_cache_hit_count",
    30  			Help: "Counter of etcd helper cache hits.",
    31  		},
    32  	)
    33  	cacheMissCounter = prometheus.NewCounter(
    34  		prometheus.CounterOpts{
    35  			Name: "etcd_helper_cache_miss_count",
    36  			Help: "Counter of etcd helper cache miss.",
    37  		},
    38  	)
    39  	cacheEntryCounter = prometheus.NewCounter(
    40  		prometheus.CounterOpts{
    41  			Name: "etcd_helper_cache_entry_count",
    42  			Help: "Counter of etcd helper cache entries. This can be different from etcd_helper_cache_miss_count " +
    43  				"because two concurrent threads can miss the cache and generate the same entry twice.",
    44  		},
    45  	)
    46  	cacheGetLatency = prometheus.NewSummary(
    47  		prometheus.SummaryOpts{
    48  			Name: "etcd_request_cache_get_latencies_summary",
    49  			Help: "Latency in microseconds of getting an object from etcd cache",
    50  		},
    51  	)
    52  	cacheAddLatency = prometheus.NewSummary(
    53  		prometheus.SummaryOpts{
    54  			Name: "etcd_request_cache_add_latencies_summary",
    55  			Help: "Latency in microseconds of adding an object to etcd cache",
    56  		},
    57  	)
    58  	etcdRequestLatenciesSummary = prometheus.NewSummaryVec(
    59  		prometheus.SummaryOpts{
    60  			Name: "etcd_request_latencies_summary",
    61  			Help: "Etcd request latency summary in microseconds for each operation and object type.",
    62  		},
    63  		[]string{"operation", "type"},
    64  	)
    65  )
    66  
    67  var registerMetrics sync.Once
    68  
    69  // Register all metrics.
    70  func Register() {
    71  	// Register the metrics.
    72  	registerMetrics.Do(func() {
    73  		prometheus.MustRegister(cacheHitCounter)
    74  		prometheus.MustRegister(cacheMissCounter)
    75  		prometheus.MustRegister(cacheEntryCounter)
    76  		prometheus.MustRegister(cacheAddLatency)
    77  		prometheus.MustRegister(cacheGetLatency)
    78  		prometheus.MustRegister(etcdRequestLatenciesSummary)
    79  	})
    80  }
    81  
    82  func RecordEtcdRequestLatency(verb, resource string, startTime time.Time) {
    83  	etcdRequestLatenciesSummary.WithLabelValues(verb, resource).Observe(float64(time.Since(startTime) / time.Microsecond))
    84  }
    85  
    86  func ObserveGetCache(startTime time.Time) {
    87  	cacheGetLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
    88  }
    89  
    90  func ObserveAddCache(startTime time.Time) {
    91  	cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
    92  }
    93  
    94  func ObserveCacheHit() {
    95  	cacheHitCounter.Inc()
    96  }
    97  
    98  func ObserveCacheMiss() {
    99  	cacheMissCounter.Inc()
   100  }
   101  
   102  func ObserveNewEntry() {
   103  	cacheEntryCounter.Inc()
   104  }