|
| 1 | +// Copyright 2024 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package metrics |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/prometheus/client_golang/prometheus" |
| 21 | + dto "github.com/prometheus/client_model/go" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 25 | + |
| 26 | + "github.com/pingcap/tidb-operator/api/v2/core/v1alpha1" |
| 27 | +) |
| 28 | + |
| 29 | +func newTiKVForMetricTest(name string) *v1alpha1.TiKV { |
| 30 | + return &v1alpha1.TiKV{ |
| 31 | + ObjectMeta: metav1.ObjectMeta{ |
| 32 | + Namespace: "test-ns", |
| 33 | + Name: name, |
| 34 | + Labels: map[string]string{ |
| 35 | + v1alpha1.LabelKeyCluster: "test-cluster", |
| 36 | + v1alpha1.LabelKeyComponent: "tikv", |
| 37 | + v1alpha1.LabelKeyGroup: "test-group", |
| 38 | + }, |
| 39 | + }, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// abnormalGaugeValue reads the AbnormalInstance gauge for the given instance |
| 44 | +// + condition. Returns (value, present). present is false when the series has |
| 45 | +// not been touched (collector returns the default zero metric without a Gauge |
| 46 | +// payload set). |
| 47 | +func abnormalGaugeValue(t *testing.T, instance, condition string) (float64, bool) { |
| 48 | + t.Helper() |
| 49 | + g, err := AbnormalInstance.GetMetricWithLabelValues( |
| 50 | + "test-ns", "test-cluster", "tikv", "test-group", instance, condition, |
| 51 | + ) |
| 52 | + require.NoError(t, err) |
| 53 | + m := &dto.Metric{} |
| 54 | + require.NoError(t, g.Write(m)) |
| 55 | + if m.Gauge == nil { |
| 56 | + return 0, false |
| 57 | + } |
| 58 | + return m.Gauge.GetValue(), true |
| 59 | +} |
| 60 | + |
| 61 | +func gaugeSeriesExists(t *testing.T, instance, condition string) bool { |
| 62 | + t.Helper() |
| 63 | + ch := make(chan prometheus.Metric, 16) |
| 64 | + AbnormalInstance.Collect(ch) |
| 65 | + close(ch) |
| 66 | + want := []string{"test-ns", "test-cluster", "tikv", "test-group", instance, condition} |
| 67 | + for m := range ch { |
| 68 | + dm := &dto.Metric{} |
| 69 | + if err := m.Write(dm); err != nil { |
| 70 | + continue |
| 71 | + } |
| 72 | + if labelsEqual(dm.GetLabel(), want) { |
| 73 | + return true |
| 74 | + } |
| 75 | + } |
| 76 | + return false |
| 77 | +} |
| 78 | + |
| 79 | +func labelsEqual(got []*dto.LabelPair, wantValues []string) bool { |
| 80 | + if len(got) != len(InstanceAbnormalMetricLabels) { |
| 81 | + return false |
| 82 | + } |
| 83 | + want := map[string]string{} |
| 84 | + for i, k := range InstanceAbnormalMetricLabels { |
| 85 | + want[k] = wantValues[i] |
| 86 | + } |
| 87 | + for _, p := range got { |
| 88 | + if want[p.GetName()] != p.GetValue() { |
| 89 | + return false |
| 90 | + } |
| 91 | + } |
| 92 | + return true |
| 93 | +} |
| 94 | + |
| 95 | +func TestObserveCondition(t *testing.T) { |
| 96 | + cases := []struct { |
| 97 | + name string |
| 98 | + condType string |
| 99 | + status metav1.ConditionStatus |
| 100 | + omitCond bool |
| 101 | + wantValue float64 |
| 102 | + }{ |
| 103 | + {name: "Synced=False sets 1", condType: v1alpha1.CondSynced, status: metav1.ConditionFalse, wantValue: 1}, |
| 104 | + {name: "Synced=True sets 0", condType: v1alpha1.CondSynced, status: metav1.ConditionTrue, wantValue: 0}, |
| 105 | + {name: "Synced absent treated as healthy", condType: v1alpha1.CondSynced, omitCond: true, wantValue: 0}, |
| 106 | + {name: "Ready=False sets 1", condType: v1alpha1.CondReady, status: metav1.ConditionFalse, wantValue: 1}, |
| 107 | + {name: "Ready=True sets 0", condType: v1alpha1.CondReady, status: metav1.ConditionTrue, wantValue: 0}, |
| 108 | + } |
| 109 | + |
| 110 | + for _, tc := range cases { |
| 111 | + t.Run(tc.name, func(t *testing.T) { |
| 112 | + instanceName := "tikv-" + tc.name |
| 113 | + defer AbnormalInstance.DeleteLabelValues( |
| 114 | + "test-ns", "test-cluster", "tikv", "test-group", instanceName, tc.condType, |
| 115 | + ) |
| 116 | + |
| 117 | + obj := newTiKVForMetricTest(instanceName) |
| 118 | + var conds []metav1.Condition |
| 119 | + if !tc.omitCond { |
| 120 | + conds = []metav1.Condition{{Type: tc.condType, Status: tc.status}} |
| 121 | + } |
| 122 | + ObserveCondition(obj, conds, tc.condType) |
| 123 | + |
| 124 | + val, present := abnormalGaugeValue(t, instanceName, tc.condType) |
| 125 | + require.True(t, present) |
| 126 | + assert.InDelta(t, tc.wantValue, val, 1e-9) |
| 127 | + assert.True(t, gaugeSeriesExists(t, instanceName, tc.condType), |
| 128 | + "series must remain present so PromQL `for:` alerts have continuous samples") |
| 129 | + }) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestClearInstanceConditionMetrics(t *testing.T) { |
| 134 | + name := "tikv-clear" |
| 135 | + obj := newTiKVForMetricTest(name) |
| 136 | + |
| 137 | + // Pre-populate both tracked conditions. |
| 138 | + ObserveCondition(obj, []metav1.Condition{{Type: v1alpha1.CondSynced, Status: metav1.ConditionFalse}}, v1alpha1.CondSynced) |
| 139 | + ObserveCondition(obj, []metav1.Condition{{Type: v1alpha1.CondReady, Status: metav1.ConditionFalse}}, v1alpha1.CondReady) |
| 140 | + require.True(t, gaugeSeriesExists(t, name, v1alpha1.CondSynced)) |
| 141 | + require.True(t, gaugeSeriesExists(t, name, v1alpha1.CondReady)) |
| 142 | + |
| 143 | + ClearInstanceConditionMetrics(obj) |
| 144 | + |
| 145 | + assert.False(t, gaugeSeriesExists(t, name, v1alpha1.CondSynced), |
| 146 | + "Synced series must be removed on clear") |
| 147 | + assert.False(t, gaugeSeriesExists(t, name, v1alpha1.CondReady), |
| 148 | + "Ready series must be removed on clear") |
| 149 | +} |
0 commit comments