k8s.io/apiserver@v0.31.1/pkg/authentication/token/cache/stats.go (about)

     1  /*
     2  Copyright 2019 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 cache
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"k8s.io/component-base/metrics"
    24  	"k8s.io/component-base/metrics/legacyregistry"
    25  )
    26  
    27  var (
    28  	requestLatency = metrics.NewHistogramVec(
    29  		&metrics.HistogramOpts{
    30  			Namespace:      "authentication",
    31  			Subsystem:      "token_cache",
    32  			Name:           "request_duration_seconds",
    33  			StabilityLevel: metrics.ALPHA,
    34  		},
    35  		[]string{"status"},
    36  	)
    37  	requestCount = metrics.NewCounterVec(
    38  		&metrics.CounterOpts{
    39  			Namespace:      "authentication",
    40  			Subsystem:      "token_cache",
    41  			Name:           "request_total",
    42  			StabilityLevel: metrics.ALPHA,
    43  		},
    44  		[]string{"status"},
    45  	)
    46  	fetchCount = metrics.NewCounterVec(
    47  		&metrics.CounterOpts{
    48  			Namespace:      "authentication",
    49  			Subsystem:      "token_cache",
    50  			Name:           "fetch_total",
    51  			StabilityLevel: metrics.ALPHA,
    52  		},
    53  		[]string{"status"},
    54  	)
    55  	activeFetchCount = metrics.NewGaugeVec(
    56  		&metrics.GaugeOpts{
    57  			Namespace:      "authentication",
    58  			Subsystem:      "token_cache",
    59  			Name:           "active_fetch_count",
    60  			StabilityLevel: metrics.ALPHA,
    61  		},
    62  		[]string{"status"},
    63  	)
    64  )
    65  
    66  func init() {
    67  	legacyregistry.MustRegister(
    68  		requestLatency,
    69  		requestCount,
    70  		fetchCount,
    71  		activeFetchCount,
    72  	)
    73  }
    74  
    75  const (
    76  	hitTag  = "hit"
    77  	missTag = "miss"
    78  
    79  	fetchFailedTag = "error"
    80  	fetchOkTag     = "ok"
    81  
    82  	fetchInFlightTag = "in_flight"
    83  	fetchBlockedTag  = "blocked"
    84  )
    85  
    86  type statsCollector struct{}
    87  
    88  var stats = statsCollector{}
    89  
    90  func (statsCollector) authenticating(ctx context.Context) func(hit bool) {
    91  	start := time.Now()
    92  	return func(hit bool) {
    93  		var tag string
    94  		if hit {
    95  			tag = hitTag
    96  		} else {
    97  			tag = missTag
    98  		}
    99  
   100  		latency := time.Since(start)
   101  
   102  		requestCount.WithContext(ctx).WithLabelValues(tag).Inc()
   103  		requestLatency.WithContext(ctx).WithLabelValues(tag).Observe(float64(latency.Milliseconds()) / 1000)
   104  	}
   105  }
   106  
   107  func (statsCollector) blocking(ctx context.Context) func() {
   108  	activeFetchCount.WithContext(ctx).WithLabelValues(fetchBlockedTag).Inc()
   109  	return activeFetchCount.WithContext(ctx).WithLabelValues(fetchBlockedTag).Dec
   110  }
   111  
   112  func (statsCollector) fetching(ctx context.Context) func(ok bool) {
   113  	activeFetchCount.WithContext(ctx).WithLabelValues(fetchInFlightTag).Inc()
   114  	return func(ok bool) {
   115  		var tag string
   116  		if ok {
   117  			tag = fetchOkTag
   118  		} else {
   119  			tag = fetchFailedTag
   120  		}
   121  
   122  		fetchCount.WithContext(ctx).WithLabelValues(tag).Inc()
   123  
   124  		activeFetchCount.WithContext(ctx).WithLabelValues(fetchInFlightTag).Dec()
   125  	}
   126  }