|
| 1 | +/* |
| 2 | +Copyright 2019 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 | + "sync" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +const ( |
| 27 | + kubeletSubsystem = "kubelet" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + // HTTPRequests tracks the number of the http requests received since the server started. |
| 32 | + HTTPRequests = prometheus.NewCounterVec( |
| 33 | + prometheus.CounterOpts{ |
| 34 | + Subsystem: kubeletSubsystem, |
| 35 | + Name: "http_requests_total", |
| 36 | + Help: "Number of the http requests received since the server started", |
| 37 | + }, |
| 38 | + // server_type aims to differentiate the readonly server and the readwrite server. |
| 39 | + // long_running marks whether the request is long-running or not. |
| 40 | + // Currently, long-running requests include exec/attach/portforward/debug. |
| 41 | + []string{"method", "path", "host", "server_type", "long_running"}, |
| 42 | + ) |
| 43 | + // HTTPRequestsDuration tracks the duration in seconds to serve http requests. |
| 44 | + HTTPRequestsDuration = prometheus.NewHistogramVec( |
| 45 | + prometheus.HistogramOpts{ |
| 46 | + Subsystem: kubeletSubsystem, |
| 47 | + Name: "http_requests_duration_seconds", |
| 48 | + Help: "Duration in seconds to serve http requests", |
| 49 | + // Use DefBuckets for now, will customize the buckets if necessary. |
| 50 | + Buckets: prometheus.DefBuckets, |
| 51 | + }, |
| 52 | + []string{"method", "path", "host", "server_type", "long_running"}, |
| 53 | + ) |
| 54 | + // HTTPInflightRequests tracks the number of the inflight http requests. |
| 55 | + HTTPInflightRequests = prometheus.NewGaugeVec( |
| 56 | + prometheus.GaugeOpts{ |
| 57 | + Subsystem: kubeletSubsystem, |
| 58 | + Name: "http_inflight_requests", |
| 59 | + Help: "Number of the inflight http requests", |
| 60 | + }, |
| 61 | + []string{"method", "path", "host", "server_type", "long_running"}, |
| 62 | + ) |
| 63 | +) |
| 64 | + |
| 65 | +var registerMetrics sync.Once |
| 66 | + |
| 67 | +// Register all metrics. |
| 68 | +func Register() { |
| 69 | + registerMetrics.Do(func() { |
| 70 | + prometheus.MustRegister(HTTPRequests) |
| 71 | + prometheus.MustRegister(HTTPRequestsDuration) |
| 72 | + prometheus.MustRegister(HTTPInflightRequests) |
| 73 | + }) |
| 74 | +} |
| 75 | + |
| 76 | +// SinceInSeconds gets the time since the specified start in seconds. |
| 77 | +func SinceInSeconds(start time.Time) float64 { |
| 78 | + return time.Since(start).Seconds() |
| 79 | +} |
0 commit comments