blob: a01306c65dfa7e15192d19c8f33bad0b49ec4fb5 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
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// LatencyMetric observes client latency partitioned by verb and url.
30type LatencyMetric interface {
31 Observe(verb string, u url.URL, latency time.Duration)
32}
33
34// ResultMetric counts response codes partitioned by method and host.
35type ResultMetric interface {
36 Increment(code string, method string, host string)
37}
38
39var (
40 // RequestLatency is the latency metric that rest clients will update.
41 RequestLatency LatencyMetric = noopLatency{}
42 // RequestResult is the result metric that rest clients will update.
43 RequestResult ResultMetric = noopResult{}
44)
45
46// Register registers metrics for the rest client to use. This can
47// only be called once.
48func Register(lm LatencyMetric, rm ResultMetric) {
49 registerMetrics.Do(func() {
50 RequestLatency = lm
51 RequestResult = rm
52 })
53}
54
55type noopLatency struct{}
56
57func (noopLatency) Observe(string, url.URL, time.Duration) {}
58
59type noopResult struct{}
60
61func (noopResult) Increment(string, string, string) {}