-
Notifications
You must be signed in to change notification settings - Fork 159
/
controller.go
261 lines (214 loc) · 6.64 KB
/
controller.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
/*
Copyright NetFoundry 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
https://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 trace
import (
"fmt"
"github.com/openziti/channel/v2/trace/pb"
"regexp"
"strings"
)
type SourceType int
const (
SourceTypePipe SourceType = iota
)
type ToggleVerbosity int
const (
ToggleVerbosityNone ToggleVerbosity = iota
ToggleVerbosityMatches
ToggleVerbosityMisses
ToggleVerbosityAll
)
func (val ToggleVerbosity) Show(matched bool) bool {
if matched {
return val == ToggleVerbosityMatches || val == ToggleVerbosityAll
}
return val == ToggleVerbosityMisses || val == ToggleVerbosityAll
}
func GetVerbosity(verbosity trace_pb.TraceToggleVerbosity) ToggleVerbosity {
switch verbosity {
case trace_pb.TraceToggleVerbosity_ReportNone:
return ToggleVerbosityNone
case trace_pb.TraceToggleVerbosity_ReportMatches:
return ToggleVerbosityMatches
case trace_pb.TraceToggleVerbosity_ReportMisses:
return ToggleVerbosityMisses
case trace_pb.TraceToggleVerbosity_ReportAll:
return ToggleVerbosityAll
}
return ToggleVerbosityNone
}
type SourceMatcher interface {
Matches(source string) bool
}
type PipeToggleMatchers struct {
AppMatcher SourceMatcher
PipeMatcher SourceMatcher
}
func NewPipeToggleMatchers(request *trace_pb.TogglePipeTracesRequest) (*PipeToggleMatchers, *ToggleResult) {
result := &ToggleResult{Success: true, Message: &strings.Builder{}}
appRegex, err := regexp.Compile(request.AppRegex)
if err != nil {
result.Success = false
errMsg := fmt.Sprintf("Failed to parse app id regex '%v' with error: %v\n", request.AppRegex, err)
result.Message.WriteString(errMsg)
}
pipeRegex, err := regexp.Compile(request.PipeRegex)
if err != nil {
result.Success = false
errMsg := fmt.Sprintf("Failed to parse pipe id regex '%v' with error: %v\n", request.PipeRegex, err)
result.Message.WriteString(errMsg)
}
appMatcher := NewSourceMatcher(appRegex)
pipeMatcher := NewSourceMatcher(pipeRegex)
return &PipeToggleMatchers{appMatcher, pipeMatcher}, result
}
type ToggleResult struct {
Success bool
Message *strings.Builder
}
func (result *ToggleResult) Append(msg string) {
result.Message.WriteString(msg)
result.Message.WriteString("\n")
}
type ToggleApplyResult interface {
IsMatched() bool
GetMessage() string
Append(result *ToggleResult, verbosity ToggleVerbosity)
}
type ToggleApplyResultImpl struct {
Matched bool
Message string
}
func (applyResult *ToggleApplyResultImpl) IsMatched() bool {
return applyResult.Matched
}
func (applyResult *ToggleApplyResultImpl) GetMessage() string {
return applyResult.Message
}
func (applyResult *ToggleApplyResultImpl) Append(result *ToggleResult, verbosity ToggleVerbosity) {
if verbosity.Show(applyResult.Matched) {
result.Append(applyResult.Message)
}
}
type sourceRegexpMatcher struct {
regex *regexp.Regexp
}
func (matcher *sourceRegexpMatcher) Matches(source string) bool {
return matcher.regex.Match([]byte(source))
}
func NewSourceMatcher(regex *regexp.Regexp) SourceMatcher {
return &sourceRegexpMatcher{regex}
}
type Source interface {
EnableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult)
DisableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult)
}
type Controller interface {
Source
AddSource(source Source)
RemoveSource(source Source)
EventHandler
}
func NewController(closeNotify <-chan struct{}) Controller {
controller := &controllerImpl{
events: make(chan controllerEvent, 25),
sources: make(map[Source]Source),
closeNotify: closeNotify,
}
go controller.run()
return controller
}
type enableSourcesEvent struct {
sourceType SourceType
matcher SourceMatcher
handler EventHandler
resultChan chan<- ToggleApplyResult
}
func (event *enableSourcesEvent) handle(controller *controllerImpl) {
for source := range controller.sources {
source.EnableTracing(event.sourceType, event.matcher, event.handler, event.resultChan)
}
close(event.resultChan)
}
type disableSourcesEvent struct {
sourceType SourceType
matcher SourceMatcher
handler EventHandler
resultChan chan<- ToggleApplyResult
}
func (event *disableSourcesEvent) handle(controller *controllerImpl) {
for handler := range controller.sources {
handler.DisableTracing(event.sourceType, event.matcher, event.handler, event.resultChan)
}
close(event.resultChan)
}
type sourceAddedEvent struct {
source Source
}
func (event *sourceAddedEvent) handle(controller *controllerImpl) {
controller.sources[event.source] = event.source
}
type sourceRemovedEvent struct {
source Source
}
func (event *sourceRemovedEvent) handle(controller *controllerImpl) {
delete(controller.sources, event.source)
}
type controllerEvent interface {
handle(impl *controllerImpl)
}
type controllerImpl struct {
events chan controllerEvent
sources map[Source]Source
closeNotify <-chan struct{}
}
func (controller *controllerImpl) Accept(event *trace_pb.ChannelMessage) {
select {
case controller.events <- &eventWrapper{wrapped: event}:
case <-controller.closeNotify:
}
}
func (controller *controllerImpl) EnableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult) {
select {
case controller.events <- &enableSourcesEvent{sourceType: sourceType, matcher: matcher, handler: handler, resultChan: resultChan}:
case <-controller.closeNotify:
}
}
func (controller *controllerImpl) DisableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult) {
select {
case controller.events <- &disableSourcesEvent{sourceType: sourceType, matcher: matcher, handler: handler, resultChan: resultChan}:
case <-controller.closeNotify:
}
}
func (controller *controllerImpl) AddSource(source Source) {
select {
case controller.events <- &sourceAddedEvent{source}:
case <-controller.closeNotify:
}
}
func (controller *controllerImpl) RemoveSource(source Source) {
select {
case controller.events <- &sourceRemovedEvent{source}:
case <-controller.closeNotify:
}
}
func (controller *controllerImpl) run() {
for {
select {
case <-controller.closeNotify:
return
case evt := <-controller.events:
evt.handle(controller)
}
}
}