forked from cloudfoundry/cli
/
consumer.go
412 lines (330 loc) · 11.5 KB
/
consumer.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Package loggregator_consumer provides a simple, channel-based API for clients to communicate with
// loggregator servers.
package loggregator_consumer
import (
"bufio"
"bytes"
"code.google.com/p/gogoprotobuf/proto"
"crypto/tls"
"errors"
"fmt"
"github.com/cloudfoundry/loggregator_consumer/noaa_errors"
"github.com/cloudfoundry/loggregatorlib/logmessage"
"github.com/gorilla/websocket"
"io/ioutil"
"mime/multipart"
"net"
"net/http"
"net/url"
"regexp"
"sort"
"strings"
"time"
)
var (
// KeepAlive sets the interval between keep-alive messages sent by the client to loggregator.
KeepAlive = 25 * time.Second
boundaryRegexp = regexp.MustCompile("boundary=(.*)")
ErrNotFound = errors.New("/recent path not found or has issues")
ErrBadResponse = errors.New("bad server response")
ErrBadRequest = errors.New("bad client request")
)
/* LoggregatorConsumer represents the actions that can be performed against a loggregator server.
*/
type LoggregatorConsumer interface {
// Tail listens indefinitely for log messages. It returns two channels; the first is populated
// with log messages, while the second contains errors (e.g. from parsing messages). It returns
// immediately. Call Close() to terminate the connection when you are finished listening.
//
// Messages are presented in the order received from the loggregator server. Chronological or
// other ordering is not guaranteed. It is the responsibility of the consumer of these channels
// to provide any desired sorting mechanism.
Tail(appGuid string, authToken string) (<-chan *logmessage.LogMessage, error)
// Recent connects to loggregator via its 'recent' endpoint and returns a slice of recent messages.
// It does not guarantee any order of the messages; they are in the order returned by loggregator.
//
// The SortRecent method is provided to sort the data returned by this method.
Recent(appGuid string, authToken string) ([]*logmessage.LogMessage, error)
// Close terminates the websocket connection to loggregator.
Close() error
// SetOnConnectCallback sets a callback function to be called with the websocket connection is established.
SetOnConnectCallback(func())
// SetDebugPrinter enables logging of the websocket handshake
SetDebugPrinter(DebugPrinter)
}
type DebugPrinter interface {
Print(title, dump string)
}
type nullDebugPrinter struct {
}
func (nullDebugPrinter) Print(title, body string) {
}
type consumer struct {
endpoint string
tlsConfig *tls.Config
ws *websocket.Conn
callback func()
proxy func(*http.Request) (*url.URL, error)
debugPrinter DebugPrinter
}
/* New creates a new consumer to a loggregator endpoint.
*/
func New(endpoint string, tlsConfig *tls.Config, proxy func(*http.Request) (*url.URL, error)) LoggregatorConsumer {
return &consumer{endpoint: endpoint, tlsConfig: tlsConfig, proxy: proxy, debugPrinter: nullDebugPrinter{}}
}
/* SetDebugPrinter enables logging of the websocket handshake
*/
func (cnsmr *consumer) SetDebugPrinter(debugPrinter DebugPrinter) {
cnsmr.debugPrinter = debugPrinter
}
/*
Tail listens indefinitely for log messages. It returns two channels; the first is populated
with log messages, while the second contains errors (e.g. from parsing messages). It returns immediately.
Call Close() to terminate the connection when you are finished listening.
Messages are presented in the order received from the loggregator server. Chronological or other ordering
is not guaranteed. It is the responsibility of the consumer of these channels to provide any desired sorting
mechanism.
*/
func (cnsmr *consumer) Tail(appGuid string, authToken string) (<-chan *logmessage.LogMessage, error) {
incomingChan := make(chan *logmessage.LogMessage)
var err error
tailPath := fmt.Sprintf("/tail/?app=%s", appGuid)
cnsmr.ws, err = cnsmr.establishWebsocketConnection(tailPath, authToken)
if err == nil {
go cnsmr.sendKeepAlive(KeepAlive)
go func() {
defer close(incomingChan)
cnsmr.listenForMessages(incomingChan)
}()
}
return incomingChan, err
}
/*
Recent connects to loggregator via its 'recent' http(s) endpoint and returns a slice of recent messages.
If the new http 'recent' endpoint isn't supported (ie you are connecting to an older loggregator server),
we will fallback to the old Websocket 'dump' endpoint.
It does not guarantee any order of the messages; they are in the order returned by loggregator.
The SortRecent method is provided to sort the data returned by this method.
*/
func (cnsmr *consumer) Recent(appGuid string, authToken string) ([]*logmessage.LogMessage, error) {
messages, err := cnsmr.httpRecent(appGuid, authToken)
if err != ErrBadRequest {
return messages, err
} else {
return cnsmr.dump(appGuid, authToken)
}
}
/*
httpRecent connects to loggregator via its 'recent' http(s) endpoint and returns a slice of recent messages.
It does not guarantee any order of the messages; they are in the order returned by loggregator.
*/
func (cnsmr *consumer) httpRecent(appGuid string, authToken string) ([]*logmessage.LogMessage, error) {
endpointUrl, err := url.ParseRequestURI(cnsmr.endpoint)
if err != nil {
return nil, err
}
scheme := "https"
if endpointUrl.Scheme == "ws" {
scheme = "http"
}
recentPath := fmt.Sprintf("%s://%s/recent?app=%s", scheme, endpointUrl.Host, appGuid)
transport := &http.Transport{Proxy: cnsmr.proxy, TLSClientConfig: cnsmr.tlsConfig}
client := &http.Client{Transport: transport}
req, _ := http.NewRequest("GET", recentPath, nil)
req.Header.Set("Authorization", authToken)
resp, err := client.Do(req)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error dialing loggregator server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (loggregator endpoint is %s).", err.Error(), cnsmr.endpoint))
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusUnauthorized {
data, _ := ioutil.ReadAll(resp.Body)
return nil, noaa_errors.NewUnauthorizedError(string(data))
}
if resp.StatusCode == http.StatusBadRequest {
return nil, ErrBadRequest
}
if resp.StatusCode != http.StatusOK {
return nil, ErrNotFound
}
contentType := resp.Header.Get("Content-Type")
if len(strings.TrimSpace(contentType)) == 0 {
return nil, ErrBadResponse
}
matches := boundaryRegexp.FindStringSubmatch(contentType)
if len(matches) != 2 || len(strings.TrimSpace(matches[1])) == 0 {
return nil, ErrBadResponse
}
reader := multipart.NewReader(resp.Body, matches[1])
var buffer bytes.Buffer
messages := make([]*logmessage.LogMessage, 0, 200)
for part, loopErr := reader.NextPart(); loopErr == nil; part, loopErr = reader.NextPart() {
buffer.Reset()
msg := new(logmessage.LogMessage)
_, err := buffer.ReadFrom(part)
if err != nil {
break
}
proto.Unmarshal(buffer.Bytes(), msg)
messages = append(messages, msg)
}
return messages, err
}
/*
dump connects to loggregator via its 'dump' ws(s) endpoint and returns a slice of recent messages. It does not
guarantee any order of the messages; they are in the order returned by loggregator.
The SortRecent method is provided to sort the data returned by this method.
*/
func (cnsmr *consumer) dump(appGuid string, authToken string) ([]*logmessage.LogMessage, error) {
var err error
dumpPath := fmt.Sprintf("/dump/?app=%s", appGuid)
cnsmr.ws, err = cnsmr.establishWebsocketConnection(dumpPath, authToken)
if err != nil {
return nil, err
}
messages := []*logmessage.LogMessage{}
messageChan := make(chan *logmessage.LogMessage)
go func() {
err = cnsmr.listenForMessages(messageChan)
close(messageChan)
}()
drainLoop:
for {
select {
case msg, ok := <-messageChan:
if !ok {
break drainLoop
}
messages = append(messages, msg)
}
}
return messages, nil
}
/* Close terminates the websocket connection to loggregator.
*/
func (cnsmr *consumer) Close() error {
if cnsmr.ws == nil {
return errors.New("connection does not exist")
}
return cnsmr.ws.Close()
}
func (cnsmr *consumer) SetOnConnectCallback(cb func()) {
cnsmr.callback = cb
}
/*
SortRecent sorts a slice of LogMessages by timestamp. The sort is stable, so
messages with the same timestamp are sorted in the order that they are received.
The input slice is sorted; the return value is simply a pointer to the same slice.
*/
func SortRecent(messages []*logmessage.LogMessage) []*logmessage.LogMessage {
sort.Stable(logMessageSlice(messages))
return messages
}
type logMessageSlice []*logmessage.LogMessage
func (lms logMessageSlice) Len() int {
return len(lms)
}
func (lms logMessageSlice) Less(i, j int) bool {
return *(lms[i]).Timestamp < *(lms[j]).Timestamp
}
func (lms logMessageSlice) Swap(i, j int) {
lms[i], lms[j] = lms[j], lms[i]
}
func (cnsmr *consumer) sendKeepAlive(interval time.Duration) {
for {
err := cnsmr.ws.WriteMessage(websocket.TextMessage, []byte("I'm alive!"))
if err != nil {
return
}
time.Sleep(interval)
}
}
func (cnsmr *consumer) listenForMessages(msgChan chan<- *logmessage.LogMessage) error {
defer cnsmr.ws.Close()
for {
var data []byte
_, data, err := cnsmr.ws.ReadMessage()
if err != nil {
return err
}
msg, msgErr := logmessage.ParseMessage(data)
if msgErr != nil {
continue
}
msgChan <- msg.GetLogMessage()
}
}
func headersString(header http.Header) string {
var result string
for name, values := range header {
result += name + ": " + strings.Join(values, ", ") + "\n"
}
return result
}
func (cnsmr *consumer) establishWebsocketConnection(path string, authToken string) (*websocket.Conn, error) {
header := http.Header{"Origin": []string{"http://localhost"}, "Authorization": []string{authToken}}
dialer := websocket.Dialer{NetDial: cnsmr.proxyDial, TLSClientConfig: cnsmr.tlsConfig}
url := cnsmr.endpoint + path
cnsmr.debugPrinter.Print("WEBSOCKET REQUEST:",
"GET "+path+" HTTP/1.1\n"+
"Host: "+cnsmr.endpoint+"\n"+
"Upgrade: websocket\nConnection: Upgrade\nSec-WebSocket-Version: 13\nSec-WebSocket-Key: [HIDDEN]\n"+
headersString(header))
ws, resp, err := dialer.Dial(url, header)
if resp != nil {
cnsmr.debugPrinter.Print("WEBSOCKET RESPONSE:",
resp.Proto+" "+resp.Status+"\n"+
headersString(resp.Header))
}
if resp != nil && resp.StatusCode == http.StatusUnauthorized {
bodyData, _ := ioutil.ReadAll(resp.Body)
err = noaa_errors.NewUnauthorizedError(string(bodyData))
return ws, err
}
if err == nil && cnsmr.callback != nil {
cnsmr.callback()
}
if err != nil {
return nil, errors.New(fmt.Sprintf("Error dialing loggregator server: %s.\nPlease ask your Cloud Foundry Operator to check the platform configuration (loggregator endpoint is %s).", err.Error(), cnsmr.endpoint))
}
return ws, err
}
func (cnsmr *consumer) proxyDial(network, addr string) (net.Conn, error) {
targetUrl, err := url.Parse("http://" + addr)
if err != nil {
return nil, err
}
proxy := cnsmr.proxy
if proxy == nil {
proxy = http.ProxyFromEnvironment
}
proxyUrl, err := proxy(&http.Request{URL: targetUrl})
if err != nil {
return nil, err
}
if proxyUrl == nil {
return net.Dial(network, addr)
}
proxyConn, err := net.Dial(network, proxyUrl.Host)
if err != nil {
return nil, err
}
connectReq := &http.Request{
Method: "CONNECT",
URL: targetUrl,
Host: targetUrl.Host,
Header: make(http.Header),
}
connectReq.Write(proxyConn)
connectResp, err := http.ReadResponse(bufio.NewReader(proxyConn), connectReq)
if err != nil {
proxyConn.Close()
return nil, err
}
if connectResp.StatusCode != http.StatusOK {
f := strings.SplitN(connectResp.Status, " ", 2)
proxyConn.Close()
return nil, errors.New(f[1])
}
return proxyConn, nil
}