...

Source file src/k8s.io/kubernetes/pkg/controller/certificates/rootcacertpublisher/metrics.go

Documentation: k8s.io/kubernetes/pkg/controller/certificates/rootcacertpublisher

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     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 rootcacertpublisher
    18  
    19  import (
    20  	"strconv"
    21  	"sync"
    22  	"time"
    23  
    24  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    25  	"k8s.io/component-base/metrics"
    26  	"k8s.io/component-base/metrics/legacyregistry"
    27  )
    28  
    29  // RootCACertPublisher - subsystem name used by root_ca_cert_publisher
    30  const RootCACertPublisher = "root_ca_cert_publisher"
    31  
    32  var (
    33  	syncCounter = metrics.NewCounterVec(
    34  		&metrics.CounterOpts{
    35  			Subsystem:      RootCACertPublisher,
    36  			Name:           "sync_total",
    37  			Help:           "Number of namespace syncs happened in root ca cert publisher.",
    38  			StabilityLevel: metrics.ALPHA,
    39  		},
    40  		[]string{"code"},
    41  	)
    42  	syncLatency = metrics.NewHistogramVec(
    43  		&metrics.HistogramOpts{
    44  			Subsystem:      RootCACertPublisher,
    45  			Name:           "sync_duration_seconds",
    46  			Help:           "Number of namespace syncs happened in root ca cert publisher.",
    47  			Buckets:        metrics.ExponentialBuckets(0.001, 2, 15),
    48  			StabilityLevel: metrics.ALPHA,
    49  		},
    50  		[]string{"code"},
    51  	)
    52  )
    53  
    54  func recordMetrics(start time.Time, err error) {
    55  	code := "500"
    56  	if err == nil {
    57  		code = "200"
    58  	} else if se, ok := err.(*apierrors.StatusError); ok && se.Status().Code != 0 {
    59  		code = strconv.Itoa(int(se.Status().Code))
    60  	}
    61  	syncLatency.WithLabelValues(code).Observe(time.Since(start).Seconds())
    62  	syncCounter.WithLabelValues(code).Inc()
    63  }
    64  
    65  var once sync.Once
    66  
    67  func registerMetrics() {
    68  	once.Do(func() {
    69  		legacyregistry.MustRegister(syncCounter)
    70  		legacyregistry.MustRegister(syncLatency)
    71  	})
    72  }
    73  

View as plain text