2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2021-04-29 18:54:05 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
|
|
|
envoy_discovery_v3 "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
|
|
|
|
|
|
|
|
"github.com/mitchellh/copystructure"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
|
|
"google.golang.org/protobuf/proto"
|
2021-04-29 18:54:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
m := protojson.MarshalOptions{
|
2021-04-29 18:54:05 +00:00
|
|
|
Indent: " ",
|
|
|
|
}
|
2023-01-11 14:39:10 +00:00
|
|
|
|
|
|
|
out := ""
|
|
|
|
outBytes, err := m.Marshal(pb)
|
2021-04-29 18:54:05 +00:00
|
|
|
if err != nil {
|
|
|
|
out = "<ERROR: " + err.Error() + ">"
|
2023-01-11 14:39:10 +00:00
|
|
|
} else {
|
|
|
|
out = string(outBytes)
|
2021-04-29 18:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s.Logger.Trace(msg, "direction", dir, "protobuf", out)
|
|
|
|
}
|