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