From 176072dee43de613569aaa56061eba9d6d550290 Mon Sep 17 00:00:00 2001 From: dorman <37854724+dormanze@users.noreply.github.com> Date: Sat, 24 Dec 2022 23:21:38 +0800 Subject: [PATCH] trace: Add input and output filters options (#4353) --- cmd/admin-scanner-trace.go | 41 ++++++++++++++++++-- cmd/admin-trace.go | 78 ++++++++++++++++++++++++++++++++------ 2 files changed, 104 insertions(+), 15 deletions(-) diff --git a/cmd/admin-scanner-trace.go b/cmd/admin-scanner-trace.go index 8a35a35e..46f2e6b4 100644 --- a/cmd/admin-scanner-trace.go +++ b/cmd/admin-scanner-trace.go @@ -32,10 +32,6 @@ var adminScannerTraceFlags = []cli.Flag{ Name: "verbose, v", Usage: "print verbose trace", }, - cli.StringFlag{ - Name: "response-threshold", - Usage: "trace calls only with response duration greater than this threshold (e.g. `5ms`)", - }, cli.StringSliceFlag{ Name: "funcname", Usage: "trace only matching func name (eg 'scanner.ScanObject')", @@ -48,6 +44,22 @@ var adminScannerTraceFlags = []cli.Flag{ Name: "path", Usage: "trace only matching path", }, + cli.BoolFlag{ + Name: "filter-request", + Usage: "trace calls only with request bytes greater than this threshold, use with filter-size", + }, + cli.BoolFlag{ + Name: "filter-response", + Usage: "trace calls only with response bytes greater than this threshold, use with filter-size", + }, + cli.BoolFlag{ + Name: "response-duration", + Usage: "trace calls only with response duration greater than this threshold (e.g. `5ms`)", + }, + cli.StringFlag{ + Name: "filter-size", + Usage: "filter size, use with filter (see UNITS)", + }, } var adminScannerTraceCmd = cli.Command{ @@ -67,6 +79,14 @@ USAGE: FLAGS: {{range .VisibleFlags}}{{.}} {{end}} + +UNITS + --filter-size flags use with --filter-response or --filter-request accept human-readable case-insensitive number + suffixes such as "k", "m", "g" and "t" referring to the metric units KB, + MB, GB and TB respectively. Adding an "i" to these prefixes, uses the IEC + units, so that "gi" refers to "gibibyte" or "GiB". A "b" at the end is + also accepted. Without suffixes the unit is bytes. + EXAMPLES: 1. Show scanner trace for MinIO server {{.Prompt}} {{.HelpName}} myminio @@ -80,6 +100,14 @@ EXAMPLES: 4. Avoid printing replication related S3 requests {{.Prompt}} {{.HelpName}} --request-header '!X-Minio-Source' myminio + 5. Show trace only for ScanObject operations request bytes greater than 1MB + {{.Prompt}} {{.HelpName}} --filter-request --filter-size 1MB myminio + + 6. Show trace only for ScanObject operations response bytes greater than 1MB + {{.Prompt}} {{.HelpName}} --filter-response --filter-size 1MB myminio + + 7. Show trace only for requests operations duration greater than 5ms + {{.Prompt}} {{.HelpName}} --response-duration 5ms myminio `, } @@ -87,6 +115,11 @@ func checkAdminScannerTraceSyntax(ctx *cli.Context) { if len(ctx.Args()) != 1 { showCommandHelpAndExit(ctx, 1) // last argument is exit code } + filterFlag := ctx.Bool("filter-request") || ctx.Bool("filter-response") + if filterFlag && ctx.String("filter-size") == "" { + // filter must use with filter-size flags + showCommandHelpAndExit(ctx, 1) + } } // mainAdminScannerTrace - the entry function of trace command diff --git a/cmd/admin-trace.go b/cmd/admin-trace.go index cd878ff8..2132f12e 100644 --- a/cmd/admin-trace.go +++ b/cmd/admin-trace.go @@ -50,10 +50,6 @@ var adminTraceFlags = []cli.Flag{ Name: "call", Usage: "trace only matching call types. See CALL TYPES below for list. (default: s3)", }, - cli.DurationFlag{ - 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", @@ -82,6 +78,22 @@ var adminTraceFlags = []cli.Flag{ Name: "errors, e", Usage: "trace only failed requests", }, + cli.BoolFlag{ + Name: "filter-request", + Usage: "trace calls only with request bytes greater than this threshold, use with filter-size", + }, + cli.BoolFlag{ + Name: "filter-response", + Usage: "trace calls only with response bytes greater than this threshold, use with filter-size", + }, + cli.BoolFlag{ + Name: "response-duration", + Usage: "trace calls only with response duration greater than this threshold (e.g. `5ms`)", + }, + cli.StringFlag{ + Name: "filter-size", + Usage: "filter size, use with filter (see UNITS)", + }, } // traceCallTypes contains all call types and flags to apply when selected. @@ -157,6 +169,13 @@ FLAGS: CALL TYPES: ` + traceCallsHelp() + ` +UNITS + --filter-size flags use with --filter-response or --filter-request accept human-readable case-insensitive number + suffixes such as "k", "m", "g" and "t" referring to the metric units KB, + MB, GB and TB respectively. Adding an "i" to these prefixes, uses the IEC + units, so that "gi" refers to "gibibyte" or "GiB". A "b" at the end is + also accepted. Without suffixes the unit is bytes. + EXAMPLES: 1. Show verbose console trace for MinIO server {{.Prompt}} {{.HelpName}} -v -a myminio @@ -172,6 +191,15 @@ EXAMPLES: 5. Show console trace for requests with '404' and '503' status code {{.Prompt}} {{.HelpName}} --status-code 404 --status-code 503 myminio + + 6. Show trace only for requests bytes greater than 1MB + {{.Prompt}} {{.HelpName}} --filter-request --filter-size 1MB myminio + + 7. Show trace only for response bytes greater than 1MB + {{.Prompt}} {{.HelpName}} --filter-response --filter-size 1MB myminio + + 8. Show trace only for requests operations duration greater than 5ms + {{.Prompt}} {{.HelpName}} --response-duration 5ms myminio `, } @@ -183,6 +211,11 @@ func checkAdminTraceSyntax(ctx *cli.Context) { if len(ctx.Args()) != 1 { showCommandHelpAndExit(ctx, 1) // last argument is exit code } + filterFlag := ctx.Bool("filter-request") || ctx.Bool("filter-response") + if filterFlag && ctx.String("filter-size") == "" { + // filter must use with filter-size flags + showCommandHelpAndExit(ctx, 1) + } if ctx.Bool("all") && len(ctx.StringSlice("call")) > 0 { fatalIf(errDummy().Trace(), "You cannot specify both --all and --call flags at the same time.") @@ -203,12 +236,14 @@ type matchString struct { } type matchOpts struct { - statusCodes []int - methods []string - funcNames []string - apiPaths []string - nodes []string - reqHeaders []matchString + statusCodes []int + methods []string + funcNames []string + apiPaths []string + nodes []string + reqHeaders []matchString + requestSize uint64 + responseSize uint64 } func matchTrace(opts matchOpts, traceInfo madmin.ServiceTraceInfo) bool { @@ -308,6 +343,14 @@ func matchTrace(opts matchOpts, traceInfo madmin.ServiceTraceInfo) bool { } } + if opts.requestSize > 0 && traceInfo.Trace.HTTP.CallStats.InputBytes < int(opts.requestSize) { + return false + } + + if opts.responseSize > 0 && traceInfo.Trace.HTTP.CallStats.OutputBytes < int(opts.responseSize) { + return false + } + return true } @@ -323,12 +366,25 @@ func matchingOpts(ctx *cli.Context) (opts matchOpts) { ms.val = strings.TrimPrefix(s, "!") opts.reqHeaders = append(opts.reqHeaders, ms) } + var e error + var requestSize, responseSize uint64 + if ctx.Bool("filter-request") && ctx.String("filter-size") != "" { + requestSize, e = humanize.ParseBytes(ctx.String("filter-size")) + fatalIf(probe.NewError(e).Trace(ctx.String("filter-size")), "Unable to parse input bytes.") + } + + if ctx.Bool("filter-response") && ctx.String("filter-size") != "" { + responseSize, e = humanize.ParseBytes(ctx.String("filter-size")) + fatalIf(probe.NewError(e).Trace(ctx.String("filter-size")), "Unable to parse input bytes.") + } + opts.requestSize = requestSize + opts.responseSize = responseSize return } // Calculate tracing options for command line flags func tracingOpts(ctx *cli.Context, apis []string) (opts madmin.ServiceTraceOpts, e error) { - opts.Threshold = ctx.Duration("response-threshold") + opts.Threshold = ctx.Duration("response-duration") opts.OnlyErrors = ctx.Bool("errors") if ctx.Bool("all") {