blob: 91ce5b7a45c21772f2eb4979e32227daf4116893 [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// 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
14package model
15
16import (
17 "encoding/json"
18 "errors"
19 "fmt"
20 "strconv"
21 "strings"
22)
23
24type FloatString float64
25
26func (v FloatString) String() string {
27 return strconv.FormatFloat(float64(v), 'f', -1, 64)
28}
29
30func (v FloatString) MarshalJSON() ([]byte, error) {
31 return json.Marshal(v.String())
32}
33
34func (v *FloatString) UnmarshalJSON(b []byte) error {
35 if len(b) < 2 || b[0] != '"' || b[len(b)-1] != '"' {
36 return errors.New("float value must be a quoted string")
37 }
38 f, err := strconv.ParseFloat(string(b[1:len(b)-1]), 64)
39 if err != nil {
40 return err
41 }
42 *v = FloatString(f)
43 return nil
44}
45
46type HistogramBucket struct {
47 Boundaries int32
48 Lower FloatString
49 Upper FloatString
50 Count FloatString
51}
52
53func (s HistogramBucket) MarshalJSON() ([]byte, error) {
54 b, err := json.Marshal(s.Boundaries)
55 if err != nil {
56 return nil, err
57 }
58 l, err := json.Marshal(s.Lower)
59 if err != nil {
60 return nil, err
61 }
62 u, err := json.Marshal(s.Upper)
63 if err != nil {
64 return nil, err
65 }
66 c, err := json.Marshal(s.Count)
67 if err != nil {
68 return nil, err
69 }
70 return []byte(fmt.Sprintf("[%s,%s,%s,%s]", b, l, u, c)), nil
71}
72
73func (s *HistogramBucket) UnmarshalJSON(buf []byte) error {
74 tmp := []interface{}{&s.Boundaries, &s.Lower, &s.Upper, &s.Count}
75 wantLen := len(tmp)
76 if err := json.Unmarshal(buf, &tmp); err != nil {
77 return err
78 }
79 if gotLen := len(tmp); gotLen != wantLen {
80 return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
81 }
82 return nil
83}
84
85func (s *HistogramBucket) Equal(o *HistogramBucket) bool {
86 return s == o || (s.Boundaries == o.Boundaries && s.Lower == o.Lower && s.Upper == o.Upper && s.Count == o.Count)
87}
88
89func (s HistogramBucket) String() string {
90 var sb strings.Builder
91 lowerInclusive := s.Boundaries == 1 || s.Boundaries == 3
92 upperInclusive := s.Boundaries == 0 || s.Boundaries == 3
93 if lowerInclusive {
94 sb.WriteRune('[')
95 } else {
96 sb.WriteRune('(')
97 }
98 fmt.Fprintf(&sb, "%g,%g", s.Lower, s.Upper)
99 if upperInclusive {
100 sb.WriteRune(']')
101 } else {
102 sb.WriteRune(')')
103 }
104 fmt.Fprintf(&sb, ":%v", s.Count)
105 return sb.String()
106}
107
108type HistogramBuckets []*HistogramBucket
109
110func (s HistogramBuckets) Equal(o HistogramBuckets) bool {
111 if len(s) != len(o) {
112 return false
113 }
114
115 for i, bucket := range s {
116 if !bucket.Equal(o[i]) {
117 return false
118 }
119 }
120 return true
121}
122
123type SampleHistogram struct {
124 Count FloatString `json:"count"`
125 Sum FloatString `json:"sum"`
126 Buckets HistogramBuckets `json:"buckets"`
127}
128
129func (s SampleHistogram) String() string {
130 return fmt.Sprintf("Count: %f, Sum: %f, Buckets: %v", s.Count, s.Sum, s.Buckets)
131}
132
133func (s *SampleHistogram) Equal(o *SampleHistogram) bool {
134 return s == o || (s.Count == o.Count && s.Sum == o.Sum && s.Buckets.Equal(o.Buckets))
135}
136
137type SampleHistogramPair struct {
138 Timestamp Time
139 // Histogram should never be nil, it's only stored as pointer for efficiency.
140 Histogram *SampleHistogram
141}
142
143func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
144 if s.Histogram == nil {
145 return nil, errors.New("histogram is nil")
146 }
147 t, err := json.Marshal(s.Timestamp)
148 if err != nil {
149 return nil, err
150 }
151 v, err := json.Marshal(s.Histogram)
152 if err != nil {
153 return nil, err
154 }
155 return []byte(fmt.Sprintf("[%s,%s]", t, v)), nil
156}
157
158func (s *SampleHistogramPair) UnmarshalJSON(buf []byte) error {
159 tmp := []interface{}{&s.Timestamp, &s.Histogram}
160 wantLen := len(tmp)
161 if err := json.Unmarshal(buf, &tmp); err != nil {
162 return err
163 }
164 if gotLen := len(tmp); gotLen != wantLen {
165 return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
166 }
167 if s.Histogram == nil {
168 return errors.New("histogram is null")
169 }
170 return nil
171}
172
173func (s SampleHistogramPair) String() string {
174 return fmt.Sprintf("%s @[%s]", s.Histogram, s.Timestamp)
175}
176
177func (s *SampleHistogramPair) Equal(o *SampleHistogramPair) bool {
178 return s == o || (s.Histogram.Equal(o.Histogram) && s.Timestamp.Equal(o.Timestamp))
179}