open-consul/agent/xds/protocol_trace.go
Matt Keeler 554f1e6fee
Protobuf Modernization (#15949)
* Protobuf Modernization

Remove direct usage of golang/protobuf in favor of google.golang.org/protobuf

Marshallers (protobuf and json) needed some changes to account for different APIs.

Moved to using the google.golang.org/protobuf/types/known/* for the well known types including replacing some custom Struct manipulation with whats available in the structpb well known type package.

This also updates our devtools script to install protoc-gen-go from the right location so that files it generates conform to the correct interfaces.

* Fix go-mod-tidy make target to work on all modules
2023-01-11 09:39:10 -05:00

57 lines
1.3 KiB
Go

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)
}