Path: blob/main/component/loki/source/gcplog/internal/gcplogtarget/formatter_test.go
4096 views
package gcplogtarget12import (3"testing"4"time"56"cloud.google.com/go/pubsub"7"github.com/grafana/loki/clients/pkg/promtail/api"8"github.com/grafana/loki/pkg/logproto"9"github.com/prometheus/common/model"10"github.com/prometheus/prometheus/model/relabel"11"github.com/stretchr/testify/require"12)1314func TestFormat(t *testing.T) {15cases := []struct {16name string17msg *pubsub.Message18labels model.LabelSet19relabel []*relabel.Config20useIncomingTimestamp bool21expected api.Entry22}{23{24name: "relabelling",25msg: &pubsub.Message{26Data: []byte(withAllFields),27},28labels: model.LabelSet{29"jobname": "pubsub-test",30},31relabel: []*relabel.Config{32{33SourceLabels: model.LabelNames{"__gcp_resource_labels_backend_service_name"},34Separator: ";",35Regex: relabel.MustNewRegexp("(.*)"),36TargetLabel: "backend_service_name",37Action: "replace",38Replacement: "$1",39},40{41SourceLabels: model.LabelNames{"__gcp_resource_labels_bucket_name"},42Separator: ";",43Regex: relabel.MustNewRegexp("(.*)"),44TargetLabel: "bucket_name",45Action: "replace",46Replacement: "$1",47},48},49useIncomingTimestamp: true,50expected: api.Entry{51Labels: model.LabelSet{52"jobname": "pubsub-test",53"backend_service_name": "http-loki",54"bucket_name": "loki-bucket",55},56Entry: logproto.Entry{57Timestamp: mustTime(t, "2020-12-22T15:01:23.045123456Z"),58Line: withAllFields,59},60},61},62{63name: "use-original-timestamp",64msg: &pubsub.Message{65Data: []byte(withAllFields),66},67labels: model.LabelSet{68"jobname": "pubsub-test",69},70useIncomingTimestamp: true,71expected: api.Entry{72Labels: model.LabelSet{73"jobname": "pubsub-test",74},75Entry: logproto.Entry{76Timestamp: mustTime(t, "2020-12-22T15:01:23.045123456Z"),77Line: withAllFields,78},79},80},81{82name: "rewrite-timestamp",83msg: &pubsub.Message{84Data: []byte(withAllFields),85},86labels: model.LabelSet{87"jobname": "pubsub-test",88},89expected: api.Entry{90Labels: model.LabelSet{91"jobname": "pubsub-test",92},93Entry: logproto.Entry{94Timestamp: time.Now(),95Line: withAllFields,96},97},98},99}100101for _, c := range cases {102t.Run(c.name, func(t *testing.T) {103got, err := parseGCPLogsEntry(c.msg.Data, c.labels, nil, c.useIncomingTimestamp, c.relabel)104105require.NoError(t, err)106107require.Equal(t, c.expected.Labels, got.Labels)108require.Equal(t, c.expected.Line, got.Line)109if c.useIncomingTimestamp {110require.Equal(t, c.expected.Entry.Timestamp, got.Timestamp)111} else {112if got.Timestamp.Sub(c.expected.Timestamp).Seconds() > 1 {113require.Fail(t, "timestamp shouldn't differ much when rewriting log entry timestamp.")114}115}116})117}118}119120func mustTime(t *testing.T, v string) time.Time {121t.Helper()122123ts, err := time.Parse(time.RFC3339, v)124require.NoError(t, err)125return ts126}127128const (129withAllFields = `{"logName": "https://project/gcs", "resource": {"type": "gcs", "labels": {"backendServiceName": "http-loki", "bucketName": "loki-bucket", "instanceId": "344555"}}, "timestamp": "2020-12-22T15:01:23.045123456Z"}`130)131132133