github.com/nginxinc/kubernetes-ingress@v1.12.5/internal/metrics/collectors/manager.go (about)

     1  package collectors
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/prometheus/client_golang/prometheus"
     7  )
     8  
     9  // ManagerCollector is an interface for the metrics of the Nginx Manager
    10  type ManagerCollector interface {
    11  	IncNginxReloadCount(isEndPointUpdate bool)
    12  	IncNginxReloadErrors()
    13  	UpdateLastReloadTime(ms time.Duration)
    14  	Register(registry *prometheus.Registry) error
    15  }
    16  
    17  // LocalManagerMetricsCollector implements NginxManagerCollector interface and prometheus.Collector interface
    18  type LocalManagerMetricsCollector struct {
    19  	// Metrics
    20  	reloadsTotal     *prometheus.CounterVec
    21  	reloadsError     prometheus.Counter
    22  	lastReloadStatus prometheus.Gauge
    23  	lastReloadTime   prometheus.Gauge
    24  }
    25  
    26  // NewLocalManagerMetricsCollector creates a new LocalManagerMetricsCollector
    27  func NewLocalManagerMetricsCollector(constLabels map[string]string) *LocalManagerMetricsCollector {
    28  	nc := &LocalManagerMetricsCollector{
    29  		reloadsTotal: prometheus.NewCounterVec(
    30  			prometheus.CounterOpts{
    31  				Name:        "nginx_reloads_total",
    32  				Namespace:   metricsNamespace,
    33  				Help:        "Number of successful NGINX reloads",
    34  				ConstLabels: constLabels,
    35  			},
    36  			[]string{"reason"},
    37  		),
    38  		reloadsError: prometheus.NewCounter(
    39  			prometheus.CounterOpts{
    40  				Name:        "nginx_reload_errors_total",
    41  				Namespace:   metricsNamespace,
    42  				Help:        "Number of unsuccessful NGINX reloads",
    43  				ConstLabels: constLabels,
    44  			},
    45  		),
    46  		lastReloadStatus: prometheus.NewGauge(
    47  			prometheus.GaugeOpts{
    48  				Name:        "nginx_last_reload_status",
    49  				Namespace:   metricsNamespace,
    50  				Help:        "Status of the last NGINX reload",
    51  				ConstLabels: constLabels,
    52  			},
    53  		),
    54  		lastReloadTime: prometheus.NewGauge(
    55  			prometheus.GaugeOpts{
    56  				Name:        "nginx_last_reload_milliseconds",
    57  				Namespace:   metricsNamespace,
    58  				Help:        "Duration in milliseconds of the last NGINX reload",
    59  				ConstLabels: constLabels,
    60  			},
    61  		),
    62  	}
    63  	nc.reloadsTotal.WithLabelValues("other")
    64  	nc.reloadsTotal.WithLabelValues("endpoints")
    65  	return nc
    66  }
    67  
    68  // IncNginxReloadCount increments the counter of successful NGINX reloads and sets the last reload status to true
    69  func (nc *LocalManagerMetricsCollector) IncNginxReloadCount(isEndPointUpdate bool) {
    70  	var label string
    71  	if isEndPointUpdate {
    72  		label = "endpoints"
    73  	} else {
    74  		label = "other"
    75  	}
    76  	nc.reloadsTotal.WithLabelValues(label).Inc()
    77  	nc.updateLastReloadStatus(true)
    78  }
    79  
    80  // IncNginxReloadErrors increments the counter of NGINX reload errors and sets the last reload status to false
    81  func (nc *LocalManagerMetricsCollector) IncNginxReloadErrors() {
    82  	nc.reloadsError.Inc()
    83  	nc.updateLastReloadStatus(false)
    84  }
    85  
    86  // updateLastReloadStatus updates the last NGINX reload status metric
    87  func (nc *LocalManagerMetricsCollector) updateLastReloadStatus(up bool) {
    88  	var status float64
    89  	if up {
    90  		status = 1.0
    91  	}
    92  	nc.lastReloadStatus.Set(status)
    93  }
    94  
    95  // UpdateLastReloadTime updates the last NGINX reload time
    96  func (nc *LocalManagerMetricsCollector) UpdateLastReloadTime(duration time.Duration) {
    97  	nc.lastReloadTime.Set(float64(duration / time.Millisecond))
    98  }
    99  
   100  // Describe implements prometheus.Collector interface Describe method
   101  func (nc *LocalManagerMetricsCollector) Describe(ch chan<- *prometheus.Desc) {
   102  	nc.reloadsTotal.Describe(ch)
   103  	nc.reloadsError.Describe(ch)
   104  	nc.lastReloadStatus.Describe(ch)
   105  	nc.lastReloadTime.Describe(ch)
   106  }
   107  
   108  // Collect implements the prometheus.Collector interface Collect method
   109  func (nc *LocalManagerMetricsCollector) Collect(ch chan<- prometheus.Metric) {
   110  	nc.reloadsTotal.Collect(ch)
   111  	nc.reloadsError.Collect(ch)
   112  	nc.lastReloadStatus.Collect(ch)
   113  	nc.lastReloadTime.Collect(ch)
   114  }
   115  
   116  // Register registers all the metrics of the collector
   117  func (nc *LocalManagerMetricsCollector) Register(registry *prometheus.Registry) error {
   118  	return registry.Register(nc)
   119  }
   120  
   121  // ManagerFakeCollector is a fake collector that will implement ManagerCollector interface
   122  type ManagerFakeCollector struct{}
   123  
   124  // NewManagerFakeCollector creates a fake collector that implements ManagerCollector interface
   125  func NewManagerFakeCollector() *ManagerFakeCollector {
   126  	return &ManagerFakeCollector{}
   127  }
   128  
   129  // Register implements a fake Register
   130  func (nc *ManagerFakeCollector) Register(registry *prometheus.Registry) error { return nil }
   131  
   132  // IncNginxReloadCount implements a fake IncNginxReloadCount
   133  func (nc *ManagerFakeCollector) IncNginxReloadCount(isEndPointUpdate bool) {}
   134  
   135  // IncNginxReloadErrors implements a fake IncNginxReloadErrors
   136  func (nc *ManagerFakeCollector) IncNginxReloadErrors() {}
   137  
   138  // UpdateLastReloadTime implements a fake UpdateLastReloadTime
   139  func (nc *ManagerFakeCollector) UpdateLastReloadTime(ms time.Duration) {}