@@ -41,6 +41,7 @@ import (
4141
4242const (
4343 printBlocksTableHeader = "BLOCK ULID\t MIN TIME\t MAX TIME\t NUM SAMPLES\t NUM CHUNKS\t NUM SERIES"
44+ defaultAnalyzeLimit = "20"
4445)
4546
4647func main () {
@@ -66,7 +67,7 @@ func execute() (err error) {
6667 analyzeCmd = cli .Command ("analyze" , "analyze churn, label pair cardinality." )
6768 analyzePath = analyzeCmd .Arg ("db path" , "database path (default is " + defaultDBPath + ")" ).Default (defaultDBPath ).String ()
6869 analyzeBlockID = analyzeCmd .Arg ("block id" , "block to analyze (default is the last block)" ).String ()
69- analyzeLimit = analyzeCmd .Flag ("limit" , "how many items to show in each list" ).Default ("20" ).Int ()
70+ analyzeLimit = analyzeCmd .Flag ("limit" , "how many items to show in each list" ).Default (defaultAnalyzeLimit ).Int ()
7071 dumpCmd = cli .Command ("dump" , "dump samples from a TSDB" )
7172 dumpPath = dumpCmd .Arg ("db path" , "database path (default is " + defaultDBPath + ")" ).Default (defaultDBPath ).String ()
7273 dumpMinTime = dumpCmd .Flag ("min-time" , "minimum timestamp to dump" ).Default (strconv .FormatInt (math .MinInt64 , 10 )).Int64 ()
@@ -114,21 +115,12 @@ func execute() (err error) {
114115 if err != nil {
115116 return err
116117 }
117- var block tsdb.BlockReader
118- if * analyzeBlockID != "" {
119- for _ , b := range blocks {
120- if b .Meta ().ULID .String () == * analyzeBlockID {
121- block = b
122- break
123- }
124- }
125- } else if len (blocks ) > 0 {
126- block = blocks [len (blocks )- 1 ]
127- }
128- if block == nil {
129- return fmt .Errorf ("block not found" )
118+ block , err := extractBlock (blocks , analyzeBlockID )
119+ if err != nil {
120+ return err
130121 }
131- return analyzeBlock (block , * analyzeLimit )
122+
123+ return analyzeBlock (os .Stdout , block , * analyzeLimit )
132124 case dumpCmd .FullCommand ():
133125 db , err := tsdb .OpenDBReadOnly (* dumpPath , nil )
134126 if err != nil {
@@ -144,6 +136,25 @@ func execute() (err error) {
144136 return nil
145137}
146138
139+ // extractBlock takes a slice of BlockReader and returns a specific block by ID.
140+ func extractBlock (blocks []tsdb.BlockReader , analyzeBlockID * string ) (tsdb.BlockReader , error ) {
141+ var block tsdb.BlockReader
142+ if * analyzeBlockID != "" {
143+ for _ , b := range blocks {
144+ if b .Meta ().ULID .String () == * analyzeBlockID {
145+ block = b
146+ break
147+ }
148+ }
149+ } else if len (blocks ) > 0 {
150+ block = blocks [len (blocks )- 1 ]
151+ }
152+ if block == nil {
153+ return nil , fmt .Errorf ("block not found" )
154+ }
155+ return block , nil
156+ }
157+
147158type writeBenchmark struct {
148159 outPath string
149160 samplesFile string
@@ -465,12 +476,12 @@ func getFormatedTime(timestamp int64, humanReadable *bool) string {
465476 return strconv .FormatInt (timestamp , 10 )
466477}
467478
468- func analyzeBlock (b tsdb.BlockReader , limit int ) error {
479+ func analyzeBlock (w io. Writer , b tsdb.BlockReader , limit int ) error {
469480 meta := b .Meta ()
470- fmt .Printf ( "Block ID: %s\n " , meta .ULID )
481+ fmt .Fprintf ( w , "Block ID: %s\n " , meta .ULID )
471482 // Presume 1ms resolution that Prometheus uses.
472- fmt .Printf ( "Duration: %s\n " , (time .Duration (meta .MaxTime - meta .MinTime ) * 1e6 ).String ())
473- fmt .Printf ( "Series: %d\n " , meta .Stats .NumSeries )
483+ fmt .Fprintf ( w , "Duration: %s\n " , (time .Duration (meta .MaxTime - meta .MinTime ) * 1e6 ).String ())
484+ fmt .Fprintf ( w , "Series: %d\n " , meta .Stats .NumSeries )
474485 ir , err := b .Index ()
475486 if err != nil {
476487 return err
@@ -481,7 +492,7 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
481492 if err != nil {
482493 return err
483494 }
484- fmt .Printf ( "Label names: %d\n " , len (allLabelNames ))
495+ fmt .Fprintf ( w , "Label names: %d\n " , len (allLabelNames ))
485496
486497 type postingInfo struct {
487498 key string
@@ -493,7 +504,7 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
493504 sort .Slice (postingInfos , func (i , j int ) bool { return postingInfos [i ].metric > postingInfos [j ].metric })
494505
495506 for i , pc := range postingInfos {
496- fmt .Printf ( "%d %s\n " , pc .metric , pc .key )
507+ fmt .Fprintf ( w , "%d %s\n " , pc .metric , pc .key )
497508 if i >= limit {
498509 break
499510 }
@@ -527,31 +538,31 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
527538 if p .Err () != nil {
528539 return p .Err ()
529540 }
530- fmt .Printf ( "Postings (unique label pairs): %d\n " , len (labelpairsUncovered ))
531- fmt .Printf ( "Postings entries (total label pairs): %d\n " , entries )
541+ fmt .Fprintf ( w , "Postings (unique label pairs): %d\n " , len (labelpairsUncovered ))
542+ fmt .Fprintf ( w , "Postings entries (total label pairs): %d\n " , entries )
532543
533544 postingInfos = postingInfos [:0 ]
534545 for k , m := range labelpairsUncovered {
535546 postingInfos = append (postingInfos , postingInfo {k , uint64 (float64 (m ) / float64 (meta .MaxTime - meta .MinTime ))})
536547 }
537548
538- fmt .Printf ( "\n Label pairs most involved in churning:\n " )
549+ fmt .Fprintf ( w , "\n Label pairs most involved in churning:\n " )
539550 printInfo (postingInfos )
540551
541552 postingInfos = postingInfos [:0 ]
542553 for k , m := range labelsUncovered {
543554 postingInfos = append (postingInfos , postingInfo {k , uint64 (float64 (m ) / float64 (meta .MaxTime - meta .MinTime ))})
544555 }
545556
546- fmt .Printf ( "\n Label names most involved in churning:\n " )
557+ fmt .Fprintf ( w , "\n Label names most involved in churning:\n " )
547558 printInfo (postingInfos )
548559
549560 postingInfos = postingInfos [:0 ]
550561 for k , m := range labelpairsCount {
551562 postingInfos = append (postingInfos , postingInfo {k , m })
552563 }
553564
554- fmt .Printf ( "\n Most common label pairs:\n " )
565+ fmt .Fprintf ( w , "\n Most common label pairs:\n " )
555566 printInfo (postingInfos )
556567
557568 postingInfos = postingInfos [:0 ]
@@ -575,7 +586,7 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
575586 postingInfos = append (postingInfos , postingInfo {n , cumulativeLength })
576587 }
577588
578- fmt .Printf ( "\n Label names with highest cumulative label value length:\n " )
589+ fmt .Fprintf ( w , "\n Label names with highest cumulative label value length:\n " )
579590 printInfo (postingInfos )
580591
581592 postingInfos = postingInfos [:0 ]
@@ -586,7 +597,7 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
586597 }
587598 postingInfos = append (postingInfos , postingInfo {n , uint64 (lv .Len ())})
588599 }
589- fmt .Printf ( "\n Highest cardinality labels:\n " )
600+ fmt .Fprintf ( w , "\n Highest cardinality labels:\n " )
590601 printInfo (postingInfos )
591602
592603 postingInfos = postingInfos [:0 ]
@@ -614,7 +625,7 @@ func analyzeBlock(b tsdb.BlockReader, limit int) error {
614625 postingInfos = append (postingInfos , postingInfo {n , uint64 (count )})
615626 }
616627 }
617- fmt .Printf ( "\n Highest cardinality metric names:\n " )
628+ fmt .Fprintf ( w , "\n Highest cardinality metric names:\n " )
618629 printInfo (postingInfos )
619630 return nil
620631}
0 commit comments