metrics.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. Copyright 2019 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "sync"
  16. "time"
  17. "k8s.io/component-base/metrics"
  18. "k8s.io/component-base/metrics/legacyregistry"
  19. )
  20. const (
  21. kubeletSubsystem = "kubelet"
  22. )
  23. var (
  24. // HTTPRequests tracks the number of the http requests received since the server started.
  25. HTTPRequests = metrics.NewCounterVec(
  26. &metrics.CounterOpts{
  27. Subsystem: kubeletSubsystem,
  28. Name: "http_requests_total",
  29. Help: "Number of the http requests received since the server started",
  30. StabilityLevel: metrics.ALPHA,
  31. },
  32. // server_type aims to differentiate the readonly server and the readwrite server.
  33. // long_running marks whether the request is long-running or not.
  34. // Currently, long-running requests include exec/attach/portforward/debug.
  35. []string{"method", "path", "server_type", "long_running"},
  36. )
  37. // HTTPRequestsDuration tracks the duration in seconds to serve http requests.
  38. HTTPRequestsDuration = metrics.NewHistogramVec(
  39. &metrics.HistogramOpts{
  40. Subsystem: kubeletSubsystem,
  41. Name: "http_requests_duration_seconds",
  42. Help: "Duration in seconds to serve http requests",
  43. // Use DefBuckets for now, will customize the buckets if necessary.
  44. Buckets: metrics.DefBuckets,
  45. StabilityLevel: metrics.ALPHA,
  46. },
  47. []string{"method", "path", "server_type", "long_running"},
  48. )
  49. // HTTPInflightRequests tracks the number of the inflight http requests.
  50. HTTPInflightRequests = metrics.NewGaugeVec(
  51. &metrics.GaugeOpts{
  52. Subsystem: kubeletSubsystem,
  53. Name: "http_inflight_requests",
  54. Help: "Number of the inflight http requests",
  55. StabilityLevel: metrics.ALPHA,
  56. },
  57. []string{"method", "path", "server_type", "long_running"},
  58. )
  59. )
  60. var registerMetrics sync.Once
  61. // Register all metrics.
  62. func Register() {
  63. registerMetrics.Do(func() {
  64. legacyregistry.MustRegister(HTTPRequests)
  65. legacyregistry.MustRegister(HTTPRequestsDuration)
  66. legacyregistry.MustRegister(HTTPInflightRequests)
  67. })
  68. }
  69. // SinceInSeconds gets the time since the specified start in seconds.
  70. func SinceInSeconds(start time.Time) float64 {
  71. return time.Since(start).Seconds()
  72. }