-
Notifications
You must be signed in to change notification settings - Fork 3
/
destination.go
85 lines (74 loc) · 1.92 KB
/
destination.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
// Copyright (C) 2024 Storj Labs, Inc.
// See LICENSE for copying information.
package bigquery
import (
"context"
"fmt"
"os"
"time"
"google.golang.org/api/option"
"storj.io/eventkit"
"storj.io/eventkit/pb"
)
// BigQueryDestination can be used to save each evenkit package directly to server.
type BigQueryDestination struct {
client *BigQueryClient
SourceInstance string
appName string
}
var _ eventkit.Destination = &BigQueryDestination{}
func NewBigQueryDestination(ctx context.Context, appName, project, dataset string, options ...option.ClientOption) (*BigQueryDestination, error) {
c, err := NewBigQueryClient(ctx, project, dataset, options...)
if err != nil {
return nil, err
}
res := &BigQueryDestination{
client: c,
appName: appName,
}
host, err := os.Hostname()
if err == nil {
res.SourceInstance = host
}
return res, nil
}
// Submit implements Destination.
func (b *BigQueryDestination) Submit(events ...*eventkit.Event) {
var err error
defer mon.Task()(nil)(&err)
records := map[string][]*Record{}
for _, event := range events {
var tags []*pb.Tag
for _, t := range event.Tags {
tags = append(tags, &pb.Tag{
Key: t.Key,
Value: t.Value,
})
}
tableName := TableName(event.Scope, event.Name)
if _, found := records[tableName]; !found {
records[tableName] = make([]*Record, 0)
}
records[tableName] = append(records[tableName], &Record{
Application: Application{
Name: b.appName,
Version: "0.0.1",
},
Source: Source{
Instance: b.SourceInstance,
Address: "0.0.0.0",
},
ReceivedAt: time.Now(),
Timestamp: event.Timestamp,
Tags: tags,
})
}
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
err = b.client.SaveRecord(ctx, records)
if err != nil {
fmt.Println("WARN: Couldn't save eventkit record to BQ: ", err)
}
}
func (b *BigQueryDestination) Run(ctx context.Context) {
}