Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2014 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 routes |
| 18 | |
| 19 | import ( |
| 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 |
| 31 | type DefaultMetrics struct{} |
| 32 | |
| 33 | // Install adds the DefaultMetrics handler |
| 34 | func (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. |
| 41 | type MetricsWithReset struct{} |
| 42 | |
| 43 | // Install adds the MetricsWithReset handler |
| 44 | func (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 |
| 59 | func register() { |
| 60 | apimetrics.Register() |
| 61 | etcdmetrics.Register() |
| 62 | } |