forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logs_noaa.go
168 lines (142 loc) · 4.38 KB
/
logs_noaa.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
package api
import (
"errors"
"sync"
"time"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/api/authentication"
"github.com/cloudfoundry/cli/cf/configuration/core_config"
"github.com/cloudfoundry/cli/cf/models"
"github.com/cloudfoundry/noaa"
noaa_errors "github.com/cloudfoundry/noaa/errors"
"github.com/cloudfoundry/sonde-go/events"
)
type LogsNoaaRepository interface {
GetContainerMetrics(string, []models.AppInstanceFields) ([]models.AppInstanceFields, error)
RecentLogsFor(appGuid string) ([]*events.LogMessage, error)
TailNoaaLogsFor(appGuid string, onConnect func(), onMessage func(*events.LogMessage)) error
Close()
}
type logNoaaRepository struct {
config core_config.Reader
consumer NoaaConsumer
tokenRefresher authentication.TokenRefresher
messageQueue *SortedMessageQueue
onMessage func(*events.LogMessage)
doneChan chan struct{}
tailing bool
mutexLock sync.Mutex
}
var BufferTime time.Duration = 5 * time.Second
func NewLogsNoaaRepository(config core_config.Reader, consumer NoaaConsumer, tr authentication.TokenRefresher) LogsNoaaRepository {
return &logNoaaRepository{
config: config,
consumer: consumer,
tokenRefresher: tr,
messageQueue: NewSortedMessageQueue(BufferTime, time.Now),
}
}
func (l *logNoaaRepository) Close() {
l.mutexLock.Lock()
defer l.mutexLock.Unlock()
l.tailing = false
l.flushMessageQueue()
close(l.doneChan)
}
func (l *logNoaaRepository) GetContainerMetrics(appGuid string, instances []models.AppInstanceFields) ([]models.AppInstanceFields, error) {
metrics, err := l.consumer.GetContainerMetrics(appGuid, l.config.AccessToken())
switch err.(type) {
case nil: // do nothing
case *noaa_errors.UnauthorizedError:
l.tokenRefresher.RefreshAuthToken()
metrics, err = l.consumer.GetContainerMetrics(appGuid, l.config.AccessToken())
default:
return instances, err
}
for _, m := range metrics {
instances[int(*m.InstanceIndex)].MemUsage = int64(m.GetMemoryBytes())
instances[int(*m.InstanceIndex)].CpuUsage = m.GetCpuPercentage()
instances[int(*m.InstanceIndex)].DiskUsage = int64(m.GetDiskBytes())
}
return instances, nil
}
func (l *logNoaaRepository) RecentLogsFor(appGuid string) ([]*events.LogMessage, error) {
logs, err := l.consumer.RecentLogs(appGuid, l.config.AccessToken())
switch err.(type) {
case nil: // do nothing
case *noaa_errors.UnauthorizedError:
l.tokenRefresher.RefreshAuthToken()
logs, err = l.consumer.RecentLogs(appGuid, l.config.AccessToken())
default:
return logs, err
}
return noaa.SortRecent(logs), err
}
func (l *logNoaaRepository) TailNoaaLogsFor(appGuid string, onConnect func(), onMessage func(*events.LogMessage)) error {
l.mutexLock.Lock()
var hasReauthed bool
l.doneChan = make(chan struct{})
l.tailing = true
l.onMessage = onMessage
l.mutexLock.Unlock()
endpoint := l.config.DopplerEndpoint()
if endpoint == "" {
return errors.New(T("Loggregator endpoint missing from config file"))
}
l.consumer.SetOnConnectCallback(onConnect)
logChan := make(chan *events.LogMessage)
errChan := make(chan error)
go l.consumer.TailingLogs(appGuid, l.config.AccessToken(), logChan, errChan)
for {
sendNoaaMessages(l.messageQueue, onMessage)
select {
case <-l.doneChan:
l.consumer.Close()
return nil
case err := <-errChan:
switch err.(type) {
case nil: // do nothing
case *noaa_errors.UnauthorizedError:
if !hasReauthed {
l.tokenRefresher.RefreshAuthToken()
hasReauthed = true
l.consumer.Close()
time.Sleep(100 * time.Millisecond) //wait a little before retrying
go l.consumer.TailingLogs(appGuid, l.config.AccessToken(), logChan, errChan)
} else {
l.consumer.Close()
return err
}
default:
if !l.tailing { //"use of closed network connection" is expected since we closed the websocket connection
return nil
} else {
return err
}
}
case log := <-logChan:
l.messageQueue.PushMessage(log)
default:
time.Sleep(10 * time.Millisecond)
}
}
}
func sendNoaaMessages(queue *SortedMessageQueue, onMessage func(*events.LogMessage)) {
for queue.NextTimestamp() < time.Now().UnixNano() {
msg := queue.PopMessage()
onMessage(msg)
}
}
func (l *logNoaaRepository) flushMessageQueue() {
if l.onMessage == nil {
return
}
for {
message := l.messageQueue.PopMessage()
if message == nil {
break
}
l.onMessage(message)
}
l.onMessage = nil
}