k8s.io/apiserver@v0.31.1/pkg/authentication/authenticatorfactory/metrics.go (about)

     1  /*
     2  Copyright 2021 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 authenticatorfactory
    18  
    19  import (
    20  	"context"
    21  
    22  	compbasemetrics "k8s.io/component-base/metrics"
    23  	"k8s.io/component-base/metrics/legacyregistry"
    24  )
    25  
    26  type registerables []compbasemetrics.Registerable
    27  
    28  // init registers all metrics.
    29  func init() {
    30  	for _, metric := range metrics {
    31  		legacyregistry.MustRegister(metric)
    32  	}
    33  }
    34  
    35  var (
    36  	requestTotal = compbasemetrics.NewCounterVec(
    37  		&compbasemetrics.CounterOpts{
    38  			Name:           "apiserver_delegated_authn_request_total",
    39  			Help:           "Number of HTTP requests partitioned by status code.",
    40  			StabilityLevel: compbasemetrics.ALPHA,
    41  		},
    42  		[]string{"code"},
    43  	)
    44  
    45  	requestLatency = compbasemetrics.NewHistogramVec(
    46  		&compbasemetrics.HistogramOpts{
    47  			Name:           "apiserver_delegated_authn_request_duration_seconds",
    48  			Help:           "Request latency in seconds. Broken down by status code.",
    49  			Buckets:        []float64{0.25, 0.5, 0.7, 1, 1.5, 3, 5, 10},
    50  			StabilityLevel: compbasemetrics.ALPHA,
    51  		},
    52  		[]string{"code"},
    53  	)
    54  
    55  	metrics = registerables{
    56  		requestTotal,
    57  		requestLatency,
    58  	}
    59  )
    60  
    61  // RecordRequestTotal increments the total number of requests for the delegated authentication.
    62  func RecordRequestTotal(ctx context.Context, code string) {
    63  	requestTotal.WithContext(ctx).WithLabelValues(code).Inc()
    64  }
    65  
    66  // RecordRequestLatency measures request latency in seconds for the delegated authentication. Broken down by status code.
    67  func RecordRequestLatency(ctx context.Context, code string, latency float64) {
    68  	requestLatency.WithContext(ctx).WithLabelValues(code).Observe(latency)
    69  }