/
exporter.go
88 lines (68 loc) · 2 KB
/
exporter.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
package coordinator
import (
"context"
"fmt"
"net"
pb "github.com/ethpandaops/xatu/pkg/proto/xatu"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/encoding/gzip"
"google.golang.org/grpc/metadata"
)
type ItemExporter struct {
config *Config
log logrus.FieldLogger
conn *grpc.ClientConn
client pb.CoordinatorClient
}
func NewItemExporter(config *Config, log logrus.FieldLogger) (ItemExporter, error) {
var opts []grpc.DialOption
if config.TLS {
host, _, err := net.SplitHostPort(config.Address)
if err != nil {
return ItemExporter{}, fmt.Errorf("fail to get host from address: %v", err)
}
opts = append(opts, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, host)))
} else {
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
}
conn, err := grpc.Dial(config.Address, opts...)
if err != nil {
return ItemExporter{}, fmt.Errorf("fail to dial: %v", err)
}
return ItemExporter{
config: config,
log: log,
conn: conn,
client: pb.NewCoordinatorClient(conn),
}, nil
}
func (e ItemExporter) ExportItems(ctx context.Context, items []*string) error {
e.log.WithField("records", len(items)).Debug("Sending batch of records to coordinator")
if err := e.sendUpstream(ctx, items); err != nil {
return err
}
return nil
}
func (e ItemExporter) Shutdown(ctx context.Context) error {
return nil
}
func (e *ItemExporter) sendUpstream(ctx context.Context, items []*string) error {
records := make([]string, len(items))
for i, item := range items {
records[i] = *item
}
req := &pb.CreateNodeRecordsRequest{
NodeRecords: records,
}
md := metadata.New(e.config.Headers)
ctx = metadata.NewOutgoingContext(ctx, md)
rsp, err := e.client.CreateNodeRecords(ctx, req, grpc.UseCompressor(gzip.Name))
if err != nil {
return err
}
e.log.WithField("response", rsp).Debug("Received response from Xatu sink")
return nil
}