diff --git a/server/client.go b/server/client.go index 1452cdeedf7..377903c3b09 100644 --- a/server/client.go +++ b/server/client.go @@ -1010,8 +1010,15 @@ 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]) + + origLength := len(msg) - LEN_CR_LF + validLength := origLength + formatter := "<<- MSG_PAYLOAD: [%q]" + if origLength > c.srv.getOpts().MaxTracedMsgLen { + validLength = c.srv.getOpts().MaxTracedMsgLen + formatter = "<<- MSG_PAYLOAD: [\"%s...\"]" + } + c.Tracef(formatter, msg[:validLength]) } func (c *client) traceInOp(op string, arg []byte) { diff --git a/server/const.go b/server/const.go index 74fc1c13eb0..bd3fbec9586 100644 --- a/server/const.go +++ b/server/const.go @@ -163,4 +163,7 @@ const ( // DEFAULT_RTT_MEASUREMENT_INTERVAL is how often we want to measure RTT from // this server to clients, routes, gateways or leafnode connections. DEFAULT_RTT_MEASUREMENT_INTERVAL = time.Hour + + // MAX_TRACED_MSG_LEN is the maximum printable length for traced messages. + MAX_TRACED_MSG_LEN = 1024 ) diff --git a/server/opts.go b/server/opts.go index f2e04aa0349..28e1ac4c37d 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:"max_traced_msg_len"` // 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": @@ -2482,6 +2486,9 @@ func setBaselineOptions(opts *Options) { if opts.ReconnectErrorReports == 0 { opts.ReconnectErrorReports = DEFAULT_RECONNECT_ERROR_REPORTS } + if opts.MaxTracedMsgLen == -1 { + opts.MaxTracedMsgLen = MAX_TRACED_MSG_LEN + } } // ConfigureOptions accepts a flag set and augment it with NATS Server @@ -2551,6 +2558,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", -1, "Maximum printable length for traced messages") // 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 b645591449c..83c12a6cf40 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 1511fcfdad0..28525de2975 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. @@ -778,7 +792,8 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { continue } fallthrough - + case "maxtracedmsglen": + diffOpts = append(diffOpts, &maxTracedMsgLenOption{newValue: newValue.(int)}) 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