k3s/vendor/k8s.io/apiextensions-apiserver/pkg/apiserver/conversion/metrics.go

90 lines
2.9 KiB
Go
Raw Normal View History

2019-04-07 17:07:55 +00:00
/*
Copyright 2019 The Kubernetes Authors.
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 conversion
import (
"fmt"
"strconv"
"sync"
"time"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
2019-09-27 21:51:53 +00:00
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
2019-04-07 17:07:55 +00:00
)
var (
2019-12-12 01:27:03 +00:00
latencyBuckets = metrics.ExponentialBuckets(0.001, 2, 15)
2019-04-07 17:07:55 +00:00
)
// converterMetricFactory holds metrics for all CRD converters
type converterMetricFactory struct {
// A map from a converter name to it's metric. Allows the converterMetric to be created
// again with the same metric for a specific converter (e.g. 'webhook').
2019-09-27 21:51:53 +00:00
durations map[string]*metrics.HistogramVec
2019-04-07 17:07:55 +00:00
factoryLock sync.Mutex
}
func newConverterMertricFactory() *converterMetricFactory {
2019-09-27 21:51:53 +00:00
return &converterMetricFactory{durations: map[string]*metrics.HistogramVec{}, factoryLock: sync.Mutex{}}
2019-04-07 17:07:55 +00:00
}
var _ crConverterInterface = &converterMetric{}
type converterMetric struct {
delegate crConverterInterface
2019-09-27 21:51:53 +00:00
latencies *metrics.HistogramVec
2019-04-07 17:07:55 +00:00
crdName string
}
func (c *converterMetricFactory) addMetrics(converterName string, crdName string, converter crConverterInterface) (crConverterInterface, error) {
c.factoryLock.Lock()
defer c.factoryLock.Unlock()
metric, exists := c.durations[converterName]
if !exists {
2019-09-27 21:51:53 +00:00
metric = metrics.NewHistogramVec(
&metrics.HistogramOpts{
Name: fmt.Sprintf("apiserver_crd_%s_conversion_duration_seconds", converterName),
Help: fmt.Sprintf("CRD %s conversion duration in seconds", converterName),
Buckets: latencyBuckets,
StabilityLevel: metrics.ALPHA,
2019-04-07 17:07:55 +00:00
},
[]string{"crd_name", "from_version", "to_version", "succeeded"})
2019-09-27 21:51:53 +00:00
err := legacyregistry.Register(metric)
2019-04-07 17:07:55 +00:00
if err != nil {
return nil, err
}
c.durations[converterName] = metric
}
return &converterMetric{latencies: metric, delegate: converter, crdName: crdName}, nil
}
func (m *converterMetric) Convert(in runtime.Object, targetGV schema.GroupVersion) (runtime.Object, error) {
start := time.Now()
obj, err := m.delegate.Convert(in, targetGV)
fromVersion := in.GetObjectKind().GroupVersionKind().Version
toVersion := targetGV.Version
// only record this observation if the version is different
if fromVersion != toVersion {
m.latencies.WithLabelValues(
m.crdName, fromVersion, toVersion, strconv.FormatBool(err == nil)).Observe(time.Since(start).Seconds())
}
return obj, err
}