| 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 |
"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 |
// peerProxiedRequestsTotal counts the number of requests that were proxied to a peer kube-apiserver. |
| 36 |
peerProxiedRequestsTotal = metrics.NewCounterVec( |
| 37 |
&metrics.CounterOpts{ |
| 38 |
Subsystem: subsystem, |
| 39 |
Name: "rerouted_request_total", |
| 40 |
Help: "Total number of requests that were proxied to a peer kube apiserver because the local apiserver was not capable of serving it", |
| 41 |
StabilityLevel: metrics.ALPHA, |
| 42 |
}, |
| 43 |
[]string{statuscode}, |
| 44 |
) |
| 45 |
) |
| 46 |
|
| 47 |
func Register() { |
| 48 |
registerMetricsOnce.Do(func() { |
| 49 |
legacyregistry.MustRegister(peerProxiedRequestsTotal) |
| 50 |
}) |
| 51 |
} |
| 52 |
|
| 53 |
// Only used for tests. |
| 54 |
func Reset() { |
| 55 |
legacyregistry.Reset() |
| 56 |
} |
| 57 |
|
| 58 |
// IncPeerProxiedRequest increments the # of proxied requests to peer kube-apiserver |
| 59 |
func IncPeerProxiedRequest(ctx context.Context, status string) { |
| 60 |
peerProxiedRequestsTotal.WithContext(ctx).WithLabelValues(status).Add(1) |
| 61 |
} |