Skip to content

Commit

Permalink
Fix and add tests for model matrices
Browse files Browse the repository at this point in the history
Signed-off-by: Jeanette Tan <[email protected]>
  • Loading branch information
zenador committed Dec 8, 2022
1 parent 2545c1b commit 6f7d46a
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 120 deletions.
22 changes: 22 additions & 0 deletions model/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,28 @@ func (ss SampleStream) String() string {
return fmt.Sprintf("%s =>\n%s", ss.Metric, strings.Join(vals, "\n"))
}

func (ss SampleStream) MarshalJSON() ([]byte, error) {
if len(ss.Histograms) > 0 {
v := struct {
Metric Metric `json:"metric"`
Histograms []SampleHistogramPair `json:"histograms"`
}{
Metric: ss.Metric,
Histograms: ss.Histograms,
}
return json.Marshal(&v)
} else {
v := struct {
Metric Metric `json:"metric"`
Values []SamplePair `json:"values"`
}{
Metric: ss.Metric,
Values: ss.Values,
}
return json.Marshal(&v)
}
}

// Scalar is a scalar value evaluated at the set timestamp.
type Scalar struct {
Value SampleValue `json:"value"`
Expand Down
71 changes: 71 additions & 0 deletions model/value_float_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,74 @@ func TestVectorJSON(t *testing.T) {
}
}
}

func TestMatrixJSON(t *testing.T) {
input := []struct {
plain string
value Matrix
}{
{
plain: `[]`,
value: Matrix{},
},
{
plain: `[{"metric":{"__name__":"test_metric"},"values":[[1234.567,"123.1"],[12345.678,"123.12"]]},{"metric":{"foo":"bar"},"values":[[2234.567,"223.1"],[22345.678,"223.12"]]}]`,
value: Matrix{
&SampleStream{
Metric: Metric{
MetricNameLabel: "test_metric",
},
Values: []SamplePair{
{
Value: 123.1,
Timestamp: 1234567,
},
{
Value: 123.12,
Timestamp: 12345678,
},
},
},
&SampleStream{
Metric: Metric{
"foo": "bar",
},
Values: []SamplePair{
{
Value: 223.1,
Timestamp: 2234567,
},
{
Value: 223.12,
Timestamp: 22345678,
},
},
},
},
},
}

for _, test := range input {
b, err := json.Marshal(test.value)
if err != nil {
t.Error(err)
continue
}

if string(b) != test.plain {
t.Errorf("encoding error: expected %q, got %q", test.plain, b)
continue
}

var mat Matrix
err = json.Unmarshal(b, &mat)
if err != nil {
t.Error(err)
continue
}

if !reflect.DeepEqual(mat, test.value) {
t.Errorf("decoding error: expected %v, got %v", test.value, mat)
}
}
}
151 changes: 91 additions & 60 deletions model/value_histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@ import (
"testing"
)

func genSampleHistogram() SampleHistogram {
return SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
}
}

func TestSampleHistogramPairJSON(t *testing.T) {
input := []struct {
plain string
Expand All @@ -27,18 +42,7 @@ func TestSampleHistogramPairJSON(t *testing.T) {
{
plain: `[1234.567,{"count":"1","sum":"4500","buckets":[[0,"4466.7196729968955","4870.992343051145","1"]]}]`,
value: SampleHistogramPair{
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
},
Expand Down Expand Up @@ -80,18 +84,7 @@ func TestSampleHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
},
Expand Down Expand Up @@ -133,18 +126,7 @@ func TestVectorHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
Timestamp: 1234567,
}},
},
Expand All @@ -155,36 +137,14 @@ func TestVectorHistogramJSON(t *testing.T) {
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
&Sample{
Metric: Metric{
"foo": "bar",
},
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
Timestamp: 1234,
},
},
Expand Down Expand Up @@ -215,3 +175,74 @@ func TestVectorHistogramJSON(t *testing.T) {
}
}
}

func TestMatrixHistogramJSON(t *testing.T) {
input := []struct {
plain string
value Matrix
}{
{
plain: `[]`,
value: Matrix{},
},
{
plain: `[{"metric":{"__name__":"test_metric"},"histograms":[[1234.567,{"count":"1","sum":"4500","buckets":[[0,"4466.7196729968955","4870.992343051145","1"]]}],[12345.678,{"count":"1","sum":"4500","buckets":[[0,"4466.7196729968955","4870.992343051145","1"]]}]]},{"metric":{"foo":"bar"},"histograms":[[2234.567,{"count":"1","sum":"4500","buckets":[[0,"4466.7196729968955","4870.992343051145","1"]]}],[22345.678,{"count":"1","sum":"4500","buckets":[[0,"4466.7196729968955","4870.992343051145","1"]]}]]}]`,
value: Matrix{
&SampleStream{
Metric: Metric{
MetricNameLabel: "test_metric",
},
Histograms: []SampleHistogramPair{
{
Histogram: genSampleHistogram(),
Timestamp: 1234567,
},
{
Histogram: genSampleHistogram(),
Timestamp: 12345678,
},
},
},
&SampleStream{
Metric: Metric{
"foo": "bar",
},
Histograms: []SampleHistogramPair{
{
Histogram: genSampleHistogram(),
Timestamp: 2234567,
},
{
Histogram: genSampleHistogram(),
Timestamp: 22345678,
},
},
},
},
},
}

for _, test := range input {
b, err := json.Marshal(test.value)
if err != nil {
t.Error(err)
continue
}

if string(b) != test.plain {
t.Errorf("encoding error: expected %q, got %q", test.plain, b)
continue
}

var mat Matrix
err = json.Unmarshal(b, &mat)
if err != nil {
t.Error(err)
continue
}

if !reflect.DeepEqual(mat, test.value) {
t.Errorf("decoding error: expected %v, got %v", test.value, mat)
}
}
}
65 changes: 5 additions & 60 deletions model/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,12 @@ func TestEqualSamples(t *testing.T) {
in1: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
},
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
},
want: true,
},
Expand All @@ -115,18 +93,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
},
want: false,
},
Expand All @@ -150,18 +117,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
},
want: false,
},
Expand All @@ -185,18 +141,7 @@ func TestEqualSamples(t *testing.T) {
in2: &Sample{
Metric: Metric{"foo": "bar"},
Timestamp: 0,
Histogram: SampleHistogram{
Count: 1,
Sum: 4500,
Buckets: HistogramBuckets{
{
Boundaries: 0,
Lower: 4466.7196729968955,
Upper: 4870.992343051145,
Count: 1,
},
},
},
Histogram: genSampleHistogram(),
},
want: false,
},
Expand Down

0 comments on commit 6f7d46a

Please sign in to comment.