@@ -71,6 +71,8 @@ type Database struct {
7171 seekCompGauge metrics.Gauge // Gauge for tracking the number of table compaction caused by read opt
7272 manualMemAllocGauge metrics.Gauge // Gauge for tracking amount of non-managed memory currently allocated
7373
74+ levelsGauge []metrics.Gauge // Gauge for tracking the number of tables in levels
75+
7476 quitLock sync.RWMutex // Mutex protecting the quit channel and the closed flag
7577 quitChan chan chan error // Quit channel to stop the metrics collection before closing the database
7678 closed bool // keep track of whether we're Closed
@@ -230,7 +232,7 @@ func New(file string, cache int, handles int, namespace string, readonly bool, e
230232 db .manualMemAllocGauge = metrics .NewRegisteredGauge (namespace + "memory/manualalloc" , nil )
231233
232234 // Start up the metrics gathering and return
233- go db .meter (metricsGatheringInterval )
235+ go db .meter (metricsGatheringInterval , namespace )
234236 return db , nil
235237}
236238
@@ -427,7 +429,7 @@ func (d *Database) Path() string {
427429
428430// meter periodically retrieves internal pebble counters and reports them to
429431// the metrics subsystem.
430- func (d * Database ) meter (refresh time.Duration ) {
432+ func (d * Database ) meter (refresh time.Duration , namespace string ) {
431433 var errc chan error
432434 timer := time .NewTimer (refresh )
433435 defer timer .Stop ()
@@ -450,7 +452,7 @@ func (d *Database) meter(refresh time.Duration) {
450452 compRead int64
451453 nWrite int64
452454
453- metrics = d .db .Metrics ()
455+ stats = d .db .Metrics ()
454456 compTime = d .compTime .Load ()
455457 writeDelayCount = d .writeDelayCount .Load ()
456458 writeDelayTime = d .writeDelayTime .Load ()
@@ -461,14 +463,14 @@ func (d *Database) meter(refresh time.Duration) {
461463 writeDelayCounts [i % 2 ] = writeDelayCount
462464 compTimes [i % 2 ] = compTime
463465
464- for _ , levelMetrics := range metrics .Levels {
466+ for _ , levelMetrics := range stats .Levels {
465467 nWrite += int64 (levelMetrics .BytesCompacted )
466468 nWrite += int64 (levelMetrics .BytesFlushed )
467469 compWrite += int64 (levelMetrics .BytesCompacted )
468470 compRead += int64 (levelMetrics .BytesRead )
469471 }
470472
471- nWrite += int64 (metrics .WAL .BytesWritten )
473+ nWrite += int64 (stats .WAL .BytesWritten )
472474
473475 compWrites [i % 2 ] = compWrite
474476 compReads [i % 2 ] = compRead
@@ -490,7 +492,7 @@ func (d *Database) meter(refresh time.Duration) {
490492 d .compWriteMeter .Mark (compWrites [i % 2 ] - compWrites [(i - 1 )% 2 ])
491493 }
492494 if d .diskSizeGauge != nil {
493- d .diskSizeGauge .Update (int64 (metrics .DiskSpaceUsage ()))
495+ d .diskSizeGauge .Update (int64 (stats .DiskSpaceUsage ()))
494496 }
495497 if d .diskReadMeter != nil {
496498 d .diskReadMeter .Mark (0 ) // pebble doesn't track non-compaction reads
@@ -499,12 +501,20 @@ func (d *Database) meter(refresh time.Duration) {
499501 d .diskWriteMeter .Mark (nWrites [i % 2 ] - nWrites [(i - 1 )% 2 ])
500502 }
501503 // See https://github.com/cockroachdb/pebble/pull/1628#pullrequestreview-1026664054
502- manuallyAllocated := metrics .BlockCache .Size + int64 (metrics .MemTable .Size ) + int64 (metrics .MemTable .ZombieSize )
504+ manuallyAllocated := stats .BlockCache .Size + int64 (stats .MemTable .Size ) + int64 (stats .MemTable .ZombieSize )
503505 d .manualMemAllocGauge .Update (manuallyAllocated )
504- d .memCompGauge .Update (metrics .Flush .Count )
506+ d .memCompGauge .Update (stats .Flush .Count )
505507 d .nonlevel0CompGauge .Update (nonLevel0CompCount )
506508 d .level0CompGauge .Update (level0CompCount )
507- d .seekCompGauge .Update (metrics .Compact .ReadCount )
509+ d .seekCompGauge .Update (stats .Compact .ReadCount )
510+
511+ for i , level := range stats .Levels {
512+ // Append metrics for additional layers
513+ if i >= len (d .levelsGauge ) {
514+ d .levelsGauge = append (d .levelsGauge , metrics .NewRegisteredGauge (namespace + fmt .Sprintf ("tables/level%v" , i ), nil ))
515+ }
516+ d .levelsGauge [i ].Update (level .NumFiles )
517+ }
508518
509519 // Sleep a bit, then repeat the stats collection
510520 select {
0 commit comments