-
Notifications
You must be signed in to change notification settings - Fork 7
/
measurements_batching.go
216 lines (200 loc) · 7.69 KB
/
measurements_batching.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
package appoptics
import (
"fmt"
"time"
log "github.com/sirupsen/logrus"
)
// MeasurementsBatch is a collection of Measurements persisted to the API at the same time.
// It can optionally have tags that are applied to all contained Measurements.
type MeasurementsBatch struct {
// Measurements is the collection of timeseries entries being sent to the server
Measurements []Measurement `json:"measurements,omitempty"`
// Period is a slice of time measured in seconds, used in service-side aggregation
Period int64 `json:"period,omitempty"`
// Time is a Unix epoch timestamp used to align a group of Measurements on a time boundary
Time int64 `json:"time"`
// Tags are key-value identifiers that will be applied to all Measurements in the batch
Tags *map[string]string `json:"tags,omitempty"`
}
// BatchPersister implements persistence to AppOptics and enforces error limits
type BatchPersister struct {
// mc is the MeasurementsCommunicator used to talk to the AppOptics API
mc MeasurementsCommunicator
// errors holds the errors received in attempting to persist to AppOptics
errors []error
// errorLimit is the number of persistence errors that will be tolerated
errorLimit int
// prepChan is a channel of Measurements slices
prepChan chan []Measurement
// batchChan is used to create MeasurementsBatches for persistence to AppOptics
batchChan chan *MeasurementsBatch
// stopBatchingChan is used to cease persisting MeasurementsBatches to AppOptics
stopBatchingChan chan struct{}
// stopPersistingChan is used to cease persisting MeasurementsBatches to AppOptics
stopPersistingChan chan struct{}
// stopErrorChan is used to cease the error checking for/select
stopErrorChan chan struct{}
// errorChan is used to tally errors that occur in batching/persisting
errorChan chan error
// maximumPushInterval is the max time (in milliseconds) to wait before pushing a batch whether its length is equal
// to the MeasurementPostMaxBatchSize or not
maximumPushInterval int
// sendStats is a flag for whether to persist to AppOptics or simply print messages to stdout
sendStats bool
}
// NewBatchPersister sets up a new instance of batched persistence capabilities using the provided MeasurementsCommunicator
func NewBatchPersister(mc MeasurementsCommunicator, sendStats bool) *BatchPersister {
return &BatchPersister{
mc: mc,
errorLimit: DefaultPersistenceErrorLimit,
prepChan: make(chan []Measurement),
batchChan: make(chan *MeasurementsBatch),
stopBatchingChan: make(chan struct{}),
stopErrorChan: make(chan struct{}),
stopPersistingChan: make(chan struct{}),
errorChan: make(chan error),
errors: []error{},
maximumPushInterval: 2000,
sendStats: sendStats,
}
}
func NewMeasurementsBatch(m []Measurement, tags *map[string]string) *MeasurementsBatch {
return &MeasurementsBatch{
Measurements: m,
Tags: tags,
Time: time.Now().UTC().Unix(),
}
}
// MeasurementsSink gives calling code write-only access to the Measurements prep channel
func (bp *BatchPersister) MeasurementsSink() chan<- []Measurement {
return bp.prepChan
}
// MeasurementsStopBatchingChannel gives calling code write-only access to the Measurements batching control channel
func (bp *BatchPersister) MeasurementsStopBatchingChannel() chan<- struct{} {
return bp.stopBatchingChan
}
// MeasurementsErrorChannel gives calling code write-only access to the Measurements error channel
func (bp *BatchPersister) MeasurementsErrorChannel() chan<- error {
return bp.errorChan
}
// MaxiumumPushIntervalMilliseconds returns the number of milliseconds the system will wait before pushing any
// accumulated Measurements to AppOptics
func (bp *BatchPersister) MaximumPushInterval() int {
return bp.maximumPushInterval
}
// SetMaximumPushInterval sets the number of milliseconds the system will wait before pushing any accumulated
// Measurements to AppOptics
func (bp *BatchPersister) SetMaximumPushInterval(ms int) {
bp.maximumPushInterval = ms
}
// batchMeasurements reads slices of Measurements off a channel and packages them into batches conforming to the
// limitations imposed by the API. If Measurements are arriving slowly, collected Measurements will be pushed on an
// interval defined by maximumPushIntervalMilliseconds
func (bp *BatchPersister) batchMeasurements() {
var currentMeasurements = []Measurement{}
ticker := time.NewTicker(time.Millisecond * time.Duration(bp.maximumPushInterval))
LOOP:
for {
select {
case receivedMeasurements := <-bp.prepChan:
currentMeasurements = append(currentMeasurements, receivedMeasurements...)
if len(currentMeasurements) >= MeasurementPostMaxBatchSize {
bp.batchChan <- &MeasurementsBatch{Measurements: currentMeasurements[:MeasurementPostMaxBatchSize]}
currentMeasurements = currentMeasurements[MeasurementPostMaxBatchSize:]
}
case <-ticker.C:
if len(currentMeasurements) > 0 {
pushBatch := &MeasurementsBatch{}
if len(currentMeasurements) >= MeasurementPostMaxBatchSize {
pushBatch.Measurements = currentMeasurements[:MeasurementPostMaxBatchSize]
bp.batchChan <- pushBatch
currentMeasurements = currentMeasurements[MeasurementPostMaxBatchSize:]
} else {
pushBatch.Measurements = currentMeasurements
bp.batchChan <- pushBatch
currentMeasurements = []Measurement{}
}
}
case <-bp.stopBatchingChan:
ticker.Stop()
if len(currentMeasurements) > 0 {
if len(bp.errors) < bp.errorLimit {
bp.batchChan <- &MeasurementsBatch{Measurements: currentMeasurements[:MeasurementPostMaxBatchSize]}
}
}
close(bp.batchChan)
bp.stopPersistingChan <- struct{}{}
bp.stopErrorChan <- struct{}{}
break LOOP
}
}
}
// BatchAndPersistMeasurementsForever continually packages up Measurements from the channel returned by MeasurementSink()
// and persists them to AppOptics.
func (bp *BatchPersister) BatchAndPersistMeasurementsForever() {
go bp.batchMeasurements()
go bp.persistBatches()
go bp.managePersistenceErrors()
}
// persistBatches reads maximal slices of Measurements off a channel and persists them to the remote AppOptics
// API. Errors are placed on the error channel.
func (bp *BatchPersister) persistBatches() {
ticker := time.NewTicker(time.Millisecond * 500)
LOOP:
for {
select {
case <-ticker.C:
batch := <-bp.batchChan
if batch != nil {
err := bp.persistBatch(batch)
if err != nil {
bp.errorChan <- err
}
}
case <-bp.stopPersistingChan:
if len(bp.errors) > bp.errorLimit {
batch := <-bp.batchChan
if batch != nil {
bp.persistBatch(batch)
}
}
ticker.Stop()
break LOOP
}
}
}
// managePersistenceErrors tracks errors on the provided channel and sends a stop signal if the ErrorLimit is reached.
func (bp *BatchPersister) managePersistenceErrors() {
LOOP:
for {
select {
case err := <-bp.errorChan:
bp.errors = append(bp.errors, err)
if len(bp.errors) == bp.errorLimit {
bp.stopBatchingChan <- struct{}{}
break LOOP
}
case <-bp.stopErrorChan:
break LOOP
}
}
}
// persistBatch sends to the remote AppOptics endpoint unless config.SendStats() returns false, when it prints to stdout
func (bp *BatchPersister) persistBatch(batch *MeasurementsBatch) error {
if bp.sendStats {
// TODO: make this conditional upon log level
log.Printf("persisting %d Measurements to AppOptics\n", len(batch.Measurements))
resp, err := bp.mc.Create(batch)
if resp == nil {
fmt.Println("response is nil")
return err
}
// TODO: make this conditional upon log level
dumpResponse(resp)
} else {
// TODO: make this more verbose upon log level
log.Printf("received %d Measurements for persistence\n", len(batch.Measurements))
//printMeasurements(batch.Measurements)
}
return nil
}