Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / k8s.io / kube-state-metrics / pkg / metric / metric.go
1 /*
2 Copyright 2018 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 metric
18
19 import (
20         "fmt"
21         "math"
22         "strconv"
23         "strings"
24         "sync"
25 )
26
27 const (
28         initialNumBufSize = 24
29 )
30
31 var (
32         numBufPool = sync.Pool{
33                 New: func() interface{} {
34                         b := make([]byte, 0, initialNumBufSize)
35                         return &b
36                 },
37         }
38 )
39
40 // Type represents the type of a metric e.g. a counter. See
41 // https://prometheus.io/docs/concepts/metric_types/.
42 type Type string
43
44 // Gauge defines a Prometheus gauge.
45 var Gauge Type = "gauge"
46
47 // Counter defines a Prometheus counter.
48 var Counter Type = "counter"
49
50 // Metric represents a single time series.
51 type Metric struct {
52         // The name of a metric is injected by its family to reduce duplication.
53         LabelKeys   []string
54         LabelValues []string
55         Value       float64
56 }
57
58 func (m *Metric) Write(s *strings.Builder) {
59         if len(m.LabelKeys) != len(m.LabelValues) {
60                 panic(fmt.Sprintf(
61                         "expected labelKeys %q to be of same length as labelValues %q",
62                         m.LabelKeys, m.LabelValues,
63                 ))
64         }
65
66         labelsToString(s, m.LabelKeys, m.LabelValues)
67         s.WriteByte(' ')
68         writeFloat(s, m.Value)
69         s.WriteByte('\n')
70 }
71
72 func labelsToString(m *strings.Builder, keys, values []string) {
73         if len(keys) > 0 {
74                 var separator byte = '{'
75
76                 for i := 0; i < len(keys); i++ {
77                         m.WriteByte(separator)
78                         m.WriteString(keys[i])
79                         m.WriteString("=\"")
80                         escapeString(m, values[i])
81                         m.WriteByte('"')
82                         separator = ','
83                 }
84
85                 m.WriteByte('}')
86         }
87 }
88
89 var (
90         escapeWithDoubleQuote = strings.NewReplacer("\\", `\\`, "\n", `\n`, "\"", `\"`)
91 )
92
93 // escapeString replaces '\' by '\\', new line character by '\n', and '"' by
94 // '\"'.
95 // Taken from github.com/prometheus/common/expfmt/text_create.go.
96 func escapeString(m *strings.Builder, v string) {
97         escapeWithDoubleQuote.WriteString(m, v)
98 }
99
100 // writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes
101 // a few common cases for increased efficiency. For non-hardcoded cases, it uses
102 // strconv.AppendFloat to avoid allocations, similar to writeInt.
103 // Taken from github.com/prometheus/common/expfmt/text_create.go.
104 func writeFloat(w *strings.Builder, f float64) {
105         switch {
106         case f == 1:
107                 w.WriteByte('1')
108         case f == 0:
109                 w.WriteByte('0')
110         case f == -1:
111                 w.WriteString("-1")
112         case math.IsNaN(f):
113                 w.WriteString("NaN")
114         case math.IsInf(f, +1):
115                 w.WriteString("+Inf")
116         case math.IsInf(f, -1):
117                 w.WriteString("-Inf")
118         default:
119                 bp := numBufPool.Get().(*[]byte)
120                 *bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64)
121                 w.Write(*bp)
122                 numBufPool.Put(bp)
123         }
124 }