/
manager.go
280 lines (246 loc) · 7.57 KB
/
manager.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package telemetry
import (
"context"
"errors"
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/go-logr/logr"
"github.com/puzpuzpuz/xsync/v2"
"github.com/kong/kubernetes-telemetry/pkg/log"
"github.com/kong/kubernetes-telemetry/pkg/types"
)
type managerErr string
func (e managerErr) Error() string {
return string(e)
}
const (
// ErrManagerAlreadyStarted occurs when a manager has been already started
// and it's attempted to be started again.
ErrManagerAlreadyStarted = managerErr("manager already started")
// ErrCantAddConsumersAfterStart occurs when consumers are tried to be added
// after the manager has been already started.
ErrCantAddConsumersAfterStart = managerErr("can't add consumers after start")
// ErrManagerAlreadyStopped occurs when manager has already been stopped.
ErrManagerAlreadyStopped = managerErr("manager stopped")
)
const (
// DefaultWorkflowTickPeriod is the default tick period with which the manager
// will trigger configured workflows execution.
DefaultWorkflowTickPeriod = 5 * time.Second
)
type manager struct {
// signal is the signal that this manager will send out periodically unless
// overridden via TriggerExecute's parameter.
signal types.Signal
// workflows contains a map of workflows identified by their names
workflows *xsync.MapOf[string, Workflow]
// period defines at what cadence the workflows will be triggered.
// For now, all workflows work on the same cadence, i.e. are triggered at the
// same given, ruled by one timer.
period time.Duration
// consumers is a slice of channels that will consume reports produced by
// execution of workflows.
consumers []chan<- types.SignalReport
chTrigger chan types.Signal
ch chan types.SignalReport
once sync.Once
logger logr.Logger
done chan struct{}
started int32
}
var _ Manager = (*manager)(nil)
// Manager controls and runs workflows which provide telemetry data.
// This telemetry is then send over to consumers. Owners of consumers are
// responsible that they consume the data in a timely manner.
//
// The reports produced by Manager are maps of workflows names - that produced
// their respective reports - to those reports. This way reports from independent
// workflows are enclosed in separate map objects in manager's report.
type Manager interface {
// Start starts the manager. This in turn starts an internal ticker which
// periodically triggers the configured workflows to get the telemetry data
// via the configured providers and to forward that data to consumers.
Start() error
// Stop stops the manager the internal loops.
Stop()
// AddConsumer adds a consumer of telemetry data provided by configured
// workflows' providers.
// AddConsumer(ch chan<- Report) error
AddConsumer(c Consumer) error
// AddWorkflow adds a workflow with providers which will provide telemetry data.
AddWorkflow(Workflow)
// TriggerExecute triggers an execution of all configured workflows, which will gather
// all telemetry data, push it downstream to configured serializers and then
// forward it using the configured forwarders.
// It will use the provided signal name overriding what's configured in the
// Manager.
TriggerExecute(context.Context, types.Signal) error
// Report executes all workflows and returns an aggregated report from those
// workflows.
Report(context.Context) (types.Report, error)
}
// NewManager creates a new manager configured via the provided options.
func NewManager(signal types.Signal, opts ...OptManager) (Manager, error) {
m := &manager{
signal: signal,
workflows: xsync.NewMapOf[Workflow](),
period: DefaultWorkflowTickPeriod,
consumers: []chan<- types.SignalReport{},
chTrigger: make(chan types.Signal),
ch: make(chan types.SignalReport),
logger: defaultLogger(),
done: make(chan struct{}),
}
for _, opt := range opts {
if err := opt(m); err != nil {
return nil, fmt.Errorf("failed to create telemetry manager: %w", err)
}
}
return m, nil
}
// AddWorkflow adds a workflow to manager's workflows.
func (m *manager) AddWorkflow(w Workflow) {
if w == nil {
return
}
m.workflows.Store(w.Name(), w)
}
// Start starts the manager and periodical workflow execution.
func (m *manager) Start() error {
if atomic.LoadInt32(&m.started) > 0 {
return ErrManagerAlreadyStarted
}
m.logger.Info("starting telemetry manager")
go m.workflowsLoop()
go m.consumerLoop()
atomic.StoreInt32(&m.started, 1)
return nil
}
// Stop stops the manager.
func (m *manager) Stop() {
m.logger.Info("stopping telemetry manager")
m.once.Do(func() {
close(m.done)
})
}
// Consumer is an entity that can consume telemetry reports on a channel returned
// by Intake().
type Consumer interface {
Intake() chan<- types.SignalReport
Close()
}
// AddConsumer adds a consumer.
func (m *manager) AddConsumer(c Consumer) error {
// func (m *manager) AddConsumer(ch chan<- Report) error {
if atomic.LoadInt32(&m.started) > 0 {
return ErrCantAddConsumersAfterStart
}
m.consumers = append(m.consumers, c.Intake())
return nil
}
func (m *manager) TriggerExecute(ctx context.Context, signal types.Signal) error {
select {
case <-ctx.Done():
return ctx.Err()
case m.chTrigger <- signal:
return nil
case <-m.done:
return ErrManagerAlreadyStopped
}
}
// workflowsLoop defines a mechanism which periodically loops over all configured
// workflows, executes them to get the telemetry data from provided telemetry
// providers and then sends that telemetry over to consumers.
//
// NOTE: for now there is just one period (and hence only one loop) which means
// that all workflow are executed at the same time.
// If there's enough demand in the future this can be done in a way such that each
// workflow has it's own independent period (and hence an independent timelime).
func (m *manager) workflowsLoop() {
ch := make(chan types.Signal)
go func() {
ticker := time.NewTicker(m.period)
defer ticker.Stop()
for {
select {
case <-m.done:
return
case <-ticker.C:
ch <- m.signal
case signal := <-m.chTrigger:
ch <- signal
}
}
}()
for {
select {
case <-m.done:
return
case signal := <-ch:
ctx, cancel := context.WithTimeout(context.Background(), m.period)
report, err := m.Report(ctx)
if err != nil {
m.logger.V(log.DebugLevel).
WithValues("error", err.Error()).
Info("error executing workflows")
}
// Continue the execution even if we get an error but account for possibility
// of getting nil reports, in which case move on to the next iteration (tick).
if report == nil {
cancel()
continue
}
select {
case m.ch <- types.SignalReport{
Signal: signal,
Report: report,
}:
case <-m.done:
cancel()
break
}
cancel()
}
}
}
// Execute executes all configures workflows and returns an aggregated report
// from all the underlying providers.
func (m *manager) Report(ctx context.Context) (types.Report, error) {
var (
errs []error
report = types.Report{}
)
m.workflows.Range(func(name string, w Workflow) bool {
r, err := w.Execute(ctx)
if err != nil {
errs = append(errs, err)
}
// Add the report regardless if it's partial only omitting empty (nil) reports.
if r != nil {
report[w.Name()] = r
}
return true
})
return report, errors.Join(errs...)
}
// consumerLoop loops over all configured consumers and sends the gathered telemetry
// reports over to them via a channel.
func (m *manager) consumerLoop() {
for {
select {
case <-m.done:
return
case r := <-m.ch:
consumersLoop:
for _, c := range m.consumers {
select {
case c <- r:
case <-m.done:
break consumersLoop
}
}
}
}
}