-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
protocol_trace.go
56 lines (45 loc) · 1.28 KB
/
protocol_trace.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
package xds
import (
envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
"github.com/mitchellh/copystructure"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func (s *ResourceGenerator) logTraceRequest(msg string, pb proto.Message) {
s.logTraceProto(msg, pb, false)
}
func (s *ResourceGenerator) logTraceResponse(msg string, pb proto.Message) {
s.logTraceProto(msg, pb, true)
}
func (s *ResourceGenerator) logTraceProto(msg string, pb proto.Message, response bool) {
if !s.Logger.IsTrace() {
return
}
dir := "request"
if response {
dir = "response"
}
// Duplicate the request so we can scrub the huge Node field for logging.
// If the cloning fails, then log anyway but don't scrub the node field.
if dup, err := copystructure.Copy(pb); err == nil {
pb = dup.(proto.Message)
// strip the node field
switch x := pb.(type) {
case *envoy_discovery_v3.DiscoveryRequest:
x.Node = nil
case *envoy_discovery_v3.DeltaDiscoveryRequest:
x.Node = nil
}
}
m := protojson.MarshalOptions{
Indent: " ",
}
out := ""
outBytes, err := m.Marshal(pb)
if err != nil {
out = "<ERROR: " + err.Error() + ">"
} else {
out = string(outBytes)
}
s.Logger.Trace(msg, "direction", dir, "protobuf", out)
}