From 9914d285275e8aafa20a17f70b6f357a9f66b7dc Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Sat, 27 Mar 2021 13:38:57 +0100 Subject: [PATCH] trace: Add support of Storage & OS tracing (#3663) --- cmd/admin-trace.go | 175 +++++++++++++++++++++++++++++++++++++++------ go.mod | 2 +- go.sum | 8 +-- 3 files changed, 158 insertions(+), 27 deletions(-) diff --git a/cmd/admin-trace.go b/cmd/admin-trace.go index 9f6547e3..8731107b 100644 --- a/cmd/admin-trace.go +++ b/cmd/admin-trace.go @@ -33,6 +33,7 @@ import ( "github.com/minio/mc/pkg/probe" "github.com/minio/minio/pkg/console" "github.com/minio/minio/pkg/madmin" + "github.com/minio/minio/pkg/trace" ) var adminTraceFlags = []cli.Flag{ @@ -42,8 +43,17 @@ var adminTraceFlags = []cli.Flag{ }, cli.BoolFlag{ Name: "all, a", - Usage: "trace all traffic (includes internode traffic from MinIO cluster)", + Usage: "trace all call types", }, + cli.StringSliceFlag{ + Name: "call", + Usage: "trace only matching Call types (values: `s3`, `internal`, `storage`, `os`)", + }, + cli.StringFlag{ + Name: "response-threshold", + Usage: "trace calls only with response duration greater than this threshold (e.g. `5ms`)", + }, + cli.IntSliceFlag{ Name: "status-code", Usage: "trace only matching status code", @@ -162,14 +172,59 @@ func matchTrace(ctx *cli.Context, traceInfo madmin.ServiceTraceInfo) bool { return false } +// Calculate tracing options for command line flags +func tracingOpts(ctx *cli.Context) (opts madmin.ServiceTraceOpts, e error) { + + if t := ctx.String("response-threshold"); t != "" { + d, e := time.ParseDuration(t) + if e != nil { + return opts, fmt.Errorf("Unable to parse threshold argument: %w", e) + } + opts.Threshold = d + } + + opts.OnlyErrors = ctx.Bool("errors") + + if ctx.Bool("all") { + opts.S3 = true + opts.Internal = true + opts.Storage = true + opts.OS = true + return + } + + apis := ctx.StringSlice("call") + if len(apis) == 0 { + // If api flag is not specified, then we will + // trace only S3 requests by default. + opts.S3 = true + return + } + + for _, api := range apis { + switch api { + case "storage": + opts.Storage = true + case "internal": + opts.Internal = true + case "s3": + opts.S3 = true + case "os": + opts.OS = true + } + } + + return +} + // mainAdminTrace - the entry function of trace command func mainAdminTrace(ctx *cli.Context) error { // Check for command syntax checkAdminTraceSyntax(ctx) + verbose := ctx.Bool("verbose") - all := ctx.Bool("all") - errfltr := ctx.Bool("errors") aliasedURL := ctx.Args().Get(0) + console.SetColor("Stat", color.New(color.FgYellow)) console.SetColor("Request", color.New(color.FgCyan)) @@ -198,8 +253,11 @@ func mainAdminTrace(ctx *cli.Context) error { ctxt, cancel := context.WithCancel(globalContext) defer cancel() + opts, e := tracingOpts(ctx) + fatalIf(probe.NewError(e), "Unable to start tracing") + // Start listening on all trace activity. - traceCh := client.ServiceTrace(ctxt, all, errfltr) + traceCh := client.ServiceTrace(ctxt, opts) for traceInfo := range traceCh { if traceInfo.Err != nil { fatalIf(probe.NewError(traceInfo.Err), "Unable to listen to http trace") @@ -223,6 +281,10 @@ type shortTraceMsg struct { Query string `json:"query"` StatusCode int `json:"statusCode"` StatusMsg string `json:"statusMsg"` + + StorageStats storageStats `json:"storageStats"` + OSStats osStats `json:"osStats"` + Type trace.Type `json:"type"` } type traceMessage struct { @@ -246,6 +308,7 @@ type responseInfo struct { Body string `json:"body,omitempty"` StatusCode int `json:"statusCode,omitempty"` } + type callStats struct { Rx int `json:"rx"` Tx int `json:"tx"` @@ -253,31 +316,60 @@ type callStats struct { Ttfb time.Duration `json:"timeToFirstByte"` } -type trace struct { - NodeName string `json:"host"` - FuncName string `json:"api"` +type osStats struct { + Duration time.Duration `json:"duration"` + Path string `json:"path"` +} + +type storageStats struct { + Duration time.Duration `json:"duration"` + Path string `json:"path"` +} + +type verboseTrace struct { + Type trace.Type `json:"type"` + + NodeName string `json:"host"` + FuncName string `json:"api"` + Time time.Time `json:"time"` + RequestInfo requestInfo `json:"request"` ResponseInfo responseInfo `json:"response"` CallStats callStats `json:"callStats"` + + StorageStats storageStats `json:"storageStats"` + OSStats osStats `json:"osStats"` } // return a struct with minimal trace info. func shortTrace(ti madmin.ServiceTraceInfo) shortTraceMsg { s := shortTraceMsg{} t := ti.Trace - s.Time = t.ReqInfo.Time - if host, ok := t.ReqInfo.Headers["Host"]; ok { - s.Host = strings.Join(host, "") - } - s.Path = t.ReqInfo.Path - s.Query = t.ReqInfo.RawQuery + + s.Type = t.TraceType s.FuncName = t.FuncName - s.StatusCode = t.RespInfo.StatusCode - s.StatusMsg = http.StatusText(t.RespInfo.StatusCode) - s.Client = t.ReqInfo.Client - s.CallStats.Duration = t.CallStats.Latency - s.CallStats.Rx = t.CallStats.InputBytes - s.CallStats.Tx = t.CallStats.OutputBytes + s.Time = t.Time + + switch t.TraceType { + case trace.Storage: + s.Path = t.StorageStats.Path + s.StorageStats.Duration = t.StorageStats.Duration + case trace.OS: + s.Path = t.OSStats.Path + s.OSStats.Duration = t.OSStats.Duration + case trace.HTTP: + if host, ok := t.ReqInfo.Headers["Host"]; ok { + s.Host = strings.Join(host, "") + } + s.Path = t.ReqInfo.Path + s.Query = t.ReqInfo.RawQuery + s.StatusCode = t.RespInfo.StatusCode + s.StatusMsg = http.StatusText(t.RespInfo.StatusCode) + s.Client = t.ReqInfo.Client + s.CallStats.Duration = t.CallStats.Latency + s.CallStats.Rx = t.CallStats.InputBytes + s.CallStats.Tx = t.CallStats.OutputBytes + } return s } @@ -297,14 +389,33 @@ func (s shortTraceMsg) String() string { var hostStr string var b = &strings.Builder{} + switch s.Type { + case trace.Storage: + fmt.Fprintf(b, "[%s] %s %s %2s", console.Colorize("RespStatus", "STORAGE"), console.Colorize("FuncName", s.FuncName), + s.Path, + console.Colorize("HeaderValue", s.StorageStats.Duration)) + return b.String() + case trace.OS: + fmt.Fprintf(b, "[%s] %s %s %2s", console.Colorize("RespStatus", "OS"), console.Colorize("FuncName", s.FuncName), + s.Path, + console.Colorize("HeaderValue", s.OSStats.Duration)) + return b.String() + } + + // HTTP trace + if s.Host != "" { hostStr = colorizedNodeName(s.Host) } fmt.Fprintf(b, "%s ", s.Time.Format(timeFormat)) - statusStr := console.Colorize("RespStatus", fmt.Sprintf("%d %s", s.StatusCode, s.StatusMsg)) + + statusStr := fmt.Sprintf("%d %s", s.StatusCode, s.StatusMsg) if s.StatusCode >= http.StatusBadRequest { - statusStr = console.Colorize("ErrStatus", fmt.Sprintf("%d %s", s.StatusCode, s.StatusMsg)) + statusStr = console.Colorize("ErrStatus", statusStr) + } else { + statusStr = console.Colorize("RespStatus", statusStr) } + fmt.Fprintf(b, "[%s] %s ", statusStr, console.Colorize("FuncName", s.FuncName)) fmt.Fprintf(b, "%s%s", hostStr, s.Path) @@ -312,6 +423,7 @@ func (s shortTraceMsg) String() string { fmt.Fprintf(b, "?%s ", s.Query) } fmt.Fprintf(b, " %s ", s.Client) + spaces := 15 - len(s.Client) fmt.Fprintf(b, "%*s", spaces, " ") fmt.Fprint(b, console.Colorize("HeaderValue", fmt.Sprintf(" %2s", s.CallStats.Duration.Round(time.Microsecond)))) @@ -346,9 +458,11 @@ func (t traceMessage) JSON() string { for k, v := range rs.Headers { rspHdrs[k] = strings.Join(v, " ") } - trc := trace{ + trc := verboseTrace{ + Type: t.Trace.TraceType, NodeName: t.Trace.NodeName, FuncName: t.Trace.FuncName, + Time: t.Trace.Time, RequestInfo: requestInfo{ Time: rq.Time, Proto: rq.Proto, @@ -370,6 +484,14 @@ func (t traceMessage) JSON() string { Tx: t.Trace.CallStats.OutputBytes, Ttfb: t.Trace.CallStats.TimeToFirstByte, }, + OSStats: osStats{ + Duration: t.Trace.OSStats.Duration, + Path: t.Trace.OSStats.Path, + }, + StorageStats: storageStats{ + Duration: t.Trace.StorageStats.Duration, + Path: t.Trace.StorageStats.Path, + }, } buf := &bytes.Buffer{} enc := json.NewEncoder(buf) @@ -391,6 +513,15 @@ func (t traceMessage) String() string { nodeNameStr = fmt.Sprintf("%s ", colorizedNodeName(trc.NodeName)) } + switch trc.TraceType { + case trace.Storage: + fmt.Fprintf(b, "%s %s [%s] %s %s", nodeNameStr, console.Colorize("Request", fmt.Sprintf("[STORAGE %s]", trc.FuncName)), trc.Time.Format(timeFormat), trc.StorageStats.Path, trc.StorageStats.Duration) + return b.String() + case trace.OS: + fmt.Fprintf(b, "%s %s [%s] %s %s", nodeNameStr, console.Colorize("Request", fmt.Sprintf("[POSIX %s]", trc.FuncName)), trc.Time.Format(timeFormat), trc.OSStats.Path, trc.OSStats.Duration) + return b.String() + } + ri := trc.ReqInfo rs := trc.RespInfo fmt.Fprintf(b, "%s%s", nodeNameStr, console.Colorize("Request", fmt.Sprintf("[REQUEST %s] ", trc.FuncName))) diff --git a/go.mod b/go.mod index 3250f7db..8eee76c4 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/mattn/go-isatty v0.0.12 github.com/mattn/go-runewidth v0.0.9 // indirect github.com/minio/cli v1.22.0 - github.com/minio/minio v0.0.0-20210317030657-0eb146e1b27c + github.com/minio/minio v0.0.0-20210327062407-d8b5adfd1013 github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e github.com/minio/sha256-simd v1.0.0 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index 1708537a..6493ccb7 100644 --- a/go.sum +++ b/go.sum @@ -328,12 +328,12 @@ github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3N github.com/miekg/dns v1.1.35/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM= github.com/minio/cli v1.22.0 h1:VTQm7lmXm3quxO917X3p+el1l0Ca5X3S4PM2ruUYO68= github.com/minio/cli v1.22.0/go.mod h1:bYxnK0uS629N3Bq+AOZZ+6lwF77Sodk4+UL9vNuXhOY= -github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= github.com/minio/md5-simd v1.1.1 h1:9ojcLbuZ4gXbB2sX53MKn8JUZ0sB/2wfwsEcRw+I08U= github.com/minio/md5-simd v1.1.1/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= -github.com/minio/minio v0.0.0-20210317030657-0eb146e1b27c h1:MEK84daKRjXBeVBsswEwEDRMS9Il5d7uDturnhr336M= -github.com/minio/minio v0.0.0-20210317030657-0eb146e1b27c/go.mod h1:3mUvnO95zQb7orgJZpTc1LdRH+vkfGahMS4UvAdhfB8= +github.com/minio/minio v0.0.0-20210327062407-d8b5adfd1013 h1:5949AasP1UUih05Gsgm8lOTSIFnPHmTniiOI/Dh4XXY= +github.com/minio/minio v0.0.0-20210327062407-d8b5adfd1013/go.mod h1:UZQwUr4doAT+EfExXJDdOBGhnnas1/97/WPdUGQpG7g= github.com/minio/minio-go/v7 v7.0.11-0.20210302210017-6ae69c73ce78/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e h1:IH4CNckd+Z+h5W9ee4QEoRD/fyw9o330PVy9l5Ogd4U= github.com/minio/minio-go/v7 v7.0.11-0.20210319012211-5a0d16291a2e/go.mod h1:mTh2uJuAbEqdhMVl6CMIIZLUeiMiWtJR4JB8/5g2skw= @@ -508,7 +508,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= github.com/tidwall/gjson v1.6.8 h1:CTmXMClGYPAmln7652e69B7OLXfTi5ABcPPwjIWUv7w= github.com/tidwall/gjson v1.6.8/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI= github.com/tidwall/match v1.0.3 h1:FQUVvBImDutD8wJLN6c5eMzWtjgONK9MwIBCOrUJKeE= @@ -519,6 +518,7 @@ github.com/tidwall/sjson v1.0.4/go.mod h1:bURseu1nuBkFpIES5cz6zBtjmYeOQmEESshn7V github.com/tinylib/msgp v1.1.3/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8 h1:ndzgwNDnKIqyCvHTXaCqh9KlOWKvBry6nuXMJmonVsE= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/ttacon/chalk v0.0.0-20160626202418-22c06c80ed31/go.mod h1:onvgF043R+lC5RZ8IT9rBXDaEDnpnw/Cl+HFiw+v/7Q= github.com/tv42/httpunix v0.0.0-20150427012821-b75d8614f926/go.mod h1:9ESjWnEqriFuLhtthL60Sar/7RFoluCcXsuvEwTV5KM= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=