blob: ee158c2045badc3a8ba2cef795aa37faa548cfba [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 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
17package routes
18
19import (
20 "io"
21 "net/http"
22
23 apimetrics "k8s.io/apiserver/pkg/endpoints/metrics"
24 "k8s.io/apiserver/pkg/server/mux"
25 etcdmetrics "k8s.io/apiserver/pkg/storage/etcd/metrics"
26
27 "github.com/prometheus/client_golang/prometheus"
28)
29
30// DefaultMetrics installs the default prometheus metrics handler
31type DefaultMetrics struct{}
32
33// Install adds the DefaultMetrics handler
34func (m DefaultMetrics) Install(c *mux.PathRecorderMux) {
35 register()
36 c.Handle("/metrics", prometheus.Handler())
37}
38
39// MetricsWithReset install the prometheus metrics handler extended with support for the DELETE method
40// which resets the metrics.
41type MetricsWithReset struct{}
42
43// Install adds the MetricsWithReset handler
44func (m MetricsWithReset) Install(c *mux.PathRecorderMux) {
45 register()
46 defaultMetricsHandler := prometheus.Handler().ServeHTTP
47 c.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
48 if req.Method == "DELETE" {
49 apimetrics.Reset()
50 etcdmetrics.Reset()
51 io.WriteString(w, "metrics reset\n")
52 return
53 }
54 defaultMetricsHandler(w, req)
55 })
56}
57
58// register apiserver and etcd metrics
59func register() {
60 apimetrics.Register()
61 etcdmetrics.Register()
62}