From f827620ae70e514bf9d04b001dd2352e6f85f475 Mon Sep 17 00:00:00 2001 From: Mario Date: Fri, 9 Feb 2024 13:45:05 +0100 Subject: [PATCH] Add `--max-start-time` and `--min-start-time` flags to tempo-cli `analyse blocks` command (#3250) * Add flag to tempo-cli command * Docs * Add min time as well * Fix chlog * Fix condition --- CHANGELOG.md | 1 + cmd/tempo-cli/cmd-analyse-block.go | 14 +++++++++++--- cmd/tempo-cli/cmd-analyse-blocks.go | 20 ++++++++++++++++++-- docs/sources/tempo/operations/tempo_cli.md | 2 ++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09b4f8487ed..51fdad81ca6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ * [ENHANCEMENT] Added a `frontend-search` cache role for job search caching. [#3225](https://github.com/grafana/tempo/pull/3225) (@joe-elliott) * [ENHANCEMENT] Added a `parquet-page` cache role for page level caching. [#3196](https://github.com/grafana/tempo/pull/3196) (@joe-elliott) * [ENHANCEMENT] Update opentelemetry-collector-contrib dependency to the latest version, v0.89.0 [#3148](https://github.com/grafana/tempo/pull/3148) (@gebn) +* [ENHANCEMENT] Add `--max-start-time` and `--min-start-time` flag to tempo-cli command `analyse blocks` [#3250](https://github.com/grafana/tempo/pull/3250) (@mapno) * [ENHANCEMENT] Add per-tenant configurable remote_write headers to metrics-generator [#3175](https://github.com/grafana/tempo/pull/3175) (@mapno) * [ENHANCEMENT] Add variable expansion support to overrides configuration [#3175](https://github.com/grafana/tempo/pull/3175) (@mapno) * [ENHANCEMENT] Update memcached default image in jsonnet for multiple CVE [#3310](https://github.com/grafana/tempo/pull/3310) (@zalegrala) diff --git a/cmd/tempo-cli/cmd-analyse-block.go b/cmd/tempo-cli/cmd-analyse-block.go index 885efe1f802..86f6d3c1973 100644 --- a/cmd/tempo-cli/cmd-analyse-block.go +++ b/cmd/tempo-cli/cmd-analyse-block.go @@ -97,12 +97,12 @@ type analyseBlockCmd struct { } func (cmd *analyseBlockCmd) Run(ctx *globalOptions) error { - r, _, c, err := loadBackend(&cmd.backendOptions, ctx) + r, _, _, err := loadBackend(&cmd.backendOptions, ctx) if err != nil { return err } - blockSum, err := processBlock(r, c, cmd.TenantID, cmd.BlockID, time.Hour, 0) + blockSum, err := processBlock(r, cmd.TenantID, cmd.BlockID, time.Time{}, time.Time{}, 0) if err != nil { if errors.Is(err, backend.ErrDoesNotExist) { return fmt.Errorf("unable to analyze block: block has no block.meta because it was compacted") @@ -117,7 +117,7 @@ func (cmd *analyseBlockCmd) Run(ctx *globalOptions) error { return blockSum.print(cmd.NumAttr, cmd.GenerateJsonnet) } -func processBlock(r backend.Reader, _ backend.Compactor, tenantID, blockID string, _ time.Duration, minCompactionLvl uint8) (*blockSummary, error) { +func processBlock(r backend.Reader, tenantID, blockID string, maxStartTime, minStartTime time.Time, minCompactionLvl uint8) (*blockSummary, error) { id := uuid.MustParse(blockID) meta, err := r.BlockMeta(context.TODO(), id, tenantID) @@ -127,6 +127,14 @@ func processBlock(r backend.Reader, _ backend.Compactor, tenantID, blockID strin if meta.CompactionLevel < minCompactionLvl { return nil, nil } + if !maxStartTime.IsZero() && meta.StartTime.After(maxStartTime) { + // Block is newer than maxStartTime + return nil, nil + } + if !minStartTime.IsZero() && meta.StartTime.Before(minStartTime) { + // Block is older than minStartTime + return nil, nil + } var reader io.ReaderAt switch meta.Version { diff --git a/cmd/tempo-cli/cmd-analyse-blocks.go b/cmd/tempo-cli/cmd-analyse-blocks.go index 65f6ddcdb2f..31fcb98e710 100644 --- a/cmd/tempo-cli/cmd-analyse-blocks.go +++ b/cmd/tempo-cli/cmd-analyse-blocks.go @@ -17,10 +17,12 @@ type analyseBlocksCmd struct { MinCompactionLevel int `help:"Min compaction level to analyse" default:"3"` MaxBlocks int `help:"Max number of blocks to analyse" default:"10"` NumAttr int `help:"Number of attributes to display" default:"15"` + MaxStartTime string `help:"Oldest start time for a block to be processed. RFC3339 format '2006-01-02T15:04:05Z07:00'" default:""` + MinStartTime string `help:"Newest start time for a block to be processed. RFC3339 format '2006-01-02T15:04:05Z07:00'" default:""` } func (cmd *analyseBlocksCmd) Run(ctx *globalOptions) error { - r, _, c, err := loadBackend(&cmd.backendOptions, ctx) + r, _, _, err := loadBackend(&cmd.backendOptions, ctx) if err != nil { return err } @@ -35,13 +37,27 @@ func (cmd *analyseBlocksCmd) Run(ctx *globalOptions) error { topSpanAttrs, topResourceAttrs := make(map[string]uint64), make(map[string]uint64) totalSpanBytes, totalResourceBytes := uint64(0), uint64(0) + var maxStartTime, minStartTime time.Time + if cmd.MaxStartTime != "" { + maxStartTime, err = time.Parse(time.RFC3339, cmd.MaxStartTime) + if err != nil { + return err + } + } + if cmd.MinStartTime != "" { + minStartTime, err = time.Parse(time.RFC3339, cmd.MinStartTime) + if err != nil { + return err + } + } + for i := 0; i < len(blocks) && len(processedBlocks) < cmd.MaxBlocks; i++ { block := blocks[i] if _, ok := processedBlocks[block]; ok { continue } - blockSum, err := processBlock(r, c, cmd.TenantID, block.String(), time.Hour, uint8(cmd.MinCompactionLevel)) + blockSum, err := processBlock(r, cmd.TenantID, block.String(), maxStartTime, minStartTime, uint8(cmd.MinCompactionLevel)) if err != nil { if !errors.Is(err, backend.ErrDoesNotExist) { return err diff --git a/docs/sources/tempo/operations/tempo_cli.md b/docs/sources/tempo/operations/tempo_cli.md index b891a048133..31ba3ffb6c9 100644 --- a/docs/sources/tempo/operations/tempo_cli.md +++ b/docs/sources/tempo/operations/tempo_cli.md @@ -433,6 +433,8 @@ Options: - `--num-attr ` Number of attributes to output (default: 10) - `--min-compaction-level ` Minimum compaction level to include in the analysis (default: 3) - `--max-blocks ` Maximum number of blocks to analyze (default: 10) +- `--max-start-time ` Oldest start time for a block to be processed. RFC3339 format (default: disabled) +- `--min-end-time ` Newest end time for a block to be processed. RFC3339 format (default: disabled) **Example:** ```bash