generic_metrics.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. "fmt"
  16. "io"
  17. "reflect"
  18. "strings"
  19. "github.com/golang/glog"
  20. "github.com/prometheus/common/expfmt"
  21. "github.com/prometheus/common/model"
  22. )
  23. type Metrics map[string]model.Samples
  24. func (m *Metrics) Equal(o Metrics) bool {
  25. leftKeySet := []string{}
  26. rightKeySet := []string{}
  27. for k := range *m {
  28. leftKeySet = append(leftKeySet, k)
  29. }
  30. for k := range o {
  31. rightKeySet = append(rightKeySet, k)
  32. }
  33. if !reflect.DeepEqual(leftKeySet, rightKeySet) {
  34. return false
  35. }
  36. for _, k := range leftKeySet {
  37. if !(*m)[k].Equal(o[k]) {
  38. return false
  39. }
  40. }
  41. return true
  42. }
  43. func PrintSample(sample *model.Sample) string {
  44. buf := make([]string, 0)
  45. // Id is a VERY special label. For 'normal' container it's usless, but it's necessary
  46. // for 'system' containers (e.g. /docker-daemon, /kubelet, etc.). We know if that's the
  47. // case by checking if there's a label "kubernetes_container_name" present. It's hacky
  48. // but it works...
  49. _, normalContainer := sample.Metric["kubernetes_container_name"]
  50. for k, v := range sample.Metric {
  51. if strings.HasPrefix(string(k), "__") {
  52. continue
  53. }
  54. if string(k) == "id" && normalContainer {
  55. continue
  56. }
  57. buf = append(buf, fmt.Sprintf("%v=%v", string(k), v))
  58. }
  59. return fmt.Sprintf("[%v] = %v", strings.Join(buf, ","), sample.Value)
  60. }
  61. func NewMetrics() Metrics {
  62. result := make(Metrics)
  63. return result
  64. }
  65. func parseMetrics(data string, output *Metrics) error {
  66. dec := expfmt.NewDecoder(strings.NewReader(data), expfmt.FmtText)
  67. decoder := expfmt.SampleDecoder{
  68. Dec: dec,
  69. Opts: &expfmt.DecodeOptions{},
  70. }
  71. for {
  72. var v model.Vector
  73. if err := decoder.Decode(&v); err != nil {
  74. if err == io.EOF {
  75. // Expected loop termination condition.
  76. return nil
  77. }
  78. glog.Warningf("Invalid Decode. Skipping.")
  79. continue
  80. }
  81. for _, metric := range v {
  82. name := string(metric.Metric[model.MetricNameLabel])
  83. (*output)[name] = append((*output)[name], metric)
  84. }
  85. }
  86. }
  87. func (g *MetricsGrabber) getMetricsFromPod(podName string, namespace string, port int) (string, error) {
  88. rawOutput, err := g.client.Get().
  89. Prefix("proxy").
  90. Namespace(namespace).
  91. Resource("pods").
  92. Name(fmt.Sprintf("%v:%v", podName, port)).
  93. Suffix("metrics").
  94. Do().Raw()
  95. if err != nil {
  96. return "", err
  97. }
  98. return string(rawOutput), nil
  99. }