e2e_metrics.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. "bytes"
  16. "encoding/json"
  17. "fmt"
  18. "k8s.io/component-base/metrics/testutil"
  19. e2elog "k8s.io/kubernetes/test/e2e/framework/log"
  20. )
  21. const (
  22. // Cluster Autoscaler metrics names
  23. caFunctionMetric = "cluster_autoscaler_function_duration_seconds_bucket"
  24. caFunctionMetricLabel = "function"
  25. )
  26. // ComponentCollection is metrics collection of components.
  27. type ComponentCollection Collection
  28. func (m *ComponentCollection) filterMetrics() {
  29. apiServerMetrics := make(APIServerMetrics)
  30. for _, metric := range interestingAPIServerMetrics {
  31. apiServerMetrics[metric] = (*m).APIServerMetrics[metric]
  32. }
  33. controllerManagerMetrics := make(ControllerManagerMetrics)
  34. for _, metric := range interestingControllerManagerMetrics {
  35. controllerManagerMetrics[metric] = (*m).ControllerManagerMetrics[metric]
  36. }
  37. kubeletMetrics := make(map[string]KubeletMetrics)
  38. for kubelet, grabbed := range (*m).KubeletMetrics {
  39. kubeletMetrics[kubelet] = make(KubeletMetrics)
  40. for _, metric := range interestingKubeletMetrics {
  41. kubeletMetrics[kubelet][metric] = grabbed[metric]
  42. }
  43. }
  44. (*m).APIServerMetrics = apiServerMetrics
  45. (*m).ControllerManagerMetrics = controllerManagerMetrics
  46. (*m).KubeletMetrics = kubeletMetrics
  47. }
  48. // PrintHumanReadable returns e2e metrics with JSON format.
  49. func (m *ComponentCollection) PrintHumanReadable() string {
  50. buf := bytes.Buffer{}
  51. for _, interestingMetric := range interestingAPIServerMetrics {
  52. buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))
  53. for _, sample := range (*m).APIServerMetrics[interestingMetric] {
  54. buf.WriteString(fmt.Sprintf("\t%v\n", testutil.PrintSample(sample)))
  55. }
  56. }
  57. for _, interestingMetric := range interestingControllerManagerMetrics {
  58. buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))
  59. for _, sample := range (*m).ControllerManagerMetrics[interestingMetric] {
  60. buf.WriteString(fmt.Sprintf("\t%v\n", testutil.PrintSample(sample)))
  61. }
  62. }
  63. for _, interestingMetric := range interestingClusterAutoscalerMetrics {
  64. buf.WriteString(fmt.Sprintf("For %v:\n", interestingMetric))
  65. for _, sample := range (*m).ClusterAutoscalerMetrics[interestingMetric] {
  66. buf.WriteString(fmt.Sprintf("\t%v\n", testutil.PrintSample(sample)))
  67. }
  68. }
  69. for kubelet, grabbed := range (*m).KubeletMetrics {
  70. buf.WriteString(fmt.Sprintf("For %v:\n", kubelet))
  71. for _, interestingMetric := range interestingKubeletMetrics {
  72. buf.WriteString(fmt.Sprintf("\tFor %v:\n", interestingMetric))
  73. for _, sample := range grabbed[interestingMetric] {
  74. buf.WriteString(fmt.Sprintf("\t\t%v\n", testutil.PrintSample(sample)))
  75. }
  76. }
  77. }
  78. return buf.String()
  79. }
  80. // PrettyPrintJSON converts metrics to JSON format.
  81. // TODO: This function should be replaced with framework.PrettyPrintJSON after solving
  82. // circulary dependency between core framework and this metrics subpackage.
  83. func PrettyPrintJSON(metrics interface{}) string {
  84. output := &bytes.Buffer{}
  85. if err := json.NewEncoder(output).Encode(metrics); err != nil {
  86. e2elog.Logf("Error building encoder: %v", err)
  87. return ""
  88. }
  89. formatted := &bytes.Buffer{}
  90. if err := json.Indent(formatted, output.Bytes(), "", " "); err != nil {
  91. e2elog.Logf("Error indenting: %v", err)
  92. return ""
  93. }
  94. return string(formatted.Bytes())
  95. }
  96. // PrintJSON returns e2e metrics with JSON format.
  97. func (m *ComponentCollection) PrintJSON() string {
  98. m.filterMetrics()
  99. return PrettyPrintJSON(m)
  100. }
  101. // SummaryKind returns the summary of e2e metrics.
  102. func (m *ComponentCollection) SummaryKind() string {
  103. return "ComponentCollection"
  104. }
  105. // ComputeClusterAutoscalerMetricsDelta computes the change in cluster
  106. // autoscaler metrics.
  107. func (m *ComponentCollection) ComputeClusterAutoscalerMetricsDelta(before Collection) {
  108. if beforeSamples, found := before.ClusterAutoscalerMetrics[caFunctionMetric]; found {
  109. if afterSamples, found := m.ClusterAutoscalerMetrics[caFunctionMetric]; found {
  110. testutil.ComputeHistogramDelta(beforeSamples, afterSamples, caFunctionMetricLabel)
  111. }
  112. }
  113. }