json_decode.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2015 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package expfmt
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "io"
  18. "sort"
  19. "github.com/golang/protobuf/proto"
  20. dto "github.com/prometheus/client_model/go"
  21. "github.com/prometheus/common/model"
  22. )
  23. type json2Decoder struct {
  24. dec *json.Decoder
  25. fams []*dto.MetricFamily
  26. }
  27. func newJSON2Decoder(r io.Reader) Decoder {
  28. return &json2Decoder{
  29. dec: json.NewDecoder(r),
  30. }
  31. }
  32. type histogram002 struct {
  33. Labels model.LabelSet `json:"labels"`
  34. Values map[string]float64 `json:"value"`
  35. }
  36. type counter002 struct {
  37. Labels model.LabelSet `json:"labels"`
  38. Value float64 `json:"value"`
  39. }
  40. func protoLabelSet(base, ext model.LabelSet) []*dto.LabelPair {
  41. labels := base.Clone().Merge(ext)
  42. delete(labels, model.MetricNameLabel)
  43. names := make([]string, 0, len(labels))
  44. for ln := range labels {
  45. names = append(names, string(ln))
  46. }
  47. sort.Strings(names)
  48. pairs := make([]*dto.LabelPair, 0, len(labels))
  49. for _, ln := range names {
  50. lv := labels[model.LabelName(ln)]
  51. pairs = append(pairs, &dto.LabelPair{
  52. Name: proto.String(ln),
  53. Value: proto.String(string(lv)),
  54. })
  55. }
  56. return pairs
  57. }
  58. func (d *json2Decoder) more() error {
  59. var entities []struct {
  60. BaseLabels model.LabelSet `json:"baseLabels"`
  61. Docstring string `json:"docstring"`
  62. Metric struct {
  63. Type string `json:"type"`
  64. Values json.RawMessage `json:"value"`
  65. } `json:"metric"`
  66. }
  67. if err := d.dec.Decode(&entities); err != nil {
  68. return err
  69. }
  70. for _, e := range entities {
  71. f := &dto.MetricFamily{
  72. Name: proto.String(string(e.BaseLabels[model.MetricNameLabel])),
  73. Help: proto.String(e.Docstring),
  74. Type: dto.MetricType_UNTYPED.Enum(),
  75. Metric: []*dto.Metric{},
  76. }
  77. d.fams = append(d.fams, f)
  78. switch e.Metric.Type {
  79. case "counter", "gauge":
  80. var values []counter002
  81. if err := json.Unmarshal(e.Metric.Values, &values); err != nil {
  82. return fmt.Errorf("could not extract %s value: %s", e.Metric.Type, err)
  83. }
  84. for _, ctr := range values {
  85. f.Metric = append(f.Metric, &dto.Metric{
  86. Label: protoLabelSet(e.BaseLabels, ctr.Labels),
  87. Untyped: &dto.Untyped{
  88. Value: proto.Float64(ctr.Value),
  89. },
  90. })
  91. }
  92. case "histogram":
  93. var values []histogram002
  94. if err := json.Unmarshal(e.Metric.Values, &values); err != nil {
  95. return fmt.Errorf("could not extract %s value: %s", e.Metric.Type, err)
  96. }
  97. for _, hist := range values {
  98. quants := make([]string, 0, len(values))
  99. for q := range hist.Values {
  100. quants = append(quants, q)
  101. }
  102. sort.Strings(quants)
  103. for _, q := range quants {
  104. value := hist.Values[q]
  105. // The correct label is "quantile" but to not break old expressions
  106. // this remains "percentile"
  107. hist.Labels["percentile"] = model.LabelValue(q)
  108. f.Metric = append(f.Metric, &dto.Metric{
  109. Label: protoLabelSet(e.BaseLabels, hist.Labels),
  110. Untyped: &dto.Untyped{
  111. Value: proto.Float64(value),
  112. },
  113. })
  114. }
  115. }
  116. default:
  117. return fmt.Errorf("unknown metric type %q", e.Metric.Type)
  118. }
  119. }
  120. return nil
  121. }
  122. // Decode implements the Decoder interface.
  123. func (d *json2Decoder) Decode(v *dto.MetricFamily) error {
  124. if len(d.fams) == 0 {
  125. if err := d.more(); err != nil {
  126. return err
  127. }
  128. }
  129. *v = *d.fams[0]
  130. d.fams = d.fams[1:]
  131. return nil
  132. }