k8s.io/apiserver@v0.31.1/pkg/server/options/encryptionconfig/metrics/metrics.go (about)

     1  /*
     2  Copyright 2023 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 metrics
    18  
    19  import (
    20  	"crypto/sha256"
    21  	"fmt"
    22  	"hash"
    23  	"sync"
    24  
    25  	"k8s.io/component-base/metrics"
    26  	"k8s.io/component-base/metrics/legacyregistry"
    27  )
    28  
    29  const (
    30  	namespace = "apiserver"
    31  	subsystem = "encryption_config_controller"
    32  )
    33  
    34  var (
    35  	encryptionConfigAutomaticReloadsTotal = metrics.NewCounterVec(
    36  		&metrics.CounterOpts{
    37  			Namespace:      namespace,
    38  			Subsystem:      subsystem,
    39  			Name:           "automatic_reloads_total",
    40  			Help:           "Total number of reload successes and failures of encryption configuration split by apiserver identity.",
    41  			StabilityLevel: metrics.ALPHA,
    42  		},
    43  		[]string{"status", "apiserver_id_hash"},
    44  	)
    45  
    46  	// deprecatedEncryptionConfigAutomaticReloadFailureTotal has been deprecated in 1.30.0
    47  	// use encryptionConfigAutomaticReloadsTotal instead
    48  	deprecatedEncryptionConfigAutomaticReloadFailureTotal = metrics.NewCounterVec(
    49  		&metrics.CounterOpts{
    50  			Namespace:         namespace,
    51  			Subsystem:         subsystem,
    52  			Name:              "automatic_reload_failures_total",
    53  			Help:              "Total number of failed automatic reloads of encryption configuration split by apiserver identity.",
    54  			StabilityLevel:    metrics.ALPHA,
    55  			DeprecatedVersion: "1.30.0",
    56  		},
    57  		[]string{"apiserver_id_hash"},
    58  	)
    59  
    60  	// deprecatedEncryptionConfigAutomaticReloadSuccessTotal has been deprecated in 1.30.0
    61  	// use encryptionConfigAutomaticReloadsTotal instead
    62  	deprecatedEncryptionConfigAutomaticReloadSuccessTotal = metrics.NewCounterVec(
    63  		&metrics.CounterOpts{
    64  			Namespace:         namespace,
    65  			Subsystem:         subsystem,
    66  			Name:              "automatic_reload_success_total",
    67  			Help:              "Total number of successful automatic reloads of encryption configuration split by apiserver identity.",
    68  			StabilityLevel:    metrics.ALPHA,
    69  			DeprecatedVersion: "1.30.0",
    70  		},
    71  		[]string{"apiserver_id_hash"},
    72  	)
    73  
    74  	encryptionConfigAutomaticReloadLastTimestampSeconds = metrics.NewGaugeVec(
    75  		&metrics.GaugeOpts{
    76  			Namespace:      namespace,
    77  			Subsystem:      subsystem,
    78  			Name:           "automatic_reload_last_timestamp_seconds",
    79  			Help:           "Timestamp of the last successful or failed automatic reload of encryption configuration split by apiserver identity.",
    80  			StabilityLevel: metrics.ALPHA,
    81  		},
    82  		[]string{"status", "apiserver_id_hash"},
    83  	)
    84  )
    85  
    86  var registerMetrics sync.Once
    87  var hashPool *sync.Pool
    88  
    89  func RegisterMetrics() {
    90  	registerMetrics.Do(func() {
    91  		hashPool = &sync.Pool{
    92  			New: func() interface{} {
    93  				return sha256.New()
    94  			},
    95  		}
    96  		legacyregistry.MustRegister(encryptionConfigAutomaticReloadsTotal)
    97  		legacyregistry.MustRegister(deprecatedEncryptionConfigAutomaticReloadFailureTotal)
    98  		legacyregistry.MustRegister(deprecatedEncryptionConfigAutomaticReloadSuccessTotal)
    99  		legacyregistry.MustRegister(encryptionConfigAutomaticReloadLastTimestampSeconds)
   100  	})
   101  }
   102  
   103  func RecordEncryptionConfigAutomaticReloadFailure(apiServerID string) {
   104  	apiServerIDHash := getHash(apiServerID)
   105  	encryptionConfigAutomaticReloadsTotal.WithLabelValues("failure", apiServerIDHash).Inc()
   106  	deprecatedEncryptionConfigAutomaticReloadFailureTotal.WithLabelValues(apiServerIDHash).Inc()
   107  	recordEncryptionConfigAutomaticReloadTimestamp("failure", apiServerIDHash)
   108  }
   109  
   110  func RecordEncryptionConfigAutomaticReloadSuccess(apiServerID string) {
   111  	apiServerIDHash := getHash(apiServerID)
   112  	encryptionConfigAutomaticReloadsTotal.WithLabelValues("success", apiServerIDHash).Inc()
   113  	deprecatedEncryptionConfigAutomaticReloadSuccessTotal.WithLabelValues(apiServerIDHash).Inc()
   114  	recordEncryptionConfigAutomaticReloadTimestamp("success", apiServerIDHash)
   115  }
   116  
   117  func recordEncryptionConfigAutomaticReloadTimestamp(result, apiServerIDHash string) {
   118  	encryptionConfigAutomaticReloadLastTimestampSeconds.WithLabelValues(result, apiServerIDHash).SetToCurrentTime()
   119  }
   120  
   121  func getHash(data string) string {
   122  	if len(data) == 0 {
   123  		return ""
   124  	}
   125  	h := hashPool.Get().(hash.Hash)
   126  	h.Reset()
   127  	h.Write([]byte(data))
   128  	dataHash := fmt.Sprintf("sha256:%x", h.Sum(nil))
   129  	hashPool.Put(h)
   130  	return dataHash
   131  }