-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
dopplerconsumer.go
206 lines (175 loc) · 4.8 KB
/
dopplerconsumer.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package cloudfoundry
import (
"fmt"
"net/http"
"regexp"
"sync"
"github.com/cloudfoundry-community/go-cfclient"
"github.com/cloudfoundry/noaa/consumer"
"github.com/cloudfoundry/sonde-go/events"
"github.com/elastic/beats/v7/libbeat/logp"
)
type DopplerCallbacks struct {
Log func(evt Event)
Metric func(evt Event)
Error func(evt EventError)
}
type DopplerConsumer struct {
sync.Mutex
subscriptionID string
callbacks DopplerCallbacks
consumer *consumer.Consumer
tokenRefresher consumer.TokenRefresher
log *logp.Logger
wg sync.WaitGroup
stop chan struct{}
started bool
}
func newDopplerConsumer(address string, id string, log *logp.Logger, client *http.Client, tr *TokenRefresher, callbacks DopplerCallbacks) (*DopplerConsumer, error) {
transport, ok := client.Transport.(*http.Transport)
if !ok {
return nil, fmt.Errorf("expected http transport on client")
}
c := consumer.New(address, transport.TLSClientConfig, transport.Proxy)
c.RefreshTokenFrom(tr)
c.SetDebugPrinter(newLogpDebugPrinter(log))
return &DopplerConsumer{
subscriptionID: id,
consumer: c,
tokenRefresher: tr,
callbacks: callbacks,
log: log,
}, nil
}
func (c *DopplerConsumer) Run() {
c.Lock()
defer c.Unlock()
if c.started {
return
}
c.stop = make(chan struct{})
if c.callbacks.Log != nil {
c.wg.Add(1)
go func() {
defer c.wg.Done()
c.logsFirehose()
}()
}
if c.callbacks.Metric != nil {
c.wg.Add(1)
go func() {
defer c.wg.Done()
c.metricsFirehose()
}()
}
c.started = true
}
func (c *DopplerConsumer) logsFirehose() {
c.firehose(c.callbacks.Log, consumer.LogMessages)
}
func (c *DopplerConsumer) metricsFirehose() {
c.firehose(c.callbacks.Metric, consumer.Metrics)
}
func (c *DopplerConsumer) firehose(cb func(evt Event), filter consumer.EnvelopeFilter) {
var msgChan <-chan *events.Envelope
var errChan <-chan error
filterFn := filterNoFilter
if filter == consumer.LogMessages {
// We are interested in more envelopes than the ones obtained when filtering
// by log messages, retrieve them all and filter later.
// If this causes performance or other problems, we will have to investigate
// if it is possible to pass different filters to the firehose url.
filterFn = filterLogs
msgChan, errChan = c.consumer.Firehose(c.subscriptionID, "")
} else {
msgChan, errChan = c.consumer.FilteredFirehose(c.subscriptionID, "", filter)
}
for {
select {
case env := <-msgChan:
if !filterFn(env) {
continue
}
event := EnvelopeToEvent(env)
if event == nil {
c.log.Debugf("Envelope couldn't be converted to event: %+v", env)
continue
}
if evtError, ok := event.(*EventError); ok {
c.reportError(*evtError)
continue
}
cb(event)
case err := <-errChan:
if err != nil {
// This error is an error on the connection, not a cloud foundry
// error envelope. Firehose should be able to reconnect, so just log it.
c.log.Infof("Error received on firehose: %v", err)
}
case <-c.stop:
return
}
}
}
func filterNoFilter(*events.Envelope) bool { return true }
func filterLogs(e *events.Envelope) bool {
if e == nil || e.EventType == nil {
return false
}
switch *e.EventType {
case events.Envelope_HttpStartStop, events.Envelope_LogMessage, events.Envelope_Error:
return true
}
return false
}
func (c *DopplerConsumer) reportError(e EventError) {
if c.callbacks.Error == nil {
c.log.Debugf("No callback for errors, error received: %s", e)
return
}
c.callbacks.Error(e)
}
func (c *DopplerConsumer) Stop() {
c.Lock()
defer c.Unlock()
if !c.started {
return
}
close(c.stop)
err := c.consumer.Close()
if err != nil {
c.log.Errorf("Error while closing doppler consumer: %v", err)
}
c.started = false
}
func (c *DopplerConsumer) Wait() {
c.Stop()
c.wg.Wait()
}
type TokenRefresher struct {
client *cfclient.Client
}
func TokenRefresherFromCfClient(c *cfclient.Client) *TokenRefresher {
return &TokenRefresher{client: c}
}
func (tr *TokenRefresher) RefreshAuthToken() (token string, authError error) {
return tr.client.GetToken()
}
type LogpDebugPrinter struct {
log *logp.Logger
}
func newLogpDebugPrinter(log *logp.Logger) *LogpDebugPrinter {
return &LogpDebugPrinter{log: log}
}
var authorizationHeaderRE = regexp.MustCompile("Authorization: .*\n")
func (p *LogpDebugPrinter) Print(title, dump string) {
if !p.log.IsDebug() {
return
}
// Avoid printing out authorization tokens, Sec-WebSocket-Key is already hidden by the library.
dump = authorizationHeaderRE.ReplaceAllString(dump, "Authorization: [HIDDEN]\n")
p.log.Debugf("%s: %s", title, dump)
}