value.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // Copyright 2013 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 model
  14. import (
  15. "encoding/json"
  16. "fmt"
  17. "sort"
  18. "strconv"
  19. "strings"
  20. )
  21. // ZeroSample is the pseudo zero-value of Sample used to signal a
  22. // non-existing sample. It is a Sample with timestamp Earliest, value 0.0,
  23. // and metric nil. Note that the natural zero value of Sample has a timestamp
  24. // of 0, which is possible to appear in a real Sample and thus not suitable
  25. // to signal a non-existing Sample.
  26. var ZeroSample = Sample{Timestamp: Earliest}
  27. // Sample is a sample pair associated with a metric. A single sample must either
  28. // define Value or Histogram but not both. Histogram == nil implies the Value
  29. // field is used, otherwise it should be ignored.
  30. type Sample struct {
  31. Metric Metric `json:"metric"`
  32. Value SampleValue `json:"value"`
  33. Timestamp Time `json:"timestamp"`
  34. Histogram *SampleHistogram `json:"histogram"`
  35. }
  36. // Equal compares first the metrics, then the timestamp, then the value. The
  37. // semantics of value equality is defined by SampleValue.Equal.
  38. func (s *Sample) Equal(o *Sample) bool {
  39. if s == o {
  40. return true
  41. }
  42. if !s.Metric.Equal(o.Metric) {
  43. return false
  44. }
  45. if !s.Timestamp.Equal(o.Timestamp) {
  46. return false
  47. }
  48. if s.Histogram != nil {
  49. return s.Histogram.Equal(o.Histogram)
  50. }
  51. return s.Value.Equal(o.Value)
  52. }
  53. func (s Sample) String() string {
  54. if s.Histogram != nil {
  55. return fmt.Sprintf("%s => %s", s.Metric, SampleHistogramPair{
  56. Timestamp: s.Timestamp,
  57. Histogram: s.Histogram,
  58. })
  59. }
  60. return fmt.Sprintf("%s => %s", s.Metric, SamplePair{
  61. Timestamp: s.Timestamp,
  62. Value: s.Value,
  63. })
  64. }
  65. // MarshalJSON implements json.Marshaler.
  66. func (s Sample) MarshalJSON() ([]byte, error) {
  67. if s.Histogram != nil {
  68. v := struct {
  69. Metric Metric `json:"metric"`
  70. Histogram SampleHistogramPair `json:"histogram"`
  71. }{
  72. Metric: s.Metric,
  73. Histogram: SampleHistogramPair{
  74. Timestamp: s.Timestamp,
  75. Histogram: s.Histogram,
  76. },
  77. }
  78. return json.Marshal(&v)
  79. }
  80. v := struct {
  81. Metric Metric `json:"metric"`
  82. Value SamplePair `json:"value"`
  83. }{
  84. Metric: s.Metric,
  85. Value: SamplePair{
  86. Timestamp: s.Timestamp,
  87. Value: s.Value,
  88. },
  89. }
  90. return json.Marshal(&v)
  91. }
  92. // UnmarshalJSON implements json.Unmarshaler.
  93. func (s *Sample) UnmarshalJSON(b []byte) error {
  94. v := struct {
  95. Metric Metric `json:"metric"`
  96. Value SamplePair `json:"value"`
  97. Histogram SampleHistogramPair `json:"histogram"`
  98. }{
  99. Metric: s.Metric,
  100. Value: SamplePair{
  101. Timestamp: s.Timestamp,
  102. Value: s.Value,
  103. },
  104. Histogram: SampleHistogramPair{
  105. Timestamp: s.Timestamp,
  106. Histogram: s.Histogram,
  107. },
  108. }
  109. if err := json.Unmarshal(b, &v); err != nil {
  110. return err
  111. }
  112. s.Metric = v.Metric
  113. if v.Histogram.Histogram != nil {
  114. s.Timestamp = v.Histogram.Timestamp
  115. s.Histogram = v.Histogram.Histogram
  116. } else {
  117. s.Timestamp = v.Value.Timestamp
  118. s.Value = v.Value.Value
  119. }
  120. return nil
  121. }
  122. // Samples is a sortable Sample slice. It implements sort.Interface.
  123. type Samples []*Sample
  124. func (s Samples) Len() int {
  125. return len(s)
  126. }
  127. // Less compares first the metrics, then the timestamp.
  128. func (s Samples) Less(i, j int) bool {
  129. switch {
  130. case s[i].Metric.Before(s[j].Metric):
  131. return true
  132. case s[j].Metric.Before(s[i].Metric):
  133. return false
  134. case s[i].Timestamp.Before(s[j].Timestamp):
  135. return true
  136. default:
  137. return false
  138. }
  139. }
  140. func (s Samples) Swap(i, j int) {
  141. s[i], s[j] = s[j], s[i]
  142. }
  143. // Equal compares two sets of samples and returns true if they are equal.
  144. func (s Samples) Equal(o Samples) bool {
  145. if len(s) != len(o) {
  146. return false
  147. }
  148. for i, sample := range s {
  149. if !sample.Equal(o[i]) {
  150. return false
  151. }
  152. }
  153. return true
  154. }
  155. // SampleStream is a stream of Values belonging to an attached COWMetric.
  156. type SampleStream struct {
  157. Metric Metric `json:"metric"`
  158. Values []SamplePair `json:"values"`
  159. Histograms []SampleHistogramPair `json:"histograms"`
  160. }
  161. func (ss SampleStream) String() string {
  162. valuesLength := len(ss.Values)
  163. vals := make([]string, valuesLength+len(ss.Histograms))
  164. for i, v := range ss.Values {
  165. vals[i] = v.String()
  166. }
  167. for i, v := range ss.Histograms {
  168. vals[i+valuesLength] = v.String()
  169. }
  170. return fmt.Sprintf("%s =>\n%s", ss.Metric, strings.Join(vals, "\n"))
  171. }
  172. func (ss SampleStream) MarshalJSON() ([]byte, error) {
  173. if len(ss.Histograms) > 0 && len(ss.Values) > 0 {
  174. v := struct {
  175. Metric Metric `json:"metric"`
  176. Values []SamplePair `json:"values"`
  177. Histograms []SampleHistogramPair `json:"histograms"`
  178. }{
  179. Metric: ss.Metric,
  180. Values: ss.Values,
  181. Histograms: ss.Histograms,
  182. }
  183. return json.Marshal(&v)
  184. } else if len(ss.Histograms) > 0 {
  185. v := struct {
  186. Metric Metric `json:"metric"`
  187. Histograms []SampleHistogramPair `json:"histograms"`
  188. }{
  189. Metric: ss.Metric,
  190. Histograms: ss.Histograms,
  191. }
  192. return json.Marshal(&v)
  193. } else {
  194. v := struct {
  195. Metric Metric `json:"metric"`
  196. Values []SamplePair `json:"values"`
  197. }{
  198. Metric: ss.Metric,
  199. Values: ss.Values,
  200. }
  201. return json.Marshal(&v)
  202. }
  203. }
  204. func (ss *SampleStream) UnmarshalJSON(b []byte) error {
  205. v := struct {
  206. Metric Metric `json:"metric"`
  207. Values []SamplePair `json:"values"`
  208. Histograms []SampleHistogramPair `json:"histograms"`
  209. }{
  210. Metric: ss.Metric,
  211. Values: ss.Values,
  212. Histograms: ss.Histograms,
  213. }
  214. if err := json.Unmarshal(b, &v); err != nil {
  215. return err
  216. }
  217. ss.Metric = v.Metric
  218. ss.Values = v.Values
  219. ss.Histograms = v.Histograms
  220. return nil
  221. }
  222. // Scalar is a scalar value evaluated at the set timestamp.
  223. type Scalar struct {
  224. Value SampleValue `json:"value"`
  225. Timestamp Time `json:"timestamp"`
  226. }
  227. func (s Scalar) String() string {
  228. return fmt.Sprintf("scalar: %v @[%v]", s.Value, s.Timestamp)
  229. }
  230. // MarshalJSON implements json.Marshaler.
  231. func (s Scalar) MarshalJSON() ([]byte, error) {
  232. v := strconv.FormatFloat(float64(s.Value), 'f', -1, 64)
  233. return json.Marshal([...]interface{}{s.Timestamp, string(v)})
  234. }
  235. // UnmarshalJSON implements json.Unmarshaler.
  236. func (s *Scalar) UnmarshalJSON(b []byte) error {
  237. var f string
  238. v := [...]interface{}{&s.Timestamp, &f}
  239. if err := json.Unmarshal(b, &v); err != nil {
  240. return err
  241. }
  242. value, err := strconv.ParseFloat(f, 64)
  243. if err != nil {
  244. return fmt.Errorf("error parsing sample value: %w", err)
  245. }
  246. s.Value = SampleValue(value)
  247. return nil
  248. }
  249. // String is a string value evaluated at the set timestamp.
  250. type String struct {
  251. Value string `json:"value"`
  252. Timestamp Time `json:"timestamp"`
  253. }
  254. func (s *String) String() string {
  255. return s.Value
  256. }
  257. // MarshalJSON implements json.Marshaler.
  258. func (s String) MarshalJSON() ([]byte, error) {
  259. return json.Marshal([]interface{}{s.Timestamp, s.Value})
  260. }
  261. // UnmarshalJSON implements json.Unmarshaler.
  262. func (s *String) UnmarshalJSON(b []byte) error {
  263. v := [...]interface{}{&s.Timestamp, &s.Value}
  264. return json.Unmarshal(b, &v)
  265. }
  266. // Vector is basically only an alias for Samples, but the
  267. // contract is that in a Vector, all Samples have the same timestamp.
  268. type Vector []*Sample
  269. func (vec Vector) String() string {
  270. entries := make([]string, len(vec))
  271. for i, s := range vec {
  272. entries[i] = s.String()
  273. }
  274. return strings.Join(entries, "\n")
  275. }
  276. func (vec Vector) Len() int { return len(vec) }
  277. func (vec Vector) Swap(i, j int) { vec[i], vec[j] = vec[j], vec[i] }
  278. // Less compares first the metrics, then the timestamp.
  279. func (vec Vector) Less(i, j int) bool {
  280. switch {
  281. case vec[i].Metric.Before(vec[j].Metric):
  282. return true
  283. case vec[j].Metric.Before(vec[i].Metric):
  284. return false
  285. case vec[i].Timestamp.Before(vec[j].Timestamp):
  286. return true
  287. default:
  288. return false
  289. }
  290. }
  291. // Equal compares two sets of samples and returns true if they are equal.
  292. func (vec Vector) Equal(o Vector) bool {
  293. if len(vec) != len(o) {
  294. return false
  295. }
  296. for i, sample := range vec {
  297. if !sample.Equal(o[i]) {
  298. return false
  299. }
  300. }
  301. return true
  302. }
  303. // Matrix is a list of time series.
  304. type Matrix []*SampleStream
  305. func (m Matrix) Len() int { return len(m) }
  306. func (m Matrix) Less(i, j int) bool { return m[i].Metric.Before(m[j].Metric) }
  307. func (m Matrix) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
  308. func (mat Matrix) String() string {
  309. matCp := make(Matrix, len(mat))
  310. copy(matCp, mat)
  311. sort.Sort(matCp)
  312. strs := make([]string, len(matCp))
  313. for i, ss := range matCp {
  314. strs[i] = ss.String()
  315. }
  316. return strings.Join(strs, "\n")
  317. }