-
Notifications
You must be signed in to change notification settings - Fork 793
/
events.go
233 lines (193 loc) · 5.12 KB
/
events.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package tracing
import (
"sync"
"sync/atomic"
"github.com/benthosdev/benthos/v4/internal/message"
)
// EventType describes the type of event a component might experience during
// a config run.
type EventType string
// Various event types.
var (
EventProduce EventType = "PRODUCE"
EventConsume EventType = "CONSUME"
EventDelete EventType = "DELETE"
EventError EventType = "ERROR"
)
// NodeEvent represents a single event that occurred within the stream.
type NodeEvent struct {
Type EventType
Content string
Meta map[string]any
}
// EventProduceOf creates a produce event from a message part.
func EventProduceOf(part *message.Part) NodeEvent {
meta := map[string]any{}
_ = part.MetaIterMut(func(s string, a any) error {
meta[s] = message.CopyJSON(a)
return nil
})
return NodeEvent{
Type: EventProduce,
Content: string(part.AsBytes()),
Meta: meta,
}
}
// EventConsumeOf creates a consumed event from a message part.
func EventConsumeOf(part *message.Part) NodeEvent {
meta := map[string]any{}
_ = part.MetaIterMut(func(s string, a any) error {
meta[s] = message.CopyJSON(a)
return nil
})
return NodeEvent{
Type: EventConsume,
Content: string(part.AsBytes()),
Meta: meta,
}
}
// EventDeleteOf creates a deleted event from a message part.
func EventDeleteOf() NodeEvent {
return NodeEvent{
Type: EventDelete,
}
}
// EventErrorOf creates an error event from a message part.
func EventErrorOf(err error) NodeEvent {
return NodeEvent{
Type: EventError,
Content: err.Error(),
}
}
type control struct {
isEnabled int32
eventLimit int64
}
func (c *control) SetEnabled(e bool) {
if e {
atomic.StoreInt32(&c.isEnabled, 1)
} else {
atomic.StoreInt32(&c.isEnabled, 0)
}
}
func (c *control) SetEventLimit(n int64) {
atomic.StoreInt64(&c.eventLimit, n)
}
func (c *control) IsEnabled() bool {
return atomic.LoadInt32(&c.isEnabled) > 0
}
func (c *control) EventLimit() int64 {
return atomic.LoadInt64(&c.eventLimit)
}
// Summary is a high level description of all traced events.
type Summary struct {
Input uint64
Output uint64
ProcessorErrors uint64
ctrl *control
inputEvents sync.Map
processorEvents sync.Map
outputEvents sync.Map
}
// NewSummary creates a new tracing summary that can be passed to component
// constructors for adding traces.
func NewSummary() *Summary {
return &Summary{
ctrl: &control{isEnabled: 1},
}
}
// SetEnabled sets whether tracing events are enabled across the stream.
func (s *Summary) SetEnabled(e bool) {
s.ctrl.SetEnabled(e)
}
// SetEventLimit sets a limit as to how many event traces are stored, this limit
// is per component that's traced.
func (s *Summary) SetEventLimit(n int64) {
s.ctrl.SetEventLimit(n)
}
func getEvents(flush bool, from *sync.Map) map[string][]NodeEvent {
m := map[string][]NodeEvent{}
from.Range(func(key, value any) bool {
e := value.(*events)
var extracted []NodeEvent
if flush {
extracted = e.Flush()
} else {
extracted = e.Extract()
}
m[key.(string)] = extracted
return true
})
return m
}
// InputEvents returns a map of input labels to events traced during the
// execution of a stream pipeline. Set flush to true in order to clear the
// events after obtaining them.
func (s *Summary) InputEvents(flush bool) map[string][]NodeEvent {
return getEvents(flush, &s.inputEvents)
}
// ProcessorEvents returns a map of processor labels to events traced during the
// execution of a stream pipeline.
func (s *Summary) ProcessorEvents(flush bool) map[string][]NodeEvent {
return getEvents(flush, &s.processorEvents)
}
// OutputEvents returns a map of output labels to events traced during the
// execution of a stream pipeline.
func (s *Summary) OutputEvents(flush bool) map[string][]NodeEvent {
return getEvents(flush, &s.outputEvents)
}
//------------------------------------------------------------------------------
func (s *Summary) wInputEvents(label string) (e *events, counter *uint64) {
i, _ := s.inputEvents.LoadOrStore(label, &events{
ctrl: s.ctrl,
})
return i.(*events), &s.Input
}
func (s *Summary) wOutputEvents(label string) (e *events, counter *uint64) {
i, _ := s.outputEvents.LoadOrStore(label, &events{
ctrl: s.ctrl,
})
return i.(*events), &s.Output
}
func (s *Summary) wProcessorEvents(label string) (e *events, errCounter *uint64) {
i, _ := s.processorEvents.LoadOrStore(label, &events{
ctrl: s.ctrl,
})
return i.(*events), &s.ProcessorErrors
}
type events struct {
mut sync.Mutex
m []NodeEvent
mLen int64
ctrl *control
}
func (e *events) IsEnabled() bool {
if !e.ctrl.IsEnabled() {
return false
}
if limit := e.ctrl.EventLimit(); limit > 0 {
return atomic.LoadInt64(&e.mLen) < limit
}
return true
}
func (e *events) Add(event NodeEvent) {
e.mut.Lock()
defer e.mut.Unlock()
atomic.AddInt64(&e.mLen, 1)
e.m = append(e.m, event)
}
func (e *events) Extract() []NodeEvent {
e.mut.Lock()
defer e.mut.Unlock()
eventsCopy := make([]NodeEvent, len(e.m))
copy(eventsCopy, e.m)
return eventsCopy
}
func (e *events) Flush() []NodeEvent {
e.mut.Lock()
defer e.mut.Unlock()
tmpEvents := e.m
e.m = nil
atomic.StoreInt64(&e.mLen, 0)
return tmpEvents
}