mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-31 09:00:12 +08:00
Truncate payload printed in the UI
Added a flag --max-payload-length to allow customizing the value
This commit is contained in:
parent
700a8a7ac6
commit
b92ef4c369
@ -26,6 +26,7 @@ var (
|
|||||||
flagRedisURL string
|
flagRedisURL string
|
||||||
flagRedisInsecureTLS bool
|
flagRedisInsecureTLS bool
|
||||||
flagRedisClusterNodes string
|
flagRedisClusterNodes string
|
||||||
|
flagMaxPayloadLength int
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -37,6 +38,7 @@ func init() {
|
|||||||
flag.StringVar(&flagRedisURL, "redis-url", "", "URL to redis server")
|
flag.StringVar(&flagRedisURL, "redis-url", "", "URL to redis server")
|
||||||
flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", false, "disable TLS certificate host checks")
|
flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", false, "disable TLS certificate host checks")
|
||||||
flag.StringVar(&flagRedisClusterNodes, "redis-cluster-nodes", "", "comma separated list of host:port addresses of cluster nodes")
|
flag.StringVar(&flagRedisClusterNodes, "redis-cluster-nodes", "", "comma separated list of host:port addresses of cluster nodes")
|
||||||
|
flag.IntVar(&flagMaxPayloadLength, "max-payload-length", 200, "maximum number of utf8 characters printed in the payload cell in the Web UI")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Write test and refactor this code.
|
// TODO: Write test and refactor this code.
|
||||||
@ -98,7 +100,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
h := asynqmon.New(asynqmon.Options{
|
h := asynqmon.New(asynqmon.Options{
|
||||||
RedisConnOpt: redisConnOpt,
|
RedisConnOpt: redisConnOpt,
|
||||||
|
PayloadFormatter: asynqmon.PayloadFormatterFunc(formatPayload),
|
||||||
})
|
})
|
||||||
defer h.Close()
|
defer h.Close()
|
||||||
|
|
||||||
@ -116,3 +119,20 @@ func main() {
|
|||||||
fmt.Printf("Asynq Monitoring WebUI server is listening on port %d\n", flagPort)
|
fmt.Printf("Asynq Monitoring WebUI server is listening on port %d\n", flagPort)
|
||||||
log.Fatal(srv.ListenAndServe())
|
log.Fatal(srv.ListenAndServe())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func formatPayload(taskType string, payload []byte) string {
|
||||||
|
payloadStr := asynqmon.DefaultPayloadFormatter.FormatPayload(taskType, payload)
|
||||||
|
return truncate(payloadStr, flagMaxPayloadLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
// truncates string s to limit length (in utf8).
|
||||||
|
func truncate(s string, limit int) string {
|
||||||
|
i := 0
|
||||||
|
for pos := range s {
|
||||||
|
if i == limit {
|
||||||
|
return s[:pos] + "…"
|
||||||
|
}
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
@ -27,7 +27,10 @@ func (f PayloadFormatterFunc) FormatPayload(taskType string, payload []byte) str
|
|||||||
return f(taskType, payload)
|
return f(taskType, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
var defaultPayloadFormatter = PayloadFormatterFunc(func(_ string, payload []byte) string {
|
// DefaultPayloadFormatter is the PayloadFormater used by default.
|
||||||
|
// It prints the given payload bytes as is if the bytes are printable, otherwise it prints a message to indicate
|
||||||
|
// that the bytes are not printable.
|
||||||
|
var DefaultPayloadFormatter = PayloadFormatterFunc(func(_ string, payload []byte) string {
|
||||||
if !isPrintable(payload) {
|
if !isPrintable(payload) {
|
||||||
return "non-printable bytes"
|
return "non-printable bytes"
|
||||||
}
|
}
|
||||||
|
@ -88,7 +88,7 @@ var staticContents embed.FS
|
|||||||
func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspector) *mux.Router {
|
func muxRouter(opts Options, rc redis.UniversalClient, inspector *asynq.Inspector) *mux.Router {
|
||||||
router := mux.NewRouter().PathPrefix(opts.RootPath).Subrouter()
|
router := mux.NewRouter().PathPrefix(opts.RootPath).Subrouter()
|
||||||
|
|
||||||
var pf PayloadFormatter = defaultPayloadFormatter
|
var pf PayloadFormatter = DefaultPayloadFormatter
|
||||||
if opts.PayloadFormatter != nil {
|
if opts.PayloadFormatter != nil {
|
||||||
pf = opts.PayloadFormatter
|
pf = opts.PayloadFormatter
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user