From cd214fca89443e5aca5d27cf51ea32adea9feec2 Mon Sep 17 00:00:00 2001 From: Andy Xie Date: Fri, 5 Jul 2019 15:49:27 +0800 Subject: [PATCH] allow limit to traced message --- server/client.go | 9 +++++++-- server/opts.go | 5 +++++ server/opts_test.go | 1 + server/reload.go | 17 ++++++++++++++++- 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/server/client.go b/server/client.go index 1452cdeedf..d3580a10bd 100644 --- a/server/client.go +++ b/server/client.go @@ -1010,8 +1010,13 @@ func (c *client) traceMsg(msg []byte) { if !c.trace { return } - // FIXME(dlc), allow limits to printable payload. - c.Tracef("<<- MSG_PAYLOAD: [%q]", msg[:len(msg)-LEN_CR_LF]) + + maxTrace := c.srv.getOpts().MaxTracedMsgLen + if maxTrace != 0 && (len(msg)-LEN_CR_LF) > maxTrace { + c.Tracef("<<- MSG_PAYLOAD: [\"%s...\"]", msg[:maxTrace]) + } else { + c.Tracef("<<- MSG_PAYLOAD: [%q]", msg[:len(msg)-LEN_CR_LF]) + } } func (c *client) traceInOp(op string, arg []byte) { diff --git a/server/opts.go b/server/opts.go index f2e04aa034..5c5bfa3155 100644 --- a/server/opts.go +++ b/server/opts.go @@ -190,6 +190,8 @@ type Options struct { WriteDeadline time.Duration `json:"-"` MaxClosedClients int `json:"-"` LameDuckDuration time.Duration `json:"-"` + // MaxTracedMsgLen is the maximum printable length for traced messages. + MaxTracedMsgLen int `json:"-"` // Operating a trusted NATS server TrustedKeys []string `json:"-"` @@ -539,6 +541,8 @@ func (o *Options) ProcessConfigFile(configFile string) error { o.MaxPending = v.(int64) case "max_connections", "max_conn": o.MaxConn = int(v.(int64)) + case "max_traced_msg_len": + o.MaxTracedMsgLen = int(v.(int64)) case "max_subscriptions", "max_subs": o.MaxSubs = int(v.(int64)) case "ping_interval": @@ -2551,6 +2555,7 @@ func ConfigureOptions(fs *flag.FlagSet, args []string, printVersion, printHelp, fs.StringVar(&opts.TLSCert, "tlscert", "", "Server certificate file.") fs.StringVar(&opts.TLSKey, "tlskey", "", "Private key for server certificate.") fs.StringVar(&opts.TLSCaCert, "tlscacert", "", "Client certificate CA for verification.") + fs.IntVar(&opts.MaxTracedMsgLen, "max_traced_msg_len", 0, "Maximum printable length for traced messages. 0 for unlimited") // The flags definition above set "default" values to some of the options. // Calling Parse() here will override the default options with any value diff --git a/server/opts_test.go b/server/opts_test.go index b645591449..83c12a6cf4 100644 --- a/server/opts_test.go +++ b/server/opts_test.go @@ -61,6 +61,7 @@ func TestDefaultOptions(t *testing.T) { }, ConnectErrorReports: DEFAULT_CONNECT_ERROR_REPORTS, ReconnectErrorReports: DEFAULT_RECONNECT_ERROR_REPORTS, + MaxTracedMsgLen: 0, } opts := &Options{} diff --git a/server/reload.go b/server/reload.go index 1511fcfdad..a8ff586077 100644 --- a/server/reload.go +++ b/server/reload.go @@ -523,6 +523,20 @@ func (r *reconnectErrorReports) Apply(s *Server) { s.Noticef("Reloaded: reconnect_error_reports = %v", r.newValue) } +// maxTracedMsgLenOption implements the option interface for the `max_traced_msg_len` setting. +type maxTracedMsgLenOption struct { + noopOption + newValue int +} + +// Apply the setting by updating the maximum traced message length. +func (m *maxTracedMsgLenOption) Apply(server *Server) { + server.mu.Lock() + defer server.mu.Unlock() + server.opts.MaxTracedMsgLen = m.newValue + server.Noticef("Reloaded: max_traced_msg_len = %d", m.newValue) +} + // Reload reads the current configuration file and applies any supported // changes. This returns an error if the server was not started with a config // file or an option which doesn't support hot-swapping was changed. @@ -771,6 +785,8 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { // Ignore NoLog and NoSigs options since they are not parsed and only used in // testing. continue + case "maxtracedmsglen": + diffOpts = append(diffOpts, &maxTracedMsgLenOption{newValue: newValue.(int)}) case "port": // check to see if newValue == 0 and continue if so. if newValue == 0 { @@ -778,7 +794,6 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { continue } fallthrough - default: // TODO(ik): Implement String() on those options to have a nice print. // %v is difficult to figure what's what, %+v print private fields and