Skip to content

Commit ca1f99b

Browse files
krajoramacodesome
andauthored
Make it more efficient and compatible to use SampleHistogram (#439)
Store SampleHistogram in SampleHistogramPair as pointer. More compatible with what protobuf generates. More efficient when taking the histogram from the pair. Simpler test code. Signed-off-by: György Krajcsovits <[email protected]> Signed-off-by: George Krajcsovits <[email protected]> Co-authored-by: Ganesh Vernekar <[email protected]>
1 parent 87b15f9 commit ca1f99b

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

model/value.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s Sample) String() string {
6363
if s.Histogram != nil {
6464
return fmt.Sprintf("%s => %s", s.Metric, SampleHistogramPair{
6565
Timestamp: s.Timestamp,
66-
Histogram: *s.Histogram,
66+
Histogram: s.Histogram,
6767
})
6868
}
6969
return fmt.Sprintf("%s => %s", s.Metric, SamplePair{
@@ -82,7 +82,7 @@ func (s Sample) MarshalJSON() ([]byte, error) {
8282
Metric: s.Metric,
8383
Histogram: SampleHistogramPair{
8484
Timestamp: s.Timestamp,
85-
Histogram: *s.Histogram,
85+
Histogram: s.Histogram,
8686
},
8787
}
8888
return json.Marshal(&v)

model/value_histogram.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,18 @@ func (s *SampleHistogram) Equal(o *SampleHistogram) bool {
135135

136136
type SampleHistogramPair struct {
137137
Timestamp Time
138-
Histogram SampleHistogram
138+
// Histogram should never be nil, it's only stored as pointer for efficiency.
139+
Histogram *SampleHistogram
139140
}
140141

141142
func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
142143
t, err := json.Marshal(s.Timestamp)
143144
if err != nil {
144145
return nil, err
145146
}
147+
if s.Histogram == nil {
148+
return nil, fmt.Errorf("histogram is nil")
149+
}
146150
v, err := json.Marshal(s.Histogram)
147151
if err != nil {
148152
return nil, err
@@ -159,6 +163,9 @@ func (s *SampleHistogramPair) UnmarshalJSON(buf []byte) error {
159163
if gotLen := len(tmp); gotLen != wantLen {
160164
return fmt.Errorf("wrong number of fields: %d != %d", gotLen, wantLen)
161165
}
166+
if s.Histogram == nil {
167+
return fmt.Errorf("histogram is null")
168+
}
162169
return nil
163170
}
164171

@@ -167,5 +174,5 @@ func (s SampleHistogramPair) String() string {
167174
}
168175

169176
func (s *SampleHistogramPair) Equal(o *SampleHistogramPair) bool {
170-
return s == o || (s.Histogram.Equal(&o.Histogram) && s.Timestamp.Equal(o.Timestamp))
177+
return s == o || (s.Histogram.Equal(o.Histogram) && s.Timestamp.Equal(o.Timestamp))
171178
}

model/value_histogram_test.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ var (
2424
noWhitespace = regexp.MustCompile(`\s`)
2525
)
2626

27-
func genSampleHistogram() SampleHistogram {
28-
return SampleHistogram{
27+
func genSampleHistogram() *SampleHistogram {
28+
return &SampleHistogram{
2929
Count: 6,
3030
Sum: 3897,
3131
Buckets: HistogramBuckets{
@@ -69,11 +69,6 @@ func genSampleHistogram() SampleHistogram {
6969
}
7070
}
7171

72-
func genSampleHistogramPtr() *SampleHistogram {
73-
h := genSampleHistogram()
74-
return &h
75-
}
76-
7772
func TestSampleHistogramPairJSON(t *testing.T) {
7873
input := []struct {
7974
plain string
@@ -158,6 +153,24 @@ func TestSampleHistogramPairJSON(t *testing.T) {
158153
}
159154
}
160155

156+
func TestInvalidSampleHistogramPairJSON(t *testing.T) {
157+
s1 := SampleHistogramPair{
158+
Timestamp: 1,
159+
Histogram: nil,
160+
}
161+
d, err := json.Marshal(s1)
162+
if err == nil {
163+
t.Errorf("expected error when trying to marshal invalid SampleHistogramPair %s", string(d))
164+
}
165+
166+
var s2 SampleHistogramPair
167+
plain := "[0.001,null]"
168+
err = json.Unmarshal([]byte(plain), &s2)
169+
if err == nil {
170+
t.Errorf("expected error when trying to unmarshal invalid SampleHistogramPair %s", plain)
171+
}
172+
}
173+
161174
func TestSampleHistogramJSON(t *testing.T) {
162175
input := []struct {
163176
plain string
@@ -218,7 +231,7 @@ func TestSampleHistogramJSON(t *testing.T) {
218231
Metric: Metric{
219232
MetricNameLabel: "test_metric",
220233
},
221-
Histogram: genSampleHistogramPtr(),
234+
Histogram: genSampleHistogram(),
222235
Timestamp: 1234567,
223236
},
224237
},
@@ -312,7 +325,7 @@ func TestVectorHistogramJSON(t *testing.T) {
312325
Metric: Metric{
313326
MetricNameLabel: "test_metric",
314327
},
315-
Histogram: genSampleHistogramPtr(),
328+
Histogram: genSampleHistogram(),
316329
Timestamp: 1234567,
317330
}},
318331
},
@@ -424,14 +437,14 @@ func TestVectorHistogramJSON(t *testing.T) {
424437
Metric: Metric{
425438
MetricNameLabel: "test_metric",
426439
},
427-
Histogram: genSampleHistogramPtr(),
440+
Histogram: genSampleHistogram(),
428441
Timestamp: 1234567,
429442
},
430443
&Sample{
431444
Metric: Metric{
432445
"foo": "bar",
433446
},
434-
Histogram: genSampleHistogramPtr(),
447+
Histogram: genSampleHistogram(),
435448
Timestamp: 1234,
436449
},
437450
},

model/value_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ func TestEqualSamples(t *testing.T) {
6464
in1: &Sample{
6565
Metric: Metric{"foo": "bar"},
6666
Timestamp: 0,
67-
Histogram: genSampleHistogramPtr(),
67+
Histogram: genSampleHistogram(),
6868
},
6969
in2: &Sample{
7070
Metric: Metric{"foo": "bar"},
7171
Timestamp: 0,
72-
Histogram: genSampleHistogramPtr(),
72+
Histogram: genSampleHistogram(),
7373
},
7474
want: true,
7575
},
@@ -93,7 +93,7 @@ func TestEqualSamples(t *testing.T) {
9393
in2: &Sample{
9494
Metric: Metric{"foo": "bar"},
9595
Timestamp: 0,
96-
Histogram: genSampleHistogramPtr(),
96+
Histogram: genSampleHistogram(),
9797
},
9898
want: false,
9999
},
@@ -117,7 +117,7 @@ func TestEqualSamples(t *testing.T) {
117117
in2: &Sample{
118118
Metric: Metric{"foo": "bar"},
119119
Timestamp: 0,
120-
Histogram: genSampleHistogramPtr(),
120+
Histogram: genSampleHistogram(),
121121
},
122122
want: false,
123123
},
@@ -141,7 +141,7 @@ func TestEqualSamples(t *testing.T) {
141141
in2: &Sample{
142142
Metric: Metric{"foo": "bar"},
143143
Timestamp: 0,
144-
Histogram: genSampleHistogramPtr(),
144+
Histogram: genSampleHistogram(),
145145
},
146146
want: false,
147147
},

0 commit comments

Comments
 (0)