k8s.io/apiserver@v0.31.1/pkg/util/proxy/metrics/metrics.go (about)

     1  /*
     2  Copyright 2024 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  const (
    28  	subsystem  = "apiserver"
    29  	statuscode = "code"
    30  )
    31  
    32  var registerMetricsOnce sync.Once
    33  
    34  var (
    35  	// streamTranslatorRequestsTotal counts the number of requests that were handled by
    36  	// the StreamTranslatorProxy (RemoteCommand subprotocol).
    37  	streamTranslatorRequestsTotal = metrics.NewCounterVec(
    38  		&metrics.CounterOpts{
    39  			Subsystem:      subsystem,
    40  			Name:           "stream_translator_requests_total",
    41  			Help:           "Total number of requests that were handled by the StreamTranslatorProxy, which processes streaming RemoteCommand/V5",
    42  			StabilityLevel: metrics.ALPHA,
    43  		},
    44  		[]string{statuscode},
    45  	)
    46  	// streamTunnelRequestsTotal counts the number of requests that were handled by
    47  	// the StreamTunnelProxy (PortForward subprotocol).
    48  	streamTunnelRequestsTotal = metrics.NewCounterVec(
    49  		&metrics.CounterOpts{
    50  			Subsystem:      subsystem,
    51  			Name:           "stream_tunnel_requests_total",
    52  			Help:           "Total number of requests that were handled by the StreamTunnelProxy, which processes streaming PortForward/V2",
    53  			StabilityLevel: metrics.ALPHA,
    54  		},
    55  		[]string{statuscode},
    56  	)
    57  )
    58  
    59  func Register() {
    60  	registerMetricsOnce.Do(func() {
    61  		legacyregistry.MustRegister(streamTranslatorRequestsTotal)
    62  		legacyregistry.MustRegister(streamTunnelRequestsTotal)
    63  	})
    64  }
    65  
    66  func ResetForTest() {
    67  	streamTranslatorRequestsTotal.Reset()
    68  	streamTunnelRequestsTotal.Reset()
    69  }
    70  
    71  // IncStreamTranslatorRequest increments the # of requests handled by the StreamTranslatorProxy.
    72  func IncStreamTranslatorRequest(ctx context.Context, status string) {
    73  	streamTranslatorRequestsTotal.WithContext(ctx).WithLabelValues(status).Add(1)
    74  }
    75  
    76  // IncStreamTunnelRequest increments the # of requests handled by the StreamTunnelProxy.
    77  func IncStreamTunnelRequest(ctx context.Context, status string) {
    78  	streamTunnelRequestsTotal.WithContext(ctx).WithLabelValues(status).Add(1)
    79  }