forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs.go
152 lines (126 loc) · 4.13 KB
/
logs.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
package api
import (
"cf/configuration"
"cf/net"
"cf/terminal"
"cf/trace"
"code.google.com/p/go.net/websocket"
"crypto/tls"
"errors"
"fmt"
"github.com/cloudfoundry/loggregatorlib/logmessage"
"time"
)
const LogBufferSize = 1024
type LogsRepository interface {
RecentLogsFor(appGuid string, onConnect func(), logChan chan *logmessage.Message) (err error)
TailLogsFor(appGuid string, onConnect func(), logChan chan *logmessage.Message, stopLoggingChan chan bool, printInterval time.Duration) (err error)
}
type LoggregatorLogsRepository struct {
config configuration.Reader
endpointRepo EndpointRepository
TrustedCerts []tls.Certificate
}
func NewLoggregatorLogsRepository(config configuration.Reader, endpointRepo EndpointRepository) LoggregatorLogsRepository {
return LoggregatorLogsRepository{
config: config,
endpointRepo: endpointRepo,
}
}
func (repo LoggregatorLogsRepository) RecentLogsFor(appGuid string, onConnect func(), logChan chan *logmessage.Message) (err error) {
host, apiErr := repo.endpointRepo.GetLoggregatorEndpoint()
if apiErr != nil {
err = errors.New(apiErr.Error())
return
}
location := fmt.Sprintf("%s/dump/?app=%s", host, appGuid)
stopLoggingChan := make(chan bool)
defer close(stopLoggingChan)
return repo.connectToWebsocket(location, onConnect, logChan, stopLoggingChan, 0*time.Nanosecond)
}
func (repo LoggregatorLogsRepository) TailLogsFor(appGuid string, onConnect func(), logChan chan *logmessage.Message, stopLoggingChan chan bool, printTimeBuffer time.Duration) error {
host, apiErr := repo.endpointRepo.GetLoggregatorEndpoint()
if apiErr != nil {
return errors.New(apiErr.Error())
}
location := host + fmt.Sprintf("/tail/?app=%s", appGuid)
return repo.connectToWebsocket(location, onConnect, logChan, stopLoggingChan, printTimeBuffer)
}
func (repo LoggregatorLogsRepository) connectToWebsocket(location string, onConnect func(), outputChan chan *logmessage.Message, stopLoggingChan chan bool, printTimeBuffer time.Duration) (err error) {
trace.Logger.Printf("\n%s %s\n", terminal.HeaderColor("CONNECTING TO WEBSOCKET:"), location)
inputChan := make(chan *logmessage.Message, LogBufferSize)
messageQueue := NewSortedMessageQueue(printTimeBuffer, time.Now)
wsConfig, err := websocket.NewConfig(location, "http://localhost")
if err != nil {
return
}
wsConfig.Header.Add("Authorization", repo.config.AccessToken())
wsConfig.TlsConfig = net.TLSConfigWithTrustedCerts(repo.TrustedCerts)
ws, err := websocket.DialConfig(wsConfig)
if err != nil {
return
}
defer func() {
ws.Close()
repo.drainRemainingMessages(messageQueue, inputChan, outputChan)
}()
onConnect()
go repo.sendKeepAlive(ws)
go func() {
defer close(inputChan)
repo.listenForMessages(ws, inputChan)
}()
repo.processMessages(messageQueue, inputChan, outputChan, stopLoggingChan)
return
}
func (repo LoggregatorLogsRepository) processMessages(messageQueue *SortedMessageQueue, inputChan <-chan *logmessage.Message, outputChan chan *logmessage.Message, stopLoggingChan <-chan bool) {
for {
select {
case msg, ok := <-inputChan:
if ok {
messageQueue.PushMessage(msg)
} else {
return
}
case <-stopLoggingChan:
return
case <-time.After(10 * time.Millisecond):
for messageQueue.NextTimestamp() < time.Now().UnixNano() {
msg := messageQueue.PopMessage()
outputChan <- msg
}
}
}
}
func (repo LoggregatorLogsRepository) drainRemainingMessages(messageQueue *SortedMessageQueue, inputChan <-chan *logmessage.Message, outputChan chan *logmessage.Message) {
for msg := range inputChan {
messageQueue.PushMessage(msg)
}
for {
msg := messageQueue.PopMessage()
if msg == nil {
break
}
outputChan <- msg
}
}
func (repo LoggregatorLogsRepository) sendKeepAlive(ws *websocket.Conn) {
for {
websocket.Message.Send(ws, "I'm alive!")
time.Sleep(25 * time.Second)
}
}
func (repo LoggregatorLogsRepository) listenForMessages(ws *websocket.Conn, msgChan chan<- *logmessage.Message) {
for {
var data []byte
err := websocket.Message.Receive(ws, &data)
if err != nil {
break
}
msg, msgErr := logmessage.ParseMessage(data)
if msgErr != nil {
continue
}
msgChan <- msg
}
}