This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
/
bigquery.go
234 lines (207 loc) · 5.99 KB
/
bigquery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package sinks
import (
"bufio"
"cloud.google.com/go/bigquery"
"context"
"encoding/json"
"errors"
"fmt"
"github.com/opsgenie/kubernetes-event-exporter/pkg/batch"
"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
"github.com/rs/zerolog/log"
"google.golang.org/api/option"
"math/rand"
"os"
"time"
"unicode"
)
// Returns a map filtering out keys that have nil value assigned.
func bigQueryDropNils(x map[string]interface{}) map[string]interface{} {
y := make(map[string]interface{})
for key, value := range x {
if value != nil {
if mapValue, ok := value.(map[string]interface{}); ok {
y[key] = bigQueryDropNils(mapValue)
} else {
y[key] = value
}
}
}
return y
}
// Returns a string representing a fixed key. BigQuery expects keys to be valid identifiers, so if they aren't we modify them.
func bigQuerySanitizeKey(key string) string {
var fixedKey string
if !unicode.IsLetter(rune(key[0])) {
fixedKey = "_"
}
for _, ch := range key {
if unicode.IsLetter(ch) || unicode.IsDigit(ch) {
fixedKey = fixedKey + string(ch)
} else {
fixedKey = fixedKey + "_"
}
}
return fixedKey
}
// Returns a map copy with fixed keys.
func bigQuerySanitizeKeys(x map[string]interface{}) map[string]interface{} {
y := make(map[string]interface{})
for key, value := range x {
if mapValue, ok := value.(map[string]interface{}); ok {
y[bigQuerySanitizeKey(key)] = bigQuerySanitizeKeys(mapValue)
} else {
y[bigQuerySanitizeKey(key)] = value
}
}
return y
}
func bigQueryWriteBatchToJsonFile(items []interface{}, path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
writer := bufio.NewWriter(file)
for i := 0; i < len(items); i++ {
event := items[i].(*kube.EnhancedEvent)
var mapStruct map[string]interface{}
json.Unmarshal(event.ToJSON(), &mapStruct)
jsonBytes, _ := json.Marshal(bigQuerySanitizeKeys(bigQueryDropNils(mapStruct)))
fmt.Fprintln(writer, string(jsonBytes))
}
return writer.Flush()
}
func bigQueryCreateDataset(cfg *BigQueryConfig) error {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, cfg.Project, option.WithCredentialsFile(cfg.CredentialsPath))
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
meta := &bigquery.DatasetMetadata{Location: cfg.Location}
if err := client.Dataset(cfg.Dataset).Create(ctx, meta); err != nil {
return err
}
return nil
}
func bigQueryImportJsonFromFile(path string, cfg *BigQueryConfig) error {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, cfg.Project, option.WithCredentialsFile(cfg.CredentialsPath))
if err != nil {
return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return err
}
source := bigquery.NewReaderSource(f)
source.SourceFormat = bigquery.JSON
source.AutoDetect = true
loader := client.Dataset(cfg.Dataset).Table(cfg.Table).LoaderFrom(source)
loader.SchemaUpdateOptions = []string{"ALLOW_FIELD_ADDITION"}
log.Info().Msgf("BigQuery batch uploading %.3f KBs...", float64(fi.Size())/1e3)
job, err := loader.Run(ctx)
if err != nil {
return err
}
status, err := job.Wait(ctx)
if err != nil {
return err
}
log.Info().Msgf("BigQuery batch uploading done.")
if err := status.Err(); err != nil {
return err
}
return nil
}
type BigQueryConfig struct {
// BigQuery table config
Location string `yaml:"location"`
Project string `yaml:"project"`
Dataset string `yaml:"dataset"`
Table string `yaml:"table"`
// Path to a JSON file that contains your service account key.
CredentialsPath string `yaml:"credentials_path"`
// Batching config
BatchSize int `yaml:"batch_size"`
MaxRetries int `yaml:"max_retries"`
IntervalSeconds int `yaml:"interval_seconds"`
TimeoutSeconds int `yaml:"timeout_seconds"`
}
func NewBigQuerySink(cfg *BigQueryConfig) (*BigQuerySink, error) {
if cfg.Location == "" {
cfg.Location = "US"
}
if cfg.Project == "" {
return nil, errors.New("bigquery.project config option must be non-empty")
}
if cfg.Dataset == "" {
return nil, errors.New("bigquery.dataset config option must be non-empty")
}
if cfg.Table == "" {
return nil, errors.New("bigquery.table config option must be non-empty")
}
if cfg.BatchSize == 0 {
cfg.BatchSize = 1000
}
if cfg.MaxRetries == 0 {
cfg.MaxRetries = 3
}
if cfg.IntervalSeconds == 0 {
cfg.IntervalSeconds = 10
}
if cfg.TimeoutSeconds == 0 {
cfg.TimeoutSeconds = 60
}
rand.Seed(time.Now().UTC().UnixNano())
handleBatch := func(ctx context.Context, items []interface{}) []bool {
res := make([]bool, len(items))
for i := 0; i < len(items); i++ {
res[i] = true
}
path := fmt.Sprintf("/tmp/bq_batch-%d-%04x.json", time.Now().UTC().Unix(), rand.Uint64()%65535)
if err := bigQueryWriteBatchToJsonFile(items, path); err != nil {
log.Error().Msgf("Failed to write JSON file: %v", err)
}
if err := bigQueryImportJsonFromFile(path, cfg); err != nil {
log.Error().Msgf("BigQuerySink load failed: %v", err)
} else {
// The batch file is intentionally not deleted in case of failure allowing to manually uplaod it later and debug issues.
if err := os.Remove(path); err != nil {
log.Error().Msgf("Failed to delete file %v: %v", path, err)
}
}
return res
}
if err := bigQueryCreateDataset(cfg); err != nil {
log.Error().Msgf("BigQuerySink create dataset failed: %v", err)
}
batchWriter := batch.NewWriter(
batch.WriterConfig{
BatchSize: cfg.BatchSize,
MaxRetries: cfg.MaxRetries,
Interval: time.Duration(cfg.IntervalSeconds) * time.Second,
Timeout: time.Duration(cfg.TimeoutSeconds) * time.Second,
},
handleBatch,
)
batchWriter.Start()
return &BigQuerySink{batchWriter: batchWriter}, nil
}
type BigQuerySink struct {
batchWriter *batch.Writer
}
func (e *BigQuerySink) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
e.batchWriter.Submit(ev)
return nil
}
func (e *BigQuerySink) Close() {
e.batchWriter.Stop()
}