diff --git a/app/vminsert/opentsdbhttp/parser.go b/app/vminsert/opentsdbhttp/parser.go index c1cb32c43..bb6c04916 100644 --- a/app/vminsert/opentsdbhttp/parser.go +++ b/app/vminsert/opentsdbhttp/parser.go @@ -65,7 +65,7 @@ func (r *Row) unmarshal(o *fastjson.Value, tagsPool []Tag) ([]Tag, error) { rawTs := o.Get("timestamp") if rawTs != nil { - ts, err := rawTs.Int64() + ts, err := getFloat64(rawTs) if err != nil { return tagsPool, fmt.Errorf("invalid `timestamp` in %s: %s", o, err) } @@ -80,7 +80,7 @@ func (r *Row) unmarshal(o *fastjson.Value, tagsPool []Tag) ([]Tag, error) { if rawV == nil { return tagsPool, fmt.Errorf("missing `value` in %s", o) } - v, err := getValue(rawV) + v, err := getFloat64(rawV) if err != nil { return tagsPool, fmt.Errorf("invalid `value` in %s: %s", o, err) } @@ -106,7 +106,7 @@ func (r *Row) unmarshal(o *fastjson.Value, tagsPool []Tag) ([]Tag, error) { return tagsPool, nil } -func getValue(v *fastjson.Value) (float64, error) { +func getFloat64(v *fastjson.Value) (float64, error) { switch v.Type() { case fastjson.TypeNumber: return v.Float64() diff --git a/app/vminsert/opentsdbhttp/parser_test.go b/app/vminsert/opentsdbhttp/parser_test.go index 58375d9ba..366d481fa 100644 --- a/app/vminsert/opentsdbhttp/parser_test.go +++ b/app/vminsert/opentsdbhttp/parser_test.go @@ -54,8 +54,8 @@ func TestRowsUnmarshalFailure(t *testing.T) { // Invalid timestamp type f(`{"metric": "aaa", "timestamp": "foobar", "value": 0.45, "tags": {"foo": "bar"}}`) - f(`{"metric": "aaa", "timestamp": 123.456, "value": 0.45, "tags": {"foo": "bar"}}`) - f(`{"metric": "aaa", "timestamp": "123", "value": 0.45, "tags": {"foo": "bar"}}`) + f(`{"metric": "aaa", "timestamp": [1,2], "value": 0.45, "tags": {"foo": "bar"}}`) + f(`{"metric": "aaa", "timestamp": {"a":1}, "value": 0.45, "tags": {"foo": "bar"}}`) // Invalid value type f(`{"metric": "aaa", "timestamp": 1122, "value": [0,1], "tags": {"foo":"bar"}}`) @@ -120,6 +120,30 @@ func TestRowsUnmarshalSuccess(t *testing.T) { }}, }}, }) + // Timestamp as string + f(`{"metric": "foobar", "timestamp": "1789", "value": -123.456, "tags": {"a":"b"}}`, &Rows{ + Rows: []Row{{ + Metric: "foobar", + Value: -123.456, + Timestamp: 1789, + Tags: []Tag{{ + Key: "a", + Value: "b", + }}, + }}, + }) + // Timestamp as float64 (it is truncated to integer) + f(`{"metric": "foobar", "timestamp": 17.89, "value": -123.456, "tags": {"a":"b"}}`, &Rows{ + Rows: []Row{{ + Metric: "foobar", + Value: -123.456, + Timestamp: 17, + Tags: []Tag{{ + Key: "a", + Value: "b", + }}, + }}, + }) // Empty tags f(`{"metric": "foobar", "timestamp": 789, "value": -123.456, "tags": {}}`, &Rows{ Rows: []Row{{