|
| 1 | +/* |
| 2 | +Copyright 2019 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 testutil |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "reflect" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "github.com/prometheus/common/expfmt" |
| 26 | + "github.com/prometheus/common/model" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + // MetricNameLabel is label under which model.Sample stores metric name |
| 31 | + MetricNameLabel model.LabelName = model.MetricNameLabel |
| 32 | + // QuantileLabel is label under which model.Sample stores latency quantile value |
| 33 | + QuantileLabel model.LabelName = model.QuantileLabel |
| 34 | +) |
| 35 | + |
| 36 | +// Metrics is generic metrics for other specific metrics |
| 37 | +type Metrics map[string]model.Samples |
| 38 | + |
| 39 | +// Equal returns true if all metrics are the same as the arguments. |
| 40 | +func (m *Metrics) Equal(o Metrics) bool { |
| 41 | + leftKeySet := []string{} |
| 42 | + rightKeySet := []string{} |
| 43 | + for k := range *m { |
| 44 | + leftKeySet = append(leftKeySet, k) |
| 45 | + } |
| 46 | + for k := range o { |
| 47 | + rightKeySet = append(rightKeySet, k) |
| 48 | + } |
| 49 | + if !reflect.DeepEqual(leftKeySet, rightKeySet) { |
| 50 | + return false |
| 51 | + } |
| 52 | + for _, k := range leftKeySet { |
| 53 | + if !(*m)[k].Equal(o[k]) { |
| 54 | + return false |
| 55 | + } |
| 56 | + } |
| 57 | + return true |
| 58 | +} |
| 59 | + |
| 60 | +// NewMetrics returns new metrics which are initialized. |
| 61 | +func NewMetrics() Metrics { |
| 62 | + result := make(Metrics) |
| 63 | + return result |
| 64 | +} |
| 65 | + |
| 66 | +// ParseMetrics parses Metrics from data returned from prometheus endpoint |
| 67 | +func ParseMetrics(data string, output *Metrics) error { |
| 68 | + dec := expfmt.NewDecoder(strings.NewReader(data), expfmt.FmtText) |
| 69 | + decoder := expfmt.SampleDecoder{ |
| 70 | + Dec: dec, |
| 71 | + Opts: &expfmt.DecodeOptions{}, |
| 72 | + } |
| 73 | + |
| 74 | + for { |
| 75 | + var v model.Vector |
| 76 | + if err := decoder.Decode(&v); err != nil { |
| 77 | + if err == io.EOF { |
| 78 | + // Expected loop termination condition. |
| 79 | + return nil |
| 80 | + } |
| 81 | + continue |
| 82 | + } |
| 83 | + for _, metric := range v { |
| 84 | + name := string(metric.Metric[model.MetricNameLabel]) |
| 85 | + (*output)[name] = append((*output)[name], metric) |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// ExtractMetricSamples parses the prometheus metric samples from the input string. |
| 91 | +func ExtractMetricSamples(metricsBlob string) ([]*model.Sample, error) { |
| 92 | + dec := expfmt.NewDecoder(strings.NewReader(metricsBlob), expfmt.FmtText) |
| 93 | + decoder := expfmt.SampleDecoder{ |
| 94 | + Dec: dec, |
| 95 | + Opts: &expfmt.DecodeOptions{}, |
| 96 | + } |
| 97 | + |
| 98 | + var samples []*model.Sample |
| 99 | + for { |
| 100 | + var v model.Vector |
| 101 | + if err := decoder.Decode(&v); err != nil { |
| 102 | + if err == io.EOF { |
| 103 | + // Expected loop termination condition. |
| 104 | + return samples, nil |
| 105 | + } |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + samples = append(samples, v...) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// PrintSample returns formated representation of metric Sample |
| 113 | +func PrintSample(sample *model.Sample) string { |
| 114 | + buf := make([]string, 0) |
| 115 | + // Id is a VERY special label. For 'normal' container it's useless, but it's necessary |
| 116 | + // for 'system' containers (e.g. /docker-daemon, /kubelet, etc.). We know if that's the |
| 117 | + // case by checking if there's a label "kubernetes_container_name" present. It's hacky |
| 118 | + // but it works... |
| 119 | + _, normalContainer := sample.Metric["kubernetes_container_name"] |
| 120 | + for k, v := range sample.Metric { |
| 121 | + if strings.HasPrefix(string(k), "__") { |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + if string(k) == "id" && normalContainer { |
| 126 | + continue |
| 127 | + } |
| 128 | + buf = append(buf, fmt.Sprintf("%v=%v", string(k), v)) |
| 129 | + } |
| 130 | + return fmt.Sprintf("[%v] = %v", strings.Join(buf, ","), sample.Value) |
| 131 | +} |
| 132 | + |
| 133 | +// ComputeHistogramDelta computes the change in histogram metric for a selected label. |
| 134 | +// Results are stored in after samples |
| 135 | +func ComputeHistogramDelta(before, after model.Samples, label model.LabelName) { |
| 136 | + beforeSamplesMap := make(map[string]*model.Sample) |
| 137 | + for _, bSample := range before { |
| 138 | + beforeSamplesMap[makeKey(bSample.Metric[label], bSample.Metric["le"])] = bSample |
| 139 | + } |
| 140 | + for _, aSample := range after { |
| 141 | + if bSample, found := beforeSamplesMap[makeKey(aSample.Metric[label], aSample.Metric["le"])]; found { |
| 142 | + aSample.Value = aSample.Value - bSample.Value |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +func makeKey(a, b model.LabelValue) string { |
| 148 | + return string(a) + "___" + string(b) |
| 149 | +} |
0 commit comments