2019-05-22 23:16:55 +02:00
|
|
|
package graphite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
2020-07-08 12:59:19 +02:00
|
|
|
"strings"
|
2019-05-22 23:16:55 +02:00
|
|
|
"testing"
|
2021-10-22 11:23:45 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2020-11-15 23:42:27 +01:00
|
|
|
func TestUnmarshalMetricAndTagsFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var r Row
|
|
|
|
_, err := r.UnmarshalMetricAndTags(s, nil)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error for UnmarshalMetricAndTags(%q)", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("")
|
|
|
|
f(";foo=bar")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalMetricAndTagsSuccess(t *testing.T) {
|
|
|
|
f := func(s string, rExpected *Row) {
|
|
|
|
t.Helper()
|
|
|
|
var r Row
|
|
|
|
_, err := r.UnmarshalMetricAndTags(s, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected error in UnmarshalMetricAndTags(%q): %s", s, err)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(&r, rExpected) {
|
|
|
|
t.Fatalf("unexpected row;\ngot\n%+v\nwant\n%+v", &r, rExpected)
|
|
|
|
}
|
|
|
|
}
|
2022-09-26 14:17:22 +02:00
|
|
|
f(" ", &Row{
|
|
|
|
Metric: " ",
|
|
|
|
})
|
|
|
|
f("foo ;bar=baz", &Row{
|
|
|
|
Metric: "foo ",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
f("f oo;bar=baz", &Row{
|
|
|
|
Metric: "f oo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
f("foo;bar=baz ", &Row{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz ",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
f("foo;bar= baz", &Row{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: " baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
f("foo;bar=b az", &Row{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "b az",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
f("foo;b ar=baz", &Row{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "b ar",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-11-15 23:42:27 +01:00
|
|
|
f("foo", &Row{
|
|
|
|
Metric: "foo",
|
|
|
|
})
|
2022-09-26 14:17:22 +02:00
|
|
|
f("foo;bar=123;baz=aa=bb", &Row{
|
2020-11-15 23:42:27 +01:00
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "123",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "baz",
|
2022-09-26 14:17:22 +02:00
|
|
|
Value: "aa=bb",
|
2020-11-15 23:42:27 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
func TestRowsUnmarshalFailure(t *testing.T) {
|
|
|
|
f := func(s string) {
|
|
|
|
t.Helper()
|
|
|
|
var rows Rows
|
2019-08-24 11:42:59 +02:00
|
|
|
rows.Unmarshal(s)
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("unexpected number of rows parsed; got %d; want 0", len(rows.Rows))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try again
|
2019-08-24 11:42:59 +02:00
|
|
|
rows.Unmarshal(s)
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("unexpected number of rows parsed; got %d; want 0", len(rows.Rows))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Missing value
|
|
|
|
f("aaa")
|
|
|
|
|
2020-09-16 00:33:32 +02:00
|
|
|
// invalid value
|
|
|
|
f("aa bb")
|
|
|
|
|
|
|
|
// invalid timestamp
|
|
|
|
f("aa 123 bar")
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRowsUnmarshalSuccess(t *testing.T) {
|
|
|
|
f := func(s string, rowsExpected *Rows) {
|
|
|
|
t.Helper()
|
|
|
|
var rows Rows
|
2019-08-24 11:42:59 +02:00
|
|
|
rows.Unmarshal(s)
|
2019-05-22 23:16:55 +02:00
|
|
|
if !reflect.DeepEqual(rows.Rows, rowsExpected.Rows) {
|
|
|
|
t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", rows.Rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try unmarshaling again
|
2019-08-24 11:42:59 +02:00
|
|
|
rows.Unmarshal(s)
|
2019-05-22 23:16:55 +02:00
|
|
|
if !reflect.DeepEqual(rows.Rows, rowsExpected.Rows) {
|
2022-09-26 14:17:22 +02:00
|
|
|
t.Fatalf("unexpected rows on second unmarshal;\ngot\n%+v;\nwant\n%+v", rows.Rows, rowsExpected.Rows)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rows.Reset()
|
|
|
|
if len(rows.Rows) != 0 {
|
|
|
|
t.Fatalf("non-empty rows after reset: %+v", rows.Rows)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty line
|
|
|
|
f("", &Rows{})
|
2019-08-24 11:42:59 +02:00
|
|
|
f("\r", &Rows{})
|
2019-05-22 23:16:55 +02:00
|
|
|
f("\n\n", &Rows{})
|
2019-08-24 11:42:59 +02:00
|
|
|
f("\n\r\n", &Rows{})
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Single line
|
2022-09-26 14:17:22 +02:00
|
|
|
f(" 123 455", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "123",
|
|
|
|
Value: 455,
|
|
|
|
}},
|
|
|
|
})
|
2019-05-22 23:16:55 +02:00
|
|
|
f("foobar -123.456 789", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foobar",
|
|
|
|
Value: -123.456,
|
|
|
|
Timestamp: 789,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
f("foo.bar 123.456 789\n", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo.bar",
|
|
|
|
Value: 123.456,
|
|
|
|
Timestamp: 789,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2022-09-26 14:17:22 +02:00
|
|
|
// Whitespace in metric name, tag name and tag value
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3102
|
|
|
|
f("s a;ta g1=aaa1;tag2=bb b2;tag3 1 23", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "s a",
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 23,
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "ta g1",
|
|
|
|
Value: "aaa1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "tag2",
|
|
|
|
Value: "bb b2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2019-06-18 18:04:02 +02:00
|
|
|
// Missing timestamp
|
|
|
|
f("aaa 1123", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
|
|
|
}},
|
|
|
|
})
|
2020-07-08 13:10:31 +02:00
|
|
|
f("aaa 1123 -1", &Rows{
|
|
|
|
Rows: []Row{{
|
2020-07-08 13:12:10 +02:00
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 13:10:31 +02:00
|
|
|
Timestamp: -1,
|
|
|
|
}},
|
|
|
|
})
|
2019-06-18 18:04:02 +02:00
|
|
|
|
2019-10-17 17:22:56 +02:00
|
|
|
// Timestamp bigger than 1<<31
|
|
|
|
f("aaa 1123 429496729600", &Rows{
|
|
|
|
Rows: []Row{{
|
2019-10-17 19:04:26 +02:00
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2019-10-17 17:22:56 +02:00
|
|
|
Timestamp: 429496729600,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2020-10-06 10:37:50 +02:00
|
|
|
// Floating-point timestamp
|
|
|
|
// See https://github.com/graphite-project/carbon/blob/b0ba62a62d40a37950fed47a8f6ae6d0f02e6af5/lib/carbon/protocols.py#L197
|
|
|
|
f("aaa 1123 4294.943", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
|
|
|
Timestamp: 4294,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// Tags
|
|
|
|
f("foo;bar=baz 1 2", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
}},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
2019-08-24 12:35:29 +02:00
|
|
|
// Empty tags
|
2021-03-01 16:15:52 +01:00
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1100
|
|
|
|
f("foo; 1", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{},
|
|
|
|
Value: 1,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
f("foo; 1 2", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
// Empty tag name or value
|
|
|
|
f("foo;bar 1 2", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
2019-08-24 12:35:29 +02:00
|
|
|
f("foo;bar=baz;aa=;x=y;=z 1 2", &Rows{
|
2019-05-22 23:16:55 +02:00
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "bar",
|
|
|
|
Value: "baz",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "x",
|
|
|
|
Value: "y",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Multi lines
|
2019-06-18 18:04:02 +02:00
|
|
|
f("foo 0.3 2\naaa 3\nbar.baz 0.34 43\n", &Rows{
|
2019-05-22 23:16:55 +02:00
|
|
|
Rows: []Row{
|
|
|
|
{
|
|
|
|
Metric: "foo",
|
|
|
|
Value: 0.3,
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
2019-06-18 18:04:02 +02:00
|
|
|
{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 3,
|
|
|
|
},
|
2019-05-22 23:16:55 +02:00
|
|
|
{
|
|
|
|
Metric: "bar.baz",
|
|
|
|
Value: 0.34,
|
|
|
|
Timestamp: 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2019-08-24 11:42:59 +02:00
|
|
|
|
|
|
|
// Multi lines with invalid line
|
|
|
|
f("foo 0.3 2\naaa\nbar.baz 0.34 43\n", &Rows{
|
|
|
|
Rows: []Row{
|
|
|
|
{
|
|
|
|
Metric: "foo",
|
|
|
|
Value: 0.3,
|
|
|
|
Timestamp: 2,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: "bar.baz",
|
|
|
|
Value: 0.34,
|
|
|
|
Timestamp: 43,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-10-22 11:23:45 +02:00
|
|
|
|
|
|
|
// With tab as separator
|
|
|
|
// See https://github.com/grobian/carbon-c-relay/commit/f3ffe6cc2b52b07d14acbda649ad3fd6babdd528
|
|
|
|
f("foo.baz\t125.456\t1789\n", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo.baz",
|
|
|
|
Value: 125.456,
|
|
|
|
Timestamp: 1789,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
// With tab as separator and tags
|
|
|
|
f("foo;baz=bar;bb=;y=x;=z\t1\t2", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo",
|
|
|
|
Tags: []Tag{
|
|
|
|
{
|
|
|
|
Key: "baz",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Key: "y",
|
|
|
|
Value: "x",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Value: 1,
|
|
|
|
Timestamp: 2,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
|
2021-12-02 12:32:25 +01:00
|
|
|
// Whitespace after the timestamp
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1865
|
|
|
|
f("foo.baz 125 1789 \na 1.34 567\t ", &Rows{
|
|
|
|
Rows: []Row{
|
|
|
|
{
|
|
|
|
Metric: "foo.baz",
|
|
|
|
Value: 125,
|
|
|
|
Timestamp: 1789,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Metric: "a",
|
|
|
|
Value: 1.34,
|
|
|
|
Timestamp: 567,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-12-02 12:43:49 +01:00
|
|
|
// Multiple whitespaces as separators
|
|
|
|
f("foo.baz \t125 1789 \t\n", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "foo.baz",
|
|
|
|
Value: 125,
|
|
|
|
Timestamp: 1789,
|
|
|
|
}},
|
|
|
|
})
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2020-07-08 12:59:19 +02:00
|
|
|
|
|
|
|
func Test_streamContext_Read(t *testing.T) {
|
|
|
|
f := func(s string, rowsExpected *Rows) {
|
|
|
|
t.Helper()
|
2020-09-28 01:06:27 +02:00
|
|
|
ctx := getStreamContext(strings.NewReader(s))
|
2020-09-28 03:11:55 +02:00
|
|
|
if !ctx.Read() {
|
|
|
|
t.Fatalf("expecting successful read")
|
2020-07-08 12:59:19 +02:00
|
|
|
}
|
2020-09-28 03:11:55 +02:00
|
|
|
uw := getUnmarshalWork()
|
|
|
|
callbackCalls := 0
|
2022-04-06 13:00:08 +02:00
|
|
|
uw.ctx = ctx
|
|
|
|
uw.callback = func(rows []Row) error {
|
2020-09-28 03:11:55 +02:00
|
|
|
callbackCalls++
|
|
|
|
if len(rows) != len(rowsExpected.Rows) {
|
|
|
|
t.Fatalf("different len of expected rows;\ngot\n%+v;\nwant\n%+v", rows, rowsExpected.Rows)
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(rows, rowsExpected.Rows) {
|
|
|
|
t.Fatalf("unexpected rows;\ngot\n%+v;\nwant\n%+v", rows, rowsExpected.Rows)
|
|
|
|
}
|
2022-04-06 13:00:08 +02:00
|
|
|
return nil
|
2020-09-28 03:11:55 +02:00
|
|
|
}
|
|
|
|
uw.reqBuf = append(uw.reqBuf[:0], ctx.reqBuf...)
|
2022-04-06 13:00:08 +02:00
|
|
|
ctx.wg.Add(1)
|
2020-09-28 03:11:55 +02:00
|
|
|
uw.Unmarshal()
|
|
|
|
if callbackCalls != 1 {
|
|
|
|
t.Fatalf("unexpected number of callback calls; got %d; want 1", callbackCalls)
|
2020-07-08 12:59:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-08 13:10:31 +02:00
|
|
|
// Full line without tags
|
|
|
|
f("aaa 1123 345", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 13:12:10 +02:00
|
|
|
Timestamp: 345 * 1000,
|
2020-07-08 13:10:31 +02:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
// Full line with tags
|
|
|
|
f("aaa;x=y 1123 345", &Rows{
|
|
|
|
Rows: []Row{{
|
2020-07-08 13:12:10 +02:00
|
|
|
Metric: "aaa",
|
2020-07-08 13:10:31 +02:00
|
|
|
Tags: []Tag{{
|
2020-07-08 13:12:10 +02:00
|
|
|
Key: "x",
|
2020-07-08 13:10:31 +02:00
|
|
|
Value: "y",
|
|
|
|
}},
|
|
|
|
Value: 1123,
|
2020-07-08 13:12:10 +02:00
|
|
|
Timestamp: 345 * 1000,
|
2020-07-08 13:10:31 +02:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
// missing timestamp.
|
|
|
|
// Note that this test may be flaky due to timing issues. TODO: fix it
|
2020-07-08 12:59:19 +02:00
|
|
|
f("aaa 1123", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
2020-07-08 13:10:31 +02:00
|
|
|
Timestamp: int64(fasttime.UnixTimestamp()) * 1000,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
// -1 timestamp. See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/610
|
|
|
|
// Note that this test may be flaky due to timing issues. TODO: fix it.
|
|
|
|
f("aaa 1123 -1", &Rows{
|
|
|
|
Rows: []Row{{
|
|
|
|
Metric: "aaa",
|
|
|
|
Value: 1123,
|
|
|
|
Timestamp: int64(fasttime.UnixTimestamp()) * 1000,
|
2020-07-08 12:59:19 +02:00
|
|
|
}},
|
|
|
|
})
|
|
|
|
}
|