aggregator: add metric for openapi regeneration

This commit is contained in:
Dr. Stefan Schimanski 2019-08-27 13:56:15 +02:00
parent 63dbb234b8
commit beee72e28b
4 changed files with 72 additions and 3 deletions

View File

@ -5,6 +5,7 @@ go_library(
srcs = [
"aggregator.go",
"downloader.go",
"metrics.go",
"priority.go",
],
importmap = "k8s.io/kubernetes/vendor/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator",
@ -14,6 +15,8 @@ go_library(
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/server:go_default_library",
"//staging/src/k8s.io/component-base/metrics:go_default_library",
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
"//staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1:go_default_library",
"//vendor/github.com/emicklei/go-restful:go_default_library",
"//vendor/github.com/go-openapi/spec:go_default_library",

View File

@ -25,6 +25,7 @@ import (
restful "github.com/emicklei/go-restful"
"github.com/go-openapi/spec"
"k8s.io/klog"
"k8s.io/apiserver/pkg/server"
@ -107,7 +108,11 @@ func BuildAndRegisterAggregator(downloader *Downloader, delegationTarget server.
// Build initial spec to serve.
klog.V(2).Infof("Building initial OpenAPI spec")
defer func(start time.Time) {
klog.V(2).Infof("Finished initial OpenAPI spec generation after %v", time.Now().Sub(start))
duration := time.Now().Sub(start)
klog.V(2).Infof("Finished initial OpenAPI spec generation after %v", duration)
regenerationCounter.With(map[string]string{"apiservice": "*", "reason": "startup"})
regenerationDurationGauge.With(map[string]string{"reason": "startup"}).Set(duration.Seconds())
}(time.Now())
specToServe, err := s.buildOpenAPISpec()
if err != nil {
@ -207,6 +212,7 @@ func (s *specAggregator) tryUpdatingServiceSpecs(specInfo *openAPISpecInfo) erro
if specInfo == nil {
return fmt.Errorf("invalid input: specInfo must be non-nil")
}
_, updated := s.openAPISpecs[specInfo.apiService.Name]
origSpecInfo, existedBefore := s.openAPISpecs[specInfo.apiService.Name]
s.openAPISpecs[specInfo.apiService.Name] = specInfo
@ -216,7 +222,16 @@ func (s *specAggregator) tryUpdatingServiceSpecs(specInfo *openAPISpecInfo) erro
}
klog.V(2).Infof("Updating OpenAPI spec because %s is updated", specInfo.apiService.Name)
defer func(start time.Time) {
klog.V(2).Infof("Finished OpenAPI spec generation after %v", time.Now().Sub(start))
duration := time.Now().Sub(start)
klog.V(2).Infof("Finished OpenAPI spec generation after %v", duration)
reason := "add"
if updated {
reason = "update"
}
regenerationCounter.With(map[string]string{"apiservice": specInfo.apiService.Name, "reason": reason})
regenerationDurationGauge.With(map[string]string{"reason": reason}).Set(duration.Seconds())
}(time.Now())
if err := s.updateOpenAPISpec(); err != nil {
if existedBefore {
@ -239,7 +254,11 @@ func (s *specAggregator) tryDeleteServiceSpecs(apiServiceName string) error {
delete(s.openAPISpecs, apiServiceName)
klog.V(2).Infof("Updating OpenAPI spec because %s is removed", apiServiceName)
defer func(start time.Time) {
klog.V(2).Infof("Finished OpenAPI spec generation after %v", time.Now().Sub(start))
duration := time.Now().Sub(start)
klog.V(2).Infof("Finished OpenAPI spec generation after %v", duration)
regenerationCounter.With(map[string]string{"apiservice": apiServiceName, "reason": "delete"})
regenerationDurationGauge.With(map[string]string{"reason": "delete"}).Set(duration.Seconds())
}(time.Now())
if err := s.updateOpenAPISpec(); err != nil {
s.openAPISpecs[apiServiceName] = orgSpecInfo

View File

@ -0,0 +1,46 @@
/*
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 aggregator
import (
"k8s.io/component-base/metrics"
"k8s.io/component-base/metrics/legacyregistry"
)
var (
regenerationCounter = metrics.NewCounterVec(
&metrics.CounterOpts{
Name: "aggregator_openapi_v2_regeneration_count",
Help: "Counter of OpenAPI v2 spec regeneration count broken down by causing APIService name and reason.",
StabilityLevel: metrics.ALPHA,
},
[]string{"apiservice", "reason"},
)
regenerationDurationGauge = metrics.NewGaugeVec(
&metrics.GaugeOpts{
Name: "aggregator_openapi_v2_regeneration_duration",
Help: "Gauge of OpenAPI v2 spec regeneration duration in seconds.",
StabilityLevel: metrics.ALPHA,
},
[]string{"reason"},
)
)
func init() {
legacyregistry.MustRegister(regenerationCounter)
legacyregistry.MustRegister(regenerationDurationGauge)
}

1
vendor/modules.txt vendored
View File

@ -1637,6 +1637,7 @@ k8s.io/component-base/featuregate
k8s.io/component-base/featuregate/testing
k8s.io/component-base/logs
k8s.io/component-base/metrics
k8s.io/component-base/metrics/legacyregistry
k8s.io/component-base/version
# k8s.io/cri-api v0.0.0 => ./staging/src/k8s.io/cri-api
k8s.io/cri-api/pkg/apis