-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
230 lines (205 loc) · 6.03 KB
/
main.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
/*************************************************************************
* Copyright 2018 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"context"
"fmt"
"net"
"os"
"time"
"github.com/gravwell/gravwell/v3/ingest/entry"
"github.com/gravwell/gravwell/v3/ingest/log"
"github.com/gravwell/gravwell/v3/ingesters/base"
"github.com/gravwell/gravwell/v3/ingesters/utils"
"github.com/gravwell/gravwell/v3/timegrinder"
"cloud.google.com/go/pubsub"
)
const (
defaultConfigLoc = `/opt/gravwell/etc/pubsub_ingest.conf`
defaultConfigDLoc = `/opt/gravwell/etc/pubsub_ingest.conf.d`
appName = `pubsub`
ingesterName = "GooglePubSub"
)
var (
lg *log.Logger
debugOn bool
)
func main() {
var cfg *cfgType
ibc := base.IngesterBaseConfig{
IngesterName: ingesterName,
AppName: appName,
DefaultConfigLocation: defaultConfigLoc,
DefaultConfigOverlayLocation: defaultConfigDLoc,
GetConfigFunc: GetConfig,
}
ib, err := base.Init(ibc)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to get configuration %v\n", err)
return
} else if err = ib.AssignConfig(&cfg); err != nil || cfg == nil {
fmt.Fprintf(os.Stderr, "failed to assign configuration %v %v\n", err, cfg == nil)
return
}
debugOn = ib.Verbose
lg = ib.Logger
igst, err := ib.GetMuxer()
if err != nil {
ib.Logger.FatalCode(0, "failed to get ingest connection", log.KVErr(err))
return
}
defer igst.Close()
ib.AnnounceStartup()
exitCtx, exitFn := context.WithCancel(context.Background())
debugout("Started ingester muxer\n")
// Set up environment variables for AWS auth, if extant
if cfg.Global.Google_Credentials_Path != "" {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", cfg.Global.Google_Credentials_Path)
}
// make a client
ctx := context.Background()
client, err := pubsub.NewClient(ctx, cfg.Global.Project_ID)
if err != nil {
lg.Fatal("failed to create pubsub client", log.KVErr(err))
return
}
var ok bool
for _, psv := range cfg.PubSub {
tagid, err := igst.GetTag(psv.Tag_Name)
if err != nil {
lg.Fatal("failed to resolve tag", log.KV("tag", psv.Tag_Name), log.KVErr(err))
}
procset, err := cfg.Preprocessor.ProcessorSet(igst, psv.Preprocessor)
if err != nil {
lg.Fatal("preprocessor construction failed", log.KVErr(err))
}
// Get the subscription, creating if needed
subname := psv.Subscription_Name
if subname == `` {
subname = fmt.Sprintf("ingest_%s", psv.Topic_Name)
}
sub := client.Subscription(subname)
if ok, err = sub.Exists(ctx); err != nil {
lg.Fatal("error checking subscription", log.KVErr(err))
} else if !ok {
//Subscription does not exist, attempt to create it
// this may fail due to permissions
// get the topic
topic := client.Topic(psv.Topic_Name)
ok, err := topic.Exists(ctx)
if err != nil {
lg.Fatal("error checking topic", log.KVErr(err))
}
if !ok {
lg.Fatal("topic does not exist", log.KV("topic", psv.Topic_Name))
}
// doesn't exist, try creating it
sub, err = client.CreateSubscription(ctx, subname, pubsub.SubscriptionConfig{
Topic: topic,
AckDeadline: 10 * time.Second,
})
if err != nil {
lg.Fatal("error creating subscription", log.KVErr(err))
}
}
var count, size uint64
var oldcount, oldsize uint64
//fire of a verbose ticker for debugging and stats output
if debugOn {
go func() {
for {
time.Sleep(1 * time.Second)
tmpcount := count
tmpsize := size
cdiff := tmpcount - oldcount
sdiff := tmpsize - oldsize
oldcount = tmpcount
oldsize = tmpsize
lg.Info("ingest stats", log.KV("eps", cdiff), log.KV("bps", sdiff), log.KV("bytes", oldsize))
}
}()
}
go func(sub *pubsub.Subscription, tagid entry.EntryTag, ps *pubsubconf) {
eChan := make(chan *entry.Entry, 2048)
go func(c chan *entry.Entry) {
for e := range c {
if err := procset.ProcessContext(e, exitCtx); err != nil {
lg.Error("failed to process entry", log.KVErr(err))
}
count++
}
if err := procset.Close(); err != nil {
lg.Error("failed to close processor", log.KVErr(err))
}
}(eChan)
tcfg := timegrinder.Config{
EnableLeftMostSeed: true,
}
tg, err := timegrinder.NewTimeGrinder(tcfg)
if err != nil {
ps.Parse_Time = false
}
if ps.Assume_Local_Timezone {
tg.SetLocalTime()
}
if ps.Timezone_Override != `` {
err = tg.SetTimezone(ps.Timezone_Override)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to set timezone to %v: %v\n", ps.Timezone_Override, err)
return
}
}
var src net.IP
if cfg.Global.Source_Override != `` {
// global override
src = net.ParseIP(cfg.Global.Source_Override)
if src == nil {
lg.Fatal("Global Source-Override is invalid", log.KV("override", cfg.Global.Source_Override))
}
}
for {
callback := func(ctx context.Context, msg *pubsub.Message) {
ent := &entry.Entry{
Data: msg.Data,
Tag: tagid,
SRC: src,
}
size += uint64(len(msg.Data))
if ps.Parse_Time == false {
ent.TS = entry.FromStandard(msg.PublishTime)
} else {
ts, ok, err := tg.Extract(msg.Data)
if !ok || err != nil {
// failed to extract, use the publishtime
ps.Parse_Time = false
ent.TS = entry.FromStandard(msg.PublishTime)
} else {
ent.TS = entry.FromStandard(ts)
}
}
eChan <- ent
msg.Ack()
}
cctx, cancel := context.WithCancel(ctx)
defer cancel()
if err := sub.Receive(cctx, callback); err != nil {
lg.Error("receive failed", log.KVErr(err))
}
}
}(sub, tagid, psv)
}
//register quit signals so we can die gracefully
utils.WaitForQuit()
ib.AnnounceShutdown()
exitFn()
}
func debugout(format string, args ...interface{}) {
if debugOn {
fmt.Printf(format, args...)
}
}