generic_metrics.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. Copyright 2015 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. "io"
  16. "reflect"
  17. "strings"
  18. "github.com/prometheus/common/expfmt"
  19. "github.com/prometheus/common/model"
  20. "k8s.io/klog"
  21. )
  22. // Metrics is generic metrics for other specific metrics
  23. type Metrics map[string]model.Samples
  24. // Equal returns true if all metrics are the same as the arguments.
  25. func (m *Metrics) Equal(o Metrics) bool {
  26. leftKeySet := []string{}
  27. rightKeySet := []string{}
  28. for k := range *m {
  29. leftKeySet = append(leftKeySet, k)
  30. }
  31. for k := range o {
  32. rightKeySet = append(rightKeySet, k)
  33. }
  34. if !reflect.DeepEqual(leftKeySet, rightKeySet) {
  35. return false
  36. }
  37. for _, k := range leftKeySet {
  38. if !(*m)[k].Equal(o[k]) {
  39. return false
  40. }
  41. }
  42. return true
  43. }
  44. // NewMetrics returns new metrics which are initialized.
  45. func NewMetrics() Metrics {
  46. result := make(Metrics)
  47. return result
  48. }
  49. func parseMetrics(data string, output *Metrics) error {
  50. dec := expfmt.NewDecoder(strings.NewReader(data), expfmt.FmtText)
  51. decoder := expfmt.SampleDecoder{
  52. Dec: dec,
  53. Opts: &expfmt.DecodeOptions{},
  54. }
  55. for {
  56. var v model.Vector
  57. if err := decoder.Decode(&v); err != nil {
  58. if err == io.EOF {
  59. // Expected loop termination condition.
  60. return nil
  61. }
  62. klog.Warningf("Invalid Decode. Skipping.")
  63. continue
  64. }
  65. for _, metric := range v {
  66. name := string(metric.Metric[model.MetricNameLabel])
  67. (*output)[name] = append((*output)[name], metric)
  68. }
  69. }
  70. }