6
6
"time"
7
7
8
8
"cloud.google.com/go/bigquery"
9
+ "github.com/cespare/xxhash/v2"
9
10
v2 "github.com/elastic/beats/v7/filebeat/input/v2"
10
11
cursor "github.com/elastic/beats/v7/filebeat/input/v2/input-cursor"
11
12
"github.com/elastic/beats/v7/libbeat/beat"
@@ -45,10 +46,18 @@ func configure(cfg *conf.C, logger *logp.Logger) ([]cursor.Source, cursor.Input,
45
46
46
47
var sources []cursor.Source
47
48
for _ , query := range config .Queries {
49
+ // defaults to true
50
+ expandJSON := true
51
+ if query .ExpandJsonStrings != nil {
52
+ expandJSON = * query .ExpandJsonStrings
53
+ }
54
+
48
55
sources = append (sources , & bigQuerySource {
49
- ProjectID : config .ProjectID ,
50
- Query : query ,
51
- CursorField : config .CursorField ,
56
+ ProjectID : config .ProjectID ,
57
+ Query : query .Query ,
58
+ CursorField : query .CursorField ,
59
+ TimestampField : query .TimestampField ,
60
+ ExpandJson : expandJSON ,
52
61
})
53
62
}
54
63
@@ -63,13 +72,19 @@ func updateStatus(ctx v2.Context, status status.Status, msg string) {
63
72
64
73
// bigQuerySource defines the configuration for a single BigQuery query.
65
74
type bigQuerySource struct {
66
- ProjectID string
67
- Query string
68
- CursorField string
75
+ ProjectID string
76
+ Query string
77
+ CursorField string
78
+ TimestampField string
79
+ ExpandJson bool
69
80
}
70
81
71
82
func (s * bigQuerySource ) Name () string {
72
- return fmt .Sprintf ("%s-%s-%s" , s .ProjectID , s .Query , s .CursorField )
83
+ // this string uniquely identifies the source in the state store.
84
+ // configuration that doesn't affect the query/cursor itself should not be included.
85
+ name := fmt .Sprintf ("%s-%s-%s" , s .ProjectID , s .Query , s .CursorField )
86
+ // hash it to avoid unintentionally leaching queries into logs/files
87
+ return fmt .Sprintf ("%d" , xxhash .Sum64String (name ))
73
88
}
74
89
75
90
type bigQueryInput struct {
@@ -182,7 +197,7 @@ func (bq *bigQueryInput) publishEvent(src *bigQuerySource, publisher cursor.Publ
182
197
}
183
198
}
184
199
185
- if bq . config . TimestampField != "" && field .Name == bq . config .TimestampField {
200
+ if src . TimestampField != "" && field .Name == src .TimestampField {
186
201
bq .logger .Debugf ("setting timestamp from field %s" , field .Name )
187
202
188
203
ts , err := getTimestamp (field , v )
@@ -193,11 +208,10 @@ func (bq *bigQueryInput) publishEvent(src *bigQuerySource, publisher cursor.Publ
193
208
}
194
209
}
195
210
196
- if bq .config .ExpandJsonStrings {
197
- bq .logger .Debugf ("expanding JSON from field %s" , field .Name )
198
-
211
+ if src .ExpandJson {
199
212
val , err := expandJSON (field , v )
200
213
if err == nil {
214
+ bq .logger .Debugf ("expanding JSON from field %s" , field .Name )
201
215
v = val
202
216
} else {
203
217
// on error, still expand into a nested object with the original string to avoid mapping conflicts
0 commit comments