-
Notifications
You must be signed in to change notification settings - Fork 159
/
xgress_peekhandler.go
115 lines (96 loc) · 3.42 KB
/
xgress_peekhandler.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
/*
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"
"github.com/openziti/channel/v2/trace/pb"
"github.com/openziti/ziti/router/xgress"
"github.com/openziti/foundation/v2/concurrenz"
"github.com/openziti/identity"
"sync/atomic"
"time"
)
type XgressPeekHandler struct {
appId *identity.TokenId
enabled atomic.Bool
controller Controller
decoders []channel.TraceMessageDecoder
eventSinks concurrenz.CopyOnWriteSlice[EventHandler]
}
func (self *XgressPeekHandler) EnableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult) {
self.ToggleTracing(sourceType, matcher, true, handler, resultChan)
}
func (self *XgressPeekHandler) DisableTracing(sourceType SourceType, matcher SourceMatcher, handler EventHandler, resultChan chan<- ToggleApplyResult) {
self.ToggleTracing(sourceType, matcher, false, handler, resultChan)
}
func (self *XgressPeekHandler) ToggleTracing(sourceType SourceType, matcher SourceMatcher, enable bool, handler EventHandler, resultChan chan<- ToggleApplyResult) {
name := "xgress"
matched := sourceType == SourceTypePipe && matcher.Matches(name)
prevState := self.IsEnabled()
nextState := prevState
if matched {
nextState = enable
if enable {
self.enabled.Store(true)
self.eventSinks.Append(handler)
} else {
self.eventSinks.Delete(handler)
if len(self.eventSinks.Value()) == 0 {
self.enabled.Store(false)
}
}
}
resultChan <- &ToggleApplyResultImpl{matched,
fmt.Sprintf("Link %v.%v matched? %v. Old trace state: %v, New trace state: %v",
self.appId.Token, name, matched, prevState, nextState)}
}
func (self *XgressPeekHandler) Rx(x *xgress.Xgress, payload *xgress.Payload) {
self.trace(x, payload, true)
}
func (self *XgressPeekHandler) Tx(x *xgress.Xgress, payload *xgress.Payload) {
self.trace(x, payload, false)
}
func (self *XgressPeekHandler) Close(*xgress.Xgress) {
panic("implement me")
}
func NewXgressPeekHandler(appId *identity.TokenId, controller Controller) *XgressPeekHandler {
handler := &XgressPeekHandler{
appId: appId,
controller: controller,
decoders: decoders,
}
controller.AddSource(handler)
return handler
}
func (self *XgressPeekHandler) IsEnabled() bool {
return self.enabled.Load()
}
func (self *XgressPeekHandler) trace(x *xgress.Xgress, payload *xgress.Payload, rx bool) {
decode, _ := xgress.DecodePayload(payload)
traceMsg := &trace_pb.ChannelMessage{
Timestamp: time.Now().UnixNano(),
Identity: self.appId.Token,
Channel: x.Label(),
IsRx: rx,
ContentType: xgress.ContentTypePayloadType,
Sequence: -1,
ReplyFor: -1,
Length: int32(len(payload.Data)),
Decode: decode,
}
// This can result in a message send. Doing a send from inside a peekhandler can cause deadlocks, so it's best avoided
for _, eventSink := range self.eventSinks.Value() {
go eventSink.Accept(traceMsg)
}
}