blob: 5194026bdbf6c5468ee00a11c506ad49df0fb169 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17// Package metrics provides abstractions for registering which metrics
18// to record.
19package metrics
20
21import (
22 "net/url"
23 "sync"
24 "time"
25)
26
27var registerMetrics sync.Once
28
29// DurationMetric is a measurement of some amount of time.
30type DurationMetric interface {
31 Observe(duration time.Duration)
32}
33
34// ExpiryMetric sets some time of expiry. If nil, assume not relevant.
35type ExpiryMetric interface {
36 Set(expiry *time.Time)
37}
38
39// LatencyMetric observes client latency partitioned by verb and url.
40type LatencyMetric interface {
41 Observe(verb string, u url.URL, latency time.Duration)
42}
43
44// ResultMetric counts response codes partitioned by method and host.
45type ResultMetric interface {
46 Increment(code string, method string, host string)
47}
48
49var (
50 // ClientCertExpiry is the expiry time of a client certificate
51 ClientCertExpiry ExpiryMetric = noopExpiry{}
52 // ClientCertRotationAge is the age of a certificate that has just been rotated.
53 ClientCertRotationAge DurationMetric = noopDuration{}
54 // RequestLatency is the latency metric that rest clients will update.
55 RequestLatency LatencyMetric = noopLatency{}
56 // RateLimiterLatency is the client side rate limiter latency metric.
57 RateLimiterLatency LatencyMetric = noopLatency{}
58 // RequestResult is the result metric that rest clients will update.
59 RequestResult ResultMetric = noopResult{}
60)
61
62// RegisterOpts contains all the metrics to register. Metrics may be nil.
63type RegisterOpts struct {
64 ClientCertExpiry ExpiryMetric
65 ClientCertRotationAge DurationMetric
66 RequestLatency LatencyMetric
67 RateLimiterLatency LatencyMetric
68 RequestResult ResultMetric
69}
70
71// Register registers metrics for the rest client to use. This can
72// only be called once.
73func Register(opts RegisterOpts) {
74 registerMetrics.Do(func() {
75 if opts.ClientCertExpiry != nil {
76 ClientCertExpiry = opts.ClientCertExpiry
77 }
78 if opts.ClientCertRotationAge != nil {
79 ClientCertRotationAge = opts.ClientCertRotationAge
80 }
81 if opts.RequestLatency != nil {
82 RequestLatency = opts.RequestLatency
83 }
84 if opts.RateLimiterLatency != nil {
85 RateLimiterLatency = opts.RateLimiterLatency
86 }
87 if opts.RequestResult != nil {
88 RequestResult = opts.RequestResult
89 }
90 })
91}
92
93type noopDuration struct{}
94
95func (noopDuration) Observe(time.Duration) {}
96
97type noopExpiry struct{}
98
99func (noopExpiry) Set(*time.Time) {}
100
101type noopLatency struct{}
102
103func (noopLatency) Observe(string, url.URL, time.Duration) {}
104
105type noopResult struct{}
106
107func (noopResult) Increment(string, string, string) {}