|
| 1 | +/* |
| 2 | +Copyright 2019 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 problemmetrics |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "fmt" |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/golang/glog" |
| 25 | + |
| 26 | + "k8s.io/node-problem-detector/pkg/util/metrics" |
| 27 | +) |
| 28 | + |
| 29 | +// GlobalProblemMetricsManager is a singleton of ProblemMetricsManager, |
| 30 | +// which should be used to manage all problem-converted metrics across all |
| 31 | +// problem daemons. |
| 32 | +var GlobalProblemMetricsManager *ProblemMetricsManager |
| 33 | + |
| 34 | +func init() { |
| 35 | + GlobalProblemMetricsManager = NewProblemMetricsManagerOrDie() |
| 36 | +} |
| 37 | + |
| 38 | +// ProblemMetricsManager manages problem-converted metrics. |
| 39 | +// ProblemMetricsManager is thread-safe. |
| 40 | +type ProblemMetricsManager struct { |
| 41 | + problemCounter metrics.Int64MetricInterface |
| 42 | + problemGauge metrics.Int64MetricInterface |
| 43 | + problemTypeToReason map[string]string |
| 44 | + problemTypeToReasonMutex sync.Mutex |
| 45 | +} |
| 46 | + |
| 47 | +func NewProblemMetricsManagerOrDie() *ProblemMetricsManager { |
| 48 | + pmm := ProblemMetricsManager{} |
| 49 | + |
| 50 | + var err error |
| 51 | + pmm.problemCounter, err = metrics.NewInt64Metric( |
| 52 | + "problem_counter", |
| 53 | + "Number of times a specific type of problem have occurred.", |
| 54 | + "1", |
| 55 | + metrics.Sum, |
| 56 | + []string{"reason"}) |
| 57 | + if err != nil { |
| 58 | + glog.Fatalf("Failed to create problem_counter metric: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + pmm.problemGauge, err = metrics.NewInt64Metric( |
| 62 | + "problem_gauge", |
| 63 | + "Whether a specific type of problem is affecting the node or not.", |
| 64 | + "1", |
| 65 | + metrics.LastValue, |
| 66 | + []string{"type", "reason"}) |
| 67 | + if err != nil { |
| 68 | + glog.Fatalf("Failed to create problem_gauge metric: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + pmm.problemTypeToReason = make(map[string]string) |
| 72 | + |
| 73 | + return &pmm |
| 74 | +} |
| 75 | + |
| 76 | +// IncrementProblemCounter increments the value of a problem counter. |
| 77 | +func (pmm *ProblemMetricsManager) IncrementProblemCounter(reason string, count int64) error { |
| 78 | + if pmm.problemCounter == nil { |
| 79 | + return errors.New("problem counter is being incremented before initialized.") |
| 80 | + } |
| 81 | + |
| 82 | + return pmm.problemCounter.Record(map[string]string{"reason": reason}, count) |
| 83 | +} |
| 84 | + |
| 85 | +// SetProblemGauge sets the value of a problem gauge. |
| 86 | +func (pmm *ProblemMetricsManager) SetProblemGauge(problemType string, reason string, value bool) error { |
| 87 | + if pmm.problemGauge == nil { |
| 88 | + return errors.New("problem gauge is being set before initialized.") |
| 89 | + } |
| 90 | + |
| 91 | + pmm.problemTypeToReasonMutex.Lock() |
| 92 | + defer pmm.problemTypeToReasonMutex.Unlock() |
| 93 | + |
| 94 | + // We clear the last reason, because the expected behavior is that at any point of time, |
| 95 | + // for each type of permanent problem, there should be at most one reason got set to 1. |
| 96 | + // This behavior is consistent with the behavior of node condition in Kubernetes. |
| 97 | + // However, problemGauges with different "type" and "reason" are considered as different |
| 98 | + // metrics in Prometheus. So we need to clear the previous metrics explicitly. |
| 99 | + if lastReason, ok := pmm.problemTypeToReason[problemType]; ok { |
| 100 | + err := pmm.problemGauge.Record(map[string]string{"type": problemType, "reason": lastReason}, 0) |
| 101 | + if err != nil { |
| 102 | + return fmt.Errorf("failed to clear previous reason %q for type %q: %v", |
| 103 | + problemType, lastReason, err) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + pmm.problemTypeToReason[problemType] = reason |
| 108 | + |
| 109 | + var valueInt int64 |
| 110 | + if value { |
| 111 | + valueInt = 1 |
| 112 | + } |
| 113 | + return pmm.problemGauge.Record(map[string]string{"type": problemType, "reason": reason}, valueInt) |
| 114 | +} |
0 commit comments