Skip to content

Commit

Permalink
allow limit to traced message
Browse files Browse the repository at this point in the history
  • Loading branch information
andyxning committed Jul 10, 2019
1 parent 94071d3 commit 8050f92
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
14 changes: 12 additions & 2 deletions server/client.go
Expand Up @@ -1010,8 +1010,18 @@ 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
var msgToWrite []byte
if origLength > c.srv.opts.MaxTracedMessagePrintableLen {
msgToWrite = make([]byte, c.srv.opts.MaxTracedMessagePrintableLen+len("..."))
copy(msgToWrite, msg[:c.srv.opts.MaxTracedMessagePrintableLen])
msgToWrite = append(msgToWrite, []byte("...")...)
} else {
msgToWrite = msg[:origLength]
}

c.Tracef("<<- MSG_PAYLOAD: [%q]", msgToWrite)
}

func (c *client) traceInOp(op string, arg []byte) {
Expand Down
3 changes: 3 additions & 0 deletions server/const.go
Expand Up @@ -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_MESSAGE_PRINTABLE_LEN is the maximum printable length for traced messages.
MAX_TRACED_MESSAGE_PRINTABLE_LEN = 1024
)
8 changes: 8 additions & 0 deletions server/opts.go
Expand Up @@ -190,6 +190,8 @@ type Options struct {
WriteDeadline time.Duration `json:"-"`
MaxClosedClients int `json:"-"`
LameDuckDuration time.Duration `json:"-"`
// MaxTracedMessagePrintableLen is the maximum printable length for traced messages.
MaxTracedMessagePrintableLen int `json:"max_traced_message_printable_len"`

// Operating a trusted NATS server
TrustedKeys []string `json:"-"`
Expand Down Expand Up @@ -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_message_printable_len":
o.MaxTracedMessagePrintableLen = int(v.(int64))
case "max_subscriptions", "max_subs":
o.MaxSubs = int(v.(int64))
case "ping_interval":
Expand Down Expand Up @@ -2482,6 +2486,9 @@ func setBaselineOptions(opts *Options) {
if opts.ReconnectErrorReports == 0 {
opts.ReconnectErrorReports = DEFAULT_RECONNECT_ERROR_REPORTS
}
if opts.MaxTracedMessagePrintableLen == 0 {
opts.MaxTracedMessagePrintableLen = MAX_TRACED_MESSAGE_PRINTABLE_LEN
}
}

// ConfigureOptions accepts a flag set and augment it with NATS Server
Expand Down Expand Up @@ -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.MaxTracedMessagePrintableLen, "max_traced_message_printable_len", 0, "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
Expand Down
5 changes: 3 additions & 2 deletions server/opts_test.go
Expand Up @@ -59,8 +59,9 @@ func TestDefaultOptions(t *testing.T) {
LeafNode: LeafNodeOpts{
ReconnectInterval: DEFAULT_LEAF_NODE_RECONNECT,
},
ConnectErrorReports: DEFAULT_CONNECT_ERROR_REPORTS,
ReconnectErrorReports: DEFAULT_RECONNECT_ERROR_REPORTS,
ConnectErrorReports: DEFAULT_CONNECT_ERROR_REPORTS,
ReconnectErrorReports: DEFAULT_RECONNECT_ERROR_REPORTS,
MaxTracedMessagePrintableLen: MAX_TRACED_MESSAGE_PRINTABLE_LEN,
}

opts := &Options{}
Expand Down

0 comments on commit 8050f92

Please sign in to comment.