VictoriaMetrics/app/vlinsert/loki/loki_json_test.go
Zakhar Bessarab 09df5b66fd
app/vlinsert: add support of loki push protocol (#4482)
* app/vlinsert: add support of loki push protocol

- implemented loki push protocol for both Protobuf and JSON formats
- added examples in documentation
- added example docker-compose

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* app/vlinsert: move protobuf metric into its own file

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* deployment/docker/victorialogs/promtail: update reference to docker image

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* deployment/docker/victorialogs/promtail: make volume name unique

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* app/vlinsert/loki: add license reference

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* deployment/docker/victorialogs/promtail: fix volume name

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* docs/VictoriaLogs/data-ingestion: add stream fields for loki JSON ingestion example

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* app/vlinsert/loki: move entities to places where those are used

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* app/vlinsert/loki: refactor to use common components

- use CommonParameters from insertutils
- stop ingestion after first error similar to elasticsearch and jsonline

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* app/vlinsert/loki: address review feedback

- add missing logstorage.PutLogRows calls
- refactor tenant ID parsing to use common function
- reduce number of allocations for parsing by reusing  logfields slices
- add tests and benchmarks for requests processing funcs

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

---------

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>
2023-07-20 10:10:55 +02:00

100 lines
2.3 KiB
Go

package loki
import (
"reflect"
"strings"
"testing"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logstorage"
)
func TestProcessJSONRequest(t *testing.T) {
type item struct {
ts int64
fields []logstorage.Field
}
same := func(s string, expected []item) {
t.Helper()
r := strings.NewReader(s)
actual := make([]item, 0)
n, err := processJSONRequest(r, func(timestamp int64, fields []logstorage.Field) {
actual = append(actual, item{
ts: timestamp,
fields: fields,
})
})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(actual) != len(expected) || n != len(expected) {
t.Fatalf("unexpected len(actual)=%d; expecting %d", len(actual), len(expected))
}
for i, actualItem := range actual {
expectedItem := expected[i]
if actualItem.ts != expectedItem.ts {
t.Fatalf("unexpected timestamp for item #%d; got %d; expecting %d", i, actualItem.ts, expectedItem.ts)
}
if !reflect.DeepEqual(actualItem.fields, expectedItem.fields) {
t.Fatalf("unexpected fields for item #%d; got %v; expecting %v", i, actualItem.fields, expectedItem.fields)
}
}
}
fail := func(s string) {
t.Helper()
r := strings.NewReader(s)
actual := make([]item, 0)
_, err := processJSONRequest(r, func(timestamp int64, fields []logstorage.Field) {
actual = append(actual, item{
ts: timestamp,
fields: fields,
})
})
if err == nil {
t.Fatalf("expected to fail with body: %q", s)
}
}
same(`{"streams":[{"stream":{"foo":"bar"},"values":[["1577836800000000000","baz"]]}]}`, []item{
{
ts: 1577836800000000000,
fields: []logstorage.Field{
{
Name: "foo",
Value: "bar",
},
{
Name: "_msg",
Value: "baz",
},
},
},
})
fail(``)
fail(`{"streams":[{"stream":{"foo" = "bar"},"values":[["1577836800000000000","baz"]]}]}`)
fail(`{"streams":[{"stream":{"foo": "bar"}`)
}
func Test_parseLokiTimestamp(t *testing.T) {
f := func(s string, expected int64) {
t.Helper()
actual, err := parseLokiTimestamp(s)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if actual != expected {
t.Fatalf("unexpected timestamp; got %d; expecting %d", actual, expected)
}
}
f("1687510468000000000", 1687510468000000000)
f("1577836800000000000", 1577836800000000000)
}