k8s.io/apiserver@v0.31.1/pkg/endpoints/handlers/metrics/metrics.go (about)

     1  /*
     2  Copyright 2022 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  	"context"
    21  	"sync"
    22  
    23  	"k8s.io/component-base/metrics"
    24  	"k8s.io/component-base/metrics/legacyregistry"
    25  )
    26  
    27  type RequestBodyVerb string
    28  
    29  const (
    30  	Patch            RequestBodyVerb = "patch"
    31  	Delete           RequestBodyVerb = "delete"
    32  	Update           RequestBodyVerb = "update"
    33  	Create           RequestBodyVerb = "create"
    34  	DeleteCollection RequestBodyVerb = "delete_collection"
    35  )
    36  
    37  var (
    38  	RequestBodySizes = metrics.NewHistogramVec(
    39  		&metrics.HistogramOpts{
    40  			Subsystem: "apiserver",
    41  			Name:      "request_body_size_bytes",
    42  			Help:      "Apiserver request body size in bytes broken out by resource and verb.",
    43  			// we use 0.05 KB as the smallest bucket with 0.1 KB increments up to the
    44  			// apiserver limit.
    45  			Buckets:        metrics.LinearBuckets(50000, 100000, 31),
    46  			StabilityLevel: metrics.ALPHA,
    47  		},
    48  		[]string{"resource", "verb"},
    49  	)
    50  )
    51  
    52  var registerMetrics sync.Once
    53  
    54  // Register all metrics.
    55  func Register() {
    56  	registerMetrics.Do(func() {
    57  		legacyregistry.MustRegister(RequestBodySizes)
    58  	})
    59  }
    60  
    61  func RecordRequestBodySize(ctx context.Context, resource string, verb RequestBodyVerb, size int) {
    62  	RequestBodySizes.WithContext(ctx).WithLabelValues(resource, string(verb)).Observe(float64(size))
    63  }