-
Notifications
You must be signed in to change notification settings - Fork 211
/
sink.go
187 lines (155 loc) · 5.36 KB
/
sink.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
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package stackdriver
import (
"time"
"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
sd "google.golang.org/api/logging/v2"
"k8s.io/apimachinery/pkg/util/clock"
api_v1 "k8s.io/client-go/pkg/api/v1"
)
var (
receivedEntryCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "received_entry_count",
Help: "Number of entries received by the Stackdriver sink",
Subsystem: "stackdriver_sink",
},
[]string{"component"},
)
successfullySentEntryCount = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "successfully_sent_entry_count",
Help: "Number of entries successfully ingested by Stackdriver",
Subsystem: "stackdriver_sink",
},
)
)
type sdSink struct {
logEntryChannel chan *sd.LogEntry
config *sdSinkConfig
logEntryFactory *sdLogEntryFactory
sdResourceFactory *monitoredResourceFactory
writer sdWriter
logName string
currentBuffer []*sd.LogEntry
timer *time.Timer
fakeTimeChannel chan time.Time
// Channel for controlling how many requests are being sent at the same
// time. It's empty initially, each request adds an object at the start
// and takes it out upon completion. Channel's capacity is set to the
// maximum level of parallelism, so any extra request will lock on addition.
concurrencyChannel chan struct{}
beforeFirstList bool
}
func newSdSink(writer sdWriter, clock clock.Clock, config *sdSinkConfig, factory *monitoredResourceFactory) *sdSink {
return &sdSink{
logEntryChannel: make(chan *sd.LogEntry, config.MaxBufferSize),
config: config,
logEntryFactory: newSdLogEntryFactory(clock, factory),
sdResourceFactory: factory,
writer: writer,
logName: config.LogName,
currentBuffer: []*sd.LogEntry{},
timer: nil,
fakeTimeChannel: make(chan time.Time),
concurrencyChannel: make(chan struct{}, config.MaxConcurrency),
beforeFirstList: true,
}
}
func (s *sdSink) OnAdd(event *api_v1.Event) {
receivedEntryCount.WithLabelValues(event.Source.Component).Inc()
logEntry := s.logEntryFactory.FromEvent(event)
s.logEntryChannel <- logEntry
}
func (s *sdSink) OnUpdate(oldEvent *api_v1.Event, newEvent *api_v1.Event) {
var oldCount int32
if oldEvent != nil {
oldCount = oldEvent.Count
}
if newEvent.Count != oldCount+1 {
// Sink doesn't send a LogEntry to Stackdriver, b/c event compression might
// indicate that part of the watch history was lost, which may result in
// multiple events being compressed. This may create an unecessary
// flood in Stackdriver. Also this is a perfectly valid behavior for the
// configuration with empty backing storage.
glog.V(2).Infof("Event count has increased by %d != 1.\n"+
"\tOld event: %+v\n\tNew event: %+v", newEvent.Count-oldCount, oldEvent, newEvent)
}
receivedEntryCount.WithLabelValues(newEvent.Source.Component).Inc()
logEntry := s.logEntryFactory.FromEvent(newEvent)
s.logEntryChannel <- logEntry
}
func (s *sdSink) OnDelete(*api_v1.Event) {
// Nothing to do here
}
func (s *sdSink) OnList(list *api_v1.EventList) {
if s.beforeFirstList {
entry := s.logEntryFactory.FromMessage("Event exporter started watching. " +
"Some events may have been lost up to this point.")
s.writer.Write([]*sd.LogEntry{entry}, s.logName, s.sdResourceFactory.defaultMonitoredResource())
s.beforeFirstList = false
}
}
func (s *sdSink) Run(stopCh <-chan struct{}) {
glog.Info("Starting Stackdriver sink")
for {
select {
case entry := <-s.logEntryChannel:
s.currentBuffer = append(s.currentBuffer, entry)
if len(s.currentBuffer) >= s.config.MaxBufferSize {
s.flushBuffer()
} else if len(s.currentBuffer) == 1 {
s.setTimer()
}
break
case <-s.getTimerChannel():
s.flushBuffer()
break
case <-stopCh:
glog.Info("Stackdriver sink received stop signal, waiting for all requests to finish")
for i := 0; i < s.config.MaxConcurrency; i++ {
s.concurrencyChannel <- struct{}{}
}
glog.Info("All requests to Stackdriver finished, exiting Stackdriver sink")
return
}
}
}
func (s *sdSink) flushBuffer() {
entries := s.currentBuffer
s.currentBuffer = nil
s.concurrencyChannel <- struct{}{}
go s.sendEntries(entries)
}
func (s *sdSink) sendEntries(entries []*sd.LogEntry) {
glog.V(4).Infof("Sending %d entries to Stackdriver", len(entries))
written := s.writer.Write(entries, s.logName, s.sdResourceFactory.defaultMonitoredResource())
successfullySentEntryCount.Add(float64(written))
<-s.concurrencyChannel
glog.V(4).Infof("Successfully sent %d entries to Stackdriver", len(entries))
}
func (s *sdSink) getTimerChannel() <-chan time.Time {
if s.timer == nil {
return s.fakeTimeChannel
}
return s.timer.C
}
func (s *sdSink) setTimer() {
if s.timer == nil {
s.timer = time.NewTimer(s.config.FlushDelay)
} else {
s.timer.Reset(s.config.FlushDelay)
}
}