/
dropped_counter.go
143 lines (122 loc) · 3.77 KB
/
dropped_counter.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
package batch
import (
"fmt"
"metron/config"
"sync"
"sync/atomic"
"time"
"github.com/cloudfoundry/dropsonde/envelope_extensions"
"github.com/cloudfoundry/sonde-go/events"
"github.com/gogo/protobuf/proto"
)
var metSourceType = proto.String("MET")
//go:generate hel --type BatchCounterIncrementer --output mock_batch_counter_incrementer_test.go
type BatchCounterIncrementer interface {
BatchIncrementCounter(name string)
}
type DroppedCounter struct {
conf *config.Config
origin string
ip string
deltaDropped int64
totalDropped int64
writer BatchChainByteWriter
incrementer BatchCounterIncrementer
timer *time.Timer
timerResetLock sync.Mutex
}
func NewDroppedCounter(byteWriter BatchChainByteWriter, incrementer BatchCounterIncrementer, origin, ip string, conf *config.Config) *DroppedCounter {
counter := &DroppedCounter{
origin: origin,
ip: ip,
conf: conf,
writer: byteWriter,
incrementer: incrementer,
timer: time.NewTimer(time.Second),
}
counter.timer.Stop()
go counter.run()
return counter
}
func (d *DroppedCounter) Drop(n uint32) {
defer func() {
d.timerResetLock.Lock()
defer d.timerResetLock.Unlock()
d.timer.Reset(time.Millisecond)
}()
atomic.AddInt64(&d.deltaDropped, int64(n))
atomic.AddInt64(&d.totalDropped, int64(n))
}
func (d *DroppedCounter) Dropped() int64 {
return atomic.LoadInt64(&d.deltaDropped)
}
func (d *DroppedCounter) run() {
for range d.timer.C {
d.sendDroppedMessages()
}
}
func (d *DroppedCounter) sendDroppedMessages() {
droppedCount := d.Dropped()
if droppedCount == 0 {
return
}
totalDropped := atomic.LoadInt64(&d.totalDropped)
bytes := append(d.encodeCounterEvent(droppedCount, totalDropped), d.encodeLogMessage(droppedCount)...)
if _, err := d.writer.Write(bytes); err != nil {
d.incrementer.BatchIncrementCounter("droppedCounter.sendErrors")
d.timerResetLock.Lock()
defer d.timerResetLock.Unlock()
d.timer.Reset(time.Millisecond)
return
}
atomic.AddInt64(&d.deltaDropped, -droppedCount)
}
func (d *DroppedCounter) encodeCounterEvent(droppedCount, totalDropped int64) []byte {
message := &events.Envelope{
Origin: proto.String(d.origin),
Timestamp: proto.Int64(time.Now().UnixNano()),
Ip: proto.String(d.ip),
Deployment: proto.String(d.conf.Deployment),
Index: proto.String(d.conf.Index),
Job: proto.String(d.conf.Job),
EventType: events.Envelope_CounterEvent.Enum(),
CounterEvent: &events.CounterEvent{
Name: proto.String("DroppedCounter.droppedMessageCount"),
Delta: proto.Uint64(uint64(droppedCount)),
Total: proto.Uint64(uint64(totalDropped)),
},
}
bytes, err := message.Marshal()
if err != nil {
d.incrementer.BatchIncrementCounter("droppedCounter.sendErrors")
d.timer.Reset(time.Millisecond)
return nil
}
return prefixMessage(bytes)
}
func (d *DroppedCounter) encodeLogMessage(droppedCount int64) []byte {
now := time.Now()
message := &events.Envelope{
Origin: proto.String(d.origin),
Timestamp: proto.Int64(now.UnixNano()),
Ip: proto.String(d.ip),
Deployment: proto.String(d.conf.Deployment),
Index: proto.String(d.conf.Index),
Job: proto.String(d.conf.Job),
EventType: events.Envelope_LogMessage.Enum(),
LogMessage: &events.LogMessage{
MessageType: events.LogMessage_ERR.Enum(),
Timestamp: proto.Int64(now.UnixNano()),
AppId: proto.String(envelope_extensions.SystemAppId),
Message: []byte(fmt.Sprintf("Dropped %d message(s) from MetronAgent to Doppler", droppedCount)),
SourceType: metSourceType,
},
}
bytes, err := message.Marshal()
if err != nil {
d.incrementer.BatchIncrementCounter("droppedCounter.sendErrors")
d.timer.Reset(time.Millisecond)
return nil
}
return prefixMessage(bytes)
}