rebase: update k8s.io packages to v0.29.0

Signed-off-by: Niels de Vos <ndevos@ibm.com>
This commit is contained in:
Niels de Vos
2023-12-20 13:23:59 +01:00
committed by mergify[bot]
parent 328a264202
commit f080b9e0c9
367 changed files with 21340 additions and 11878 deletions

View File

@ -33,6 +33,16 @@ func ExponentialBuckets(start, factor float64, count int) []float64 {
return prometheus.ExponentialBuckets(start, factor, count)
}
// ExponentialBucketsRange creates 'count' buckets, where the lowest bucket is
// 'min' and the highest bucket is 'max'. The final +Inf bucket is not counted
// and not included in the returned slice. The returned slice is meant to be
// used for the Buckets field of HistogramOpts.
//
// The function panics if 'count' is 0 or negative, if 'min' is 0 or negative.
func ExponentialBucketsRange(min, max float64, count int) []float64 {
return prometheus.ExponentialBucketsRange(min, max, count)
}
// MergeBuckets merges buckets together
func MergeBuckets(buckets ...[]float64) []float64 {
result := make([]float64, 1)

View File

@ -166,7 +166,7 @@ func (r *lazyMetric) Create(version *semver.Version) bool {
if deprecatedV != nil {
dv = deprecatedV.String()
}
registeredMetrics.WithLabelValues(string(sl), dv).Inc()
registeredMetricsTotal.WithLabelValues(string(sl), dv).Inc()
return r.IsCreated()
}

View File

@ -31,6 +31,7 @@ type Options struct {
ShowHiddenMetricsForVersion string
DisabledMetrics []string
AllowListMapping map[string]string
AllowListMappingManifest string
}
// NewOptions returns default metrics options
@ -40,6 +41,10 @@ func NewOptions() *Options {
// Validate validates metrics flags options.
func (o *Options) Validate() []error {
if o == nil {
return nil
}
var errs []error
err := validateShowHiddenMetricsVersion(parseVersion(version.Get()), o.ShowHiddenMetricsForVersion)
if err != nil {
@ -77,6 +82,10 @@ func (o *Options) AddFlags(fs *pflag.FlagSet) {
"The map from metric-label to value allow-list of this label. The key's format is <MetricName>,<LabelName>. "+
"The value's format is <allowed_value>,<allowed_value>..."+
"e.g. metric1,label1='v1,v2,v3', metric1,label2='v1,v2,v3' metric2,label1='v1,v2,v3'.")
fs.StringVar(&o.AllowListMappingManifest, "allow-metric-labels-manifest", o.AllowListMappingManifest,
"The path to the manifest file that contains the allow-list mapping. "+
"The format of the file is the same as the flag --allow-metric-labels. "+
"Note that the flag --allow-metric-labels will override the manifest file.")
}
// Apply applies parameters into global configuration of metrics.
@ -93,6 +102,8 @@ func (o *Options) Apply() {
}
if o.AllowListMapping != nil {
SetLabelAllowListFromCLI(o.AllowListMapping)
} else if len(o.AllowListMappingManifest) > 0 {
SetLabelAllowListFromManifest(o.AllowListMappingManifest)
}
}
@ -118,7 +129,7 @@ func validateAllowMetricLabel(allowListMapping map[string]string) error {
for k := range allowListMapping {
reg := regexp.MustCompile(metricNameRegex + `,` + labelRegex)
if reg.FindString(k) != k {
return fmt.Errorf("--allow-metric-labels must has a list of kv pair with format `metricName:labelName=labelValue, labelValue,...`")
return fmt.Errorf("--allow-metric-labels must have a list of kv pair with format `metricName:labelName=labelValue, labelValue,...`")
}
}
return nil

View File

@ -18,13 +18,18 @@ package metrics
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/util/sets"
promext "k8s.io/component-base/metrics/prometheusextension"
"k8s.io/klog/v2"
)
var (
@ -319,6 +324,7 @@ func (allowList *MetricLabelAllowList) ConstrainToAllowedList(labelNameList, lab
if allowValues, ok := allowList.labelToAllowList[name]; ok {
if !allowValues.Has(value) {
labelValueList[index] = "unexpected"
cardinalityEnforcementUnexpectedCategorizationsTotal.Inc()
}
}
}
@ -329,6 +335,7 @@ func (allowList *MetricLabelAllowList) ConstrainLabelMap(labels map[string]strin
if allowValues, ok := allowList.labelToAllowList[name]; ok {
if !allowValues.Has(value) {
labels[name] = "unexpected"
cardinalityEnforcementUnexpectedCategorizationsTotal.Inc()
}
}
}
@ -354,3 +361,20 @@ func SetLabelAllowListFromCLI(allowListMapping map[string]string) {
}
}
}
func SetLabelAllowListFromManifest(manifest string) {
allowListLock.Lock()
defer allowListLock.Unlock()
allowListMapping := make(map[string]string)
data, err := os.ReadFile(filepath.Clean(manifest))
if err != nil {
klog.Errorf("Failed to read allow list manifest: %v", err)
return
}
err = yaml.Unmarshal(data, &allowListMapping)
if err != nil {
klog.Errorf("Failed to parse allow list manifest: %v", err)
return
}
SetLabelAllowListFromCLI(allowListMapping)
}

View File

@ -37,7 +37,7 @@ var (
Namespace: "kubernetes",
Name: "healthcheck",
Help: "This metric records the result of a single healthcheck.",
StabilityLevel: k8smetrics.BETA,
StabilityLevel: k8smetrics.STABLE,
},
[]string{"name", "type"},
)
@ -48,7 +48,7 @@ var (
Namespace: "kubernetes",
Name: "healthchecks_total",
Help: "This metric records the results of all healthcheck.",
StabilityLevel: k8smetrics.BETA,
StabilityLevel: k8smetrics.STABLE,
},
[]string{"name", "type", "status"},
)

View File

@ -37,7 +37,7 @@ var (
registriesLock sync.RWMutex
disabledMetrics = map[string]struct{}{}
registeredMetrics = NewCounterVec(
registeredMetricsTotal = NewCounterVec(
&CounterOpts{
Name: "registered_metrics_total",
Help: "The count of registered metrics broken by stability level and deprecation version.",
@ -61,6 +61,14 @@ var (
StabilityLevel: BETA,
},
)
cardinalityEnforcementUnexpectedCategorizationsTotal = NewCounter(
&CounterOpts{
Name: "cardinality_enforcement_unexpected_categorizations_total",
Help: "The count of unexpected categorizations during cardinality enforcement.",
StabilityLevel: ALPHA,
},
)
)
// shouldHide be used to check if a specific metric with deprecated version should be hidden
@ -379,7 +387,8 @@ func NewKubeRegistry() KubeRegistry {
}
func (r *kubeRegistry) RegisterMetaMetrics() {
r.MustRegister(registeredMetrics)
r.MustRegister(registeredMetricsTotal)
r.MustRegister(disabledMetricsTotal)
r.MustRegister(hiddenMetricsTotal)
r.MustRegister(cardinalityEnforcementUnexpectedCategorizationsTotal)
}

View File

@ -68,6 +68,12 @@ func (s *Span) End(logThreshold time.Duration) {
}
}
// RecordError will record err as an exception span event for this span.
// If this span is not being recorded or err is nil then this method does nothing.
func (s *Span) RecordError(err error, attributes ...attribute.KeyValue) {
s.otelSpan.RecordError(err, trace.WithAttributes(attributes...))
}
func attributesToFields(attributes []attribute.KeyValue) []utiltrace.Field {
fields := make([]utiltrace.Field, len(attributes))
for i := range attributes {

View File

@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
oteltrace "go.opentelemetry.io/otel/trace"
"k8s.io/client-go/transport"
@ -95,9 +96,17 @@ func WithTracing(handler http.Handler, tp oteltrace.TracerProvider, serviceName
otelhttp.WithPropagators(Propagators()),
otelhttp.WithTracerProvider(tp),
}
wrappedHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add the http.target attribute to the otelhttp span
// Workaround for https://github.com/open-telemetry/opentelemetry-go-contrib/issues/3743
if r.URL != nil {
oteltrace.SpanFromContext(r.Context()).SetAttributes(semconv.HTTPTarget(r.URL.RequestURI()))
}
handler.ServeHTTP(w, r)
})
// With Noop TracerProvider, the otelhttp still handles context propagation.
// See https://github.com/open-telemetry/opentelemetry-go/tree/main/example/passthrough
return otelhttp.NewHandler(handler, serviceName, opts...)
return otelhttp.NewHandler(wrappedHandler, serviceName, opts...)
}
// WrapperFor can be used to add tracing to a *rest.Config.