mirror of
https://github.com/1Password/onepassword-operator.git
synced 2025-10-23 16:00:46 +00:00
Initial 1Password Operator commit
This commit is contained in:
38
vendor/k8s.io/kube-state-metrics/pkg/metric/family.go
generated
vendored
Normal file
38
vendor/k8s.io/kube-state-metrics/pkg/metric/family.go
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package metric
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Family represents a set of metrics with the same name and help text.
|
||||
type Family struct {
|
||||
Name string
|
||||
Metrics []*Metric
|
||||
}
|
||||
|
||||
// ByteSlice returns the given Family in its string representation.
|
||||
func (f Family) ByteSlice() []byte {
|
||||
b := strings.Builder{}
|
||||
for _, m := range f.Metrics {
|
||||
b.WriteString(f.Name)
|
||||
m.Write(&b)
|
||||
}
|
||||
|
||||
return []byte(b.String())
|
||||
}
|
102
vendor/k8s.io/kube-state-metrics/pkg/metric/generator.go
generated
vendored
Normal file
102
vendor/k8s.io/kube-state-metrics/pkg/metric/generator.go
generated
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
/*
|
||||
Copyright 2019 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package metric
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
metricsstore "k8s.io/kube-state-metrics/pkg/metrics_store"
|
||||
)
|
||||
|
||||
// FamilyGenerator provides everything needed to generate a metric family with a
|
||||
// Kubernetes object.
|
||||
type FamilyGenerator struct {
|
||||
Name string
|
||||
Help string
|
||||
Type Type
|
||||
GenerateFunc func(obj interface{}) *Family
|
||||
}
|
||||
|
||||
// Generate calls the FamilyGenerator.GenerateFunc and gives the family its
|
||||
// name. The reasoning behind injecting the name at such a late point in time is
|
||||
// deduplication in the code, preventing typos made by developers as
|
||||
// well as saving memory.
|
||||
func (g *FamilyGenerator) Generate(obj interface{}) *Family {
|
||||
family := g.GenerateFunc(obj)
|
||||
family.Name = g.Name
|
||||
return family
|
||||
}
|
||||
|
||||
func (g *FamilyGenerator) generateHeader() string {
|
||||
header := strings.Builder{}
|
||||
header.WriteString("# HELP ")
|
||||
header.WriteString(g.Name)
|
||||
header.WriteByte(' ')
|
||||
header.WriteString(g.Help)
|
||||
header.WriteByte('\n')
|
||||
header.WriteString("# TYPE ")
|
||||
header.WriteString(g.Name)
|
||||
header.WriteByte(' ')
|
||||
header.WriteString(string(g.Type))
|
||||
|
||||
return header.String()
|
||||
}
|
||||
|
||||
// ExtractMetricFamilyHeaders takes in a slice of FamilyGenerator metrics and
|
||||
// returns the extracted headers.
|
||||
func ExtractMetricFamilyHeaders(families []FamilyGenerator) []string {
|
||||
headers := make([]string, len(families))
|
||||
|
||||
for i, f := range families {
|
||||
headers[i] = f.generateHeader()
|
||||
}
|
||||
|
||||
return headers
|
||||
}
|
||||
|
||||
// ComposeMetricGenFuncs takes a slice of metric families and returns a function
|
||||
// that composes their metric generation functions into a single one.
|
||||
func ComposeMetricGenFuncs(familyGens []FamilyGenerator) func(obj interface{}) []metricsstore.FamilyByteSlicer {
|
||||
return func(obj interface{}) []metricsstore.FamilyByteSlicer {
|
||||
families := make([]metricsstore.FamilyByteSlicer, len(familyGens))
|
||||
|
||||
for i, gen := range familyGens {
|
||||
families[i] = gen.Generate(obj)
|
||||
}
|
||||
|
||||
return families
|
||||
}
|
||||
}
|
||||
|
||||
type whiteBlackLister interface {
|
||||
IsIncluded(string) bool
|
||||
IsExcluded(string) bool
|
||||
}
|
||||
|
||||
// FilterMetricFamilies takes a white- and a blacklist and a slice of metric
|
||||
// families and returns a filtered slice.
|
||||
func FilterMetricFamilies(l whiteBlackLister, families []FamilyGenerator) []FamilyGenerator {
|
||||
filtered := []FamilyGenerator{}
|
||||
|
||||
for _, f := range families {
|
||||
if l.IsIncluded(f.Name) {
|
||||
filtered = append(filtered, f)
|
||||
}
|
||||
}
|
||||
|
||||
return filtered
|
||||
}
|
124
vendor/k8s.io/kube-state-metrics/pkg/metric/metric.go
generated
vendored
Normal file
124
vendor/k8s.io/kube-state-metrics/pkg/metric/metric.go
generated
vendored
Normal file
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package metric
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const (
|
||||
initialNumBufSize = 24
|
||||
)
|
||||
|
||||
var (
|
||||
numBufPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
b := make([]byte, 0, initialNumBufSize)
|
||||
return &b
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// Type represents the type of a metric e.g. a counter. See
|
||||
// https://prometheus.io/docs/concepts/metric_types/.
|
||||
type Type string
|
||||
|
||||
// Gauge defines a Prometheus gauge.
|
||||
var Gauge Type = "gauge"
|
||||
|
||||
// Counter defines a Prometheus counter.
|
||||
var Counter Type = "counter"
|
||||
|
||||
// Metric represents a single time series.
|
||||
type Metric struct {
|
||||
// The name of a metric is injected by its family to reduce duplication.
|
||||
LabelKeys []string
|
||||
LabelValues []string
|
||||
Value float64
|
||||
}
|
||||
|
||||
func (m *Metric) Write(s *strings.Builder) {
|
||||
if len(m.LabelKeys) != len(m.LabelValues) {
|
||||
panic(fmt.Sprintf(
|
||||
"expected labelKeys %q to be of same length as labelValues %q",
|
||||
m.LabelKeys, m.LabelValues,
|
||||
))
|
||||
}
|
||||
|
||||
labelsToString(s, m.LabelKeys, m.LabelValues)
|
||||
s.WriteByte(' ')
|
||||
writeFloat(s, m.Value)
|
||||
s.WriteByte('\n')
|
||||
}
|
||||
|
||||
func labelsToString(m *strings.Builder, keys, values []string) {
|
||||
if len(keys) > 0 {
|
||||
var separator byte = '{'
|
||||
|
||||
for i := 0; i < len(keys); i++ {
|
||||
m.WriteByte(separator)
|
||||
m.WriteString(keys[i])
|
||||
m.WriteString("=\"")
|
||||
escapeString(m, values[i])
|
||||
m.WriteByte('"')
|
||||
separator = ','
|
||||
}
|
||||
|
||||
m.WriteByte('}')
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
escapeWithDoubleQuote = strings.NewReplacer("\\", `\\`, "\n", `\n`, "\"", `\"`)
|
||||
)
|
||||
|
||||
// escapeString replaces '\' by '\\', new line character by '\n', and '"' by
|
||||
// '\"'.
|
||||
// Taken from github.com/prometheus/common/expfmt/text_create.go.
|
||||
func escapeString(m *strings.Builder, v string) {
|
||||
escapeWithDoubleQuote.WriteString(m, v)
|
||||
}
|
||||
|
||||
// writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes
|
||||
// a few common cases for increased efficiency. For non-hardcoded cases, it uses
|
||||
// strconv.AppendFloat to avoid allocations, similar to writeInt.
|
||||
// Taken from github.com/prometheus/common/expfmt/text_create.go.
|
||||
func writeFloat(w *strings.Builder, f float64) {
|
||||
switch {
|
||||
case f == 1:
|
||||
w.WriteByte('1')
|
||||
case f == 0:
|
||||
w.WriteByte('0')
|
||||
case f == -1:
|
||||
w.WriteString("-1")
|
||||
case math.IsNaN(f):
|
||||
w.WriteString("NaN")
|
||||
case math.IsInf(f, +1):
|
||||
w.WriteString("+Inf")
|
||||
case math.IsInf(f, -1):
|
||||
w.WriteString("-Inf")
|
||||
default:
|
||||
bp := numBufPool.Get().(*[]byte)
|
||||
*bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64)
|
||||
w.Write(*bp)
|
||||
numBufPool.Put(bp)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user