Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it more efficient and compatible to use SampleHistogram #439

Merged
merged 5 commits into from
Jan 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions model/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (s Sample) String() string {
if s.Histogram != nil {
return fmt.Sprintf("%s => %s", s.Metric, SampleHistogramPair{
Timestamp: s.Timestamp,
Histogram: *s.Histogram,
Histogram: s.Histogram,
})
}
return fmt.Sprintf("%s => %s", s.Metric, SamplePair{
Expand All @@ -82,7 +82,7 @@ func (s Sample) MarshalJSON() ([]byte, error) {
Metric: s.Metric,
Histogram: SampleHistogramPair{
Timestamp: s.Timestamp,
Histogram: *s.Histogram,
Histogram: s.Histogram,
},
}
return json.Marshal(&v)
Expand Down
4 changes: 2 additions & 2 deletions model/value_histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (s *SampleHistogram) Equal(o *SampleHistogram) bool {

type SampleHistogramPair struct {
Timestamp Time
Histogram SampleHistogram
Histogram *SampleHistogram
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth commenting that it should never be nil and it is as a pointer for efficiency.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should MarshalJSON check that it's nil and error out if it is?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've ended up convincing myself to error on nil histogram, to avoid ever generating invalid JSON from this

}

func (s SampleHistogramPair) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -167,5 +167,5 @@ func (s SampleHistogramPair) String() string {
}

func (s *SampleHistogramPair) Equal(o *SampleHistogramPair) bool {
return s == o || (s.Histogram.Equal(&o.Histogram) && s.Timestamp.Equal(o.Timestamp))
return s == o || (s.Histogram.Equal(o.Histogram) && s.Timestamp.Equal(o.Timestamp))
}
17 changes: 6 additions & 11 deletions model/value_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ var (
noWhitespace = regexp.MustCompile(`\s`)
)

func genSampleHistogram() SampleHistogram {
return SampleHistogram{
func genSampleHistogram() *SampleHistogram {
return &SampleHistogram{
Count: 6,
Sum: 3897,
Buckets: HistogramBuckets{
Expand Down Expand Up @@ -69,11 +69,6 @@ func genSampleHistogram() SampleHistogram {
}
}

func genSampleHistogramPtr() *SampleHistogram {
h := genSampleHistogram()
return &h
}

func TestSampleHistogramPairJSON(t *testing.T) {
input := []struct {
plain string
Expand Down Expand Up @@ -218,7 +213,7 @@ func TestSampleHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
},
Expand Down Expand Up @@ -312,7 +307,7 @@ func TestVectorHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
Timestamp: 1234567,
}},
},
Expand Down Expand Up @@ -424,14 +419,14 @@ func TestVectorHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
&Sample{
Metric: Metric{
"foo": "bar",
},
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
Timestamp: 1234,
},
},
Expand Down
10 changes: 5 additions & 5 deletions model/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ func TestEqualSamples(t *testing.T) {
in1: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
},
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
},
want: true,
},
Expand All @@ -93,7 +93,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
},
want: false,
},
Expand All @@ -117,7 +117,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
},
want: false,
},
Expand All @@ -141,7 +141,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: genSampleHistogramPtr(),
Histogram: genSampleHistogram(),
},
want: false,
},
Expand Down