|
| 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 prometheus |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + "k8s.io/client-go/tools/leaderelection" |
| 22 | +) |
| 23 | + |
| 24 | +// Package prometheus sets the workqueue DefaultMetricsFactory to produce |
| 25 | +// prometheus metrics. To use this package, you just have to import it. |
| 26 | + |
| 27 | +func init() { |
| 28 | + leaderelection.SetProvider(prometheusMetricsProvider{}) |
| 29 | +} |
| 30 | + |
| 31 | +type prometheusMetricsProvider struct{} |
| 32 | + |
| 33 | +func (prometheusMetricsProvider) NewLeaderMetric() leaderelection.SwitchMetric { |
| 34 | + leaderGauge := prometheus.NewGaugeVec( |
| 35 | + prometheus.GaugeOpts{ |
| 36 | + Name: "leader_election_master_status", |
| 37 | + Help: "Gauge of if the reporting system is master of the relevant lease, 0 indicates backup, 1 indicates master. 'name' is the string used to identify the lease. Please make sure to group by name.", |
| 38 | + }, |
| 39 | + []string{"name"}, |
| 40 | + ) |
| 41 | + prometheus.Register(leaderGauge) |
| 42 | + return &switchAdapter{gauge: leaderGauge} |
| 43 | +} |
| 44 | + |
| 45 | +type switchAdapter struct { |
| 46 | + gauge *prometheus.GaugeVec |
| 47 | +} |
| 48 | + |
| 49 | +func (s *switchAdapter) On(name string) { |
| 50 | + s.gauge.WithLabelValues(name).Set(1.0) |
| 51 | +} |
| 52 | + |
| 53 | +func (s *switchAdapter) Off(name string) { |
| 54 | + s.gauge.WithLabelValues(name).Set(0.0) |
| 55 | +} |
0 commit comments