metrics.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2017 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. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. const (
  20. // NetworkPluginOperationsKey is the key for operation count metrics.
  21. NetworkPluginOperationsKey = "network_plugin_operations"
  22. // NetworkPluginOperationsLatencyKey is the key for the operation latency metrics.
  23. NetworkPluginOperationsLatencyKey = "network_plugin_operations_duration_seconds"
  24. // DeprecatedNetworkPluginOperationsLatencyKey is the deprecated key for the operation latency metrics.
  25. DeprecatedNetworkPluginOperationsLatencyKey = "network_plugin_operations_latency_microseconds"
  26. // Keep the "kubelet" subsystem for backward compatibility.
  27. kubeletSubsystem = "kubelet"
  28. )
  29. var (
  30. // NetworkPluginOperationsLatency collects operation latency numbers by operation
  31. // type.
  32. NetworkPluginOperationsLatency = prometheus.NewHistogramVec(
  33. prometheus.HistogramOpts{
  34. Subsystem: kubeletSubsystem,
  35. Name: NetworkPluginOperationsLatencyKey,
  36. Help: "Latency in seconds of network plugin operations. Broken down by operation type.",
  37. Buckets: prometheus.DefBuckets,
  38. },
  39. []string{"operation_type"},
  40. )
  41. // DeprecatedNetworkPluginOperationsLatency collects operation latency numbers by operation
  42. // type.
  43. DeprecatedNetworkPluginOperationsLatency = prometheus.NewSummaryVec(
  44. prometheus.SummaryOpts{
  45. Subsystem: kubeletSubsystem,
  46. Name: DeprecatedNetworkPluginOperationsLatencyKey,
  47. Help: "(Deprecated) Latency in microseconds of network plugin operations. Broken down by operation type.",
  48. },
  49. []string{"operation_type"},
  50. )
  51. )
  52. var registerMetrics sync.Once
  53. // Register all metrics.
  54. func Register() {
  55. registerMetrics.Do(func() {
  56. prometheus.MustRegister(NetworkPluginOperationsLatency)
  57. prometheus.MustRegister(DeprecatedNetworkPluginOperationsLatency)
  58. })
  59. }
  60. // SinceInMicroseconds gets the time since the specified start in microseconds.
  61. func SinceInMicroseconds(start time.Time) float64 {
  62. return float64(time.Since(start).Nanoseconds() / time.Microsecond.Nanoseconds())
  63. }
  64. // SinceInSeconds gets the time since the specified start in seconds.
  65. func SinceInSeconds(start time.Time) float64 {
  66. return time.Since(start).Seconds()
  67. }