Skip to content

Commit

Permalink
Add --max-start-time and --min-start-time flags to tempo-cli `ana…
Browse files Browse the repository at this point in the history
…lyse blocks` command (grafana#3250)

* Add  flag to tempo-cli  command

* Docs

* Add min time as well

* Fix chlog

* Fix condition
  • Loading branch information
mapno authored and kvrhdn committed Feb 26, 2024
1 parent b087630 commit f827620
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 11 additions & 3 deletions cmd/tempo-cli/cmd-analyse-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
20 changes: 18 additions & 2 deletions cmd/tempo-cli/cmd-analyse-blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/sources/tempo/operations/tempo_cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ Options:
- `--num-attr <value>` Number of attributes to output (default: 10)
- `--min-compaction-level <value>` Minimum compaction level to include in the analysis (default: 3)
- `--max-blocks <value>` Maximum number of blocks to analyze (default: 10)
- `--max-start-time <value>` Oldest start time for a block to be processed. RFC3339 format (default: disabled)
- `--min-end-time <value>` Newest end time for a block to be processed. RFC3339 format (default: disabled)

**Example:**
```bash
Expand Down

0 comments on commit f827620

Please sign in to comment.