...

Source file src/sigs.k8s.io/controller-runtime/pkg/metrics/workqueue.go

Documentation: sigs.k8s.io/controller-runtime/pkg/metrics

     1  /*
     2  Copyright 2018 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 metrics
    18  
    19  import (
    20  	"github.com/prometheus/client_golang/prometheus"
    21  	"k8s.io/client-go/util/workqueue"
    22  )
    23  
    24  // This file is copied and adapted from k8s.io/component-base/metrics/prometheus/workqueue
    25  // which registers metrics to the k8s legacy Registry. We require very
    26  // similar functionality, but must register metrics to a different Registry.
    27  
    28  // Metrics subsystem and all keys used by the workqueue.
    29  const (
    30  	WorkQueueSubsystem         = "workqueue"
    31  	DepthKey                   = "depth"
    32  	AddsKey                    = "adds_total"
    33  	QueueLatencyKey            = "queue_duration_seconds"
    34  	WorkDurationKey            = "work_duration_seconds"
    35  	UnfinishedWorkKey          = "unfinished_work_seconds"
    36  	LongestRunningProcessorKey = "longest_running_processor_seconds"
    37  	RetriesKey                 = "retries_total"
    38  )
    39  
    40  var (
    41  	depth = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    42  		Subsystem: WorkQueueSubsystem,
    43  		Name:      DepthKey,
    44  		Help:      "Current depth of workqueue",
    45  	}, []string{"name"})
    46  
    47  	adds = prometheus.NewCounterVec(prometheus.CounterOpts{
    48  		Subsystem: WorkQueueSubsystem,
    49  		Name:      AddsKey,
    50  		Help:      "Total number of adds handled by workqueue",
    51  	}, []string{"name"})
    52  
    53  	latency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
    54  		Subsystem: WorkQueueSubsystem,
    55  		Name:      QueueLatencyKey,
    56  		Help:      "How long in seconds an item stays in workqueue before being requested",
    57  		Buckets:   prometheus.ExponentialBuckets(10e-9, 10, 12),
    58  	}, []string{"name"})
    59  
    60  	workDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
    61  		Subsystem: WorkQueueSubsystem,
    62  		Name:      WorkDurationKey,
    63  		Help:      "How long in seconds processing an item from workqueue takes.",
    64  		Buckets:   prometheus.ExponentialBuckets(10e-9, 10, 12),
    65  	}, []string{"name"})
    66  
    67  	unfinished = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    68  		Subsystem: WorkQueueSubsystem,
    69  		Name:      UnfinishedWorkKey,
    70  		Help: "How many seconds of work has been done that " +
    71  			"is in progress and hasn't been observed by work_duration. Large " +
    72  			"values indicate stuck threads. One can deduce the number of stuck " +
    73  			"threads by observing the rate at which this increases.",
    74  	}, []string{"name"})
    75  
    76  	longestRunningProcessor = prometheus.NewGaugeVec(prometheus.GaugeOpts{
    77  		Subsystem: WorkQueueSubsystem,
    78  		Name:      LongestRunningProcessorKey,
    79  		Help: "How many seconds has the longest running " +
    80  			"processor for workqueue been running.",
    81  	}, []string{"name"})
    82  
    83  	retries = prometheus.NewCounterVec(prometheus.CounterOpts{
    84  		Subsystem: WorkQueueSubsystem,
    85  		Name:      RetriesKey,
    86  		Help:      "Total number of retries handled by workqueue",
    87  	}, []string{"name"})
    88  )
    89  
    90  func init() {
    91  	Registry.MustRegister(depth)
    92  	Registry.MustRegister(adds)
    93  	Registry.MustRegister(latency)
    94  	Registry.MustRegister(workDuration)
    95  	Registry.MustRegister(unfinished)
    96  	Registry.MustRegister(longestRunningProcessor)
    97  	Registry.MustRegister(retries)
    98  
    99  	workqueue.SetProvider(workqueueMetricsProvider{})
   100  }
   101  
   102  type workqueueMetricsProvider struct{}
   103  
   104  func (workqueueMetricsProvider) NewDepthMetric(name string) workqueue.GaugeMetric {
   105  	return depth.WithLabelValues(name)
   106  }
   107  
   108  func (workqueueMetricsProvider) NewAddsMetric(name string) workqueue.CounterMetric {
   109  	return adds.WithLabelValues(name)
   110  }
   111  
   112  func (workqueueMetricsProvider) NewLatencyMetric(name string) workqueue.HistogramMetric {
   113  	return latency.WithLabelValues(name)
   114  }
   115  
   116  func (workqueueMetricsProvider) NewWorkDurationMetric(name string) workqueue.HistogramMetric {
   117  	return workDuration.WithLabelValues(name)
   118  }
   119  
   120  func (workqueueMetricsProvider) NewUnfinishedWorkSecondsMetric(name string) workqueue.SettableGaugeMetric {
   121  	return unfinished.WithLabelValues(name)
   122  }
   123  
   124  func (workqueueMetricsProvider) NewLongestRunningProcessorSecondsMetric(name string) workqueue.SettableGaugeMetric {
   125  	return longestRunningProcessor.WithLabelValues(name)
   126  }
   127  
   128  func (workqueueMetricsProvider) NewRetriesMetric(name string) workqueue.CounterMetric {
   129  	return retries.WithLabelValues(name)
   130  }
   131  

View as plain text