@@ -71,6 +71,8 @@ type Database struct {
71
71
seekCompGauge metrics.Gauge // Gauge for tracking the number of table compaction caused by read opt
72
72
manualMemAllocGauge metrics.Gauge // Gauge for tracking amount of non-managed memory currently allocated
73
73
74
+ levelsGauge []metrics.Gauge // Gauge for tracking the number of tables in levels
75
+
74
76
quitLock sync.RWMutex // Mutex protecting the quit channel and the closed flag
75
77
quitChan chan chan error // Quit channel to stop the metrics collection before closing the database
76
78
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
230
232
db .manualMemAllocGauge = metrics .NewRegisteredGauge (namespace + "memory/manualalloc" , nil )
231
233
232
234
// Start up the metrics gathering and return
233
- go db .meter (metricsGatheringInterval )
235
+ go db .meter (metricsGatheringInterval , namespace )
234
236
return db , nil
235
237
}
236
238
@@ -427,7 +429,7 @@ func (d *Database) Path() string {
427
429
428
430
// meter periodically retrieves internal pebble counters and reports them to
429
431
// the metrics subsystem.
430
- func (d * Database ) meter (refresh time.Duration ) {
432
+ func (d * Database ) meter (refresh time.Duration , namespace string ) {
431
433
var errc chan error
432
434
timer := time .NewTimer (refresh )
433
435
defer timer .Stop ()
@@ -450,7 +452,7 @@ func (d *Database) meter(refresh time.Duration) {
450
452
compRead int64
451
453
nWrite int64
452
454
453
- metrics = d .db .Metrics ()
455
+ stats = d .db .Metrics ()
454
456
compTime = d .compTime .Load ()
455
457
writeDelayCount = d .writeDelayCount .Load ()
456
458
writeDelayTime = d .writeDelayTime .Load ()
@@ -461,14 +463,14 @@ func (d *Database) meter(refresh time.Duration) {
461
463
writeDelayCounts [i % 2 ] = writeDelayCount
462
464
compTimes [i % 2 ] = compTime
463
465
464
- for _ , levelMetrics := range metrics .Levels {
466
+ for _ , levelMetrics := range stats .Levels {
465
467
nWrite += int64 (levelMetrics .BytesCompacted )
466
468
nWrite += int64 (levelMetrics .BytesFlushed )
467
469
compWrite += int64 (levelMetrics .BytesCompacted )
468
470
compRead += int64 (levelMetrics .BytesRead )
469
471
}
470
472
471
- nWrite += int64 (metrics .WAL .BytesWritten )
473
+ nWrite += int64 (stats .WAL .BytesWritten )
472
474
473
475
compWrites [i % 2 ] = compWrite
474
476
compReads [i % 2 ] = compRead
@@ -490,7 +492,7 @@ func (d *Database) meter(refresh time.Duration) {
490
492
d .compWriteMeter .Mark (compWrites [i % 2 ] - compWrites [(i - 1 )% 2 ])
491
493
}
492
494
if d .diskSizeGauge != nil {
493
- d .diskSizeGauge .Update (int64 (metrics .DiskSpaceUsage ()))
495
+ d .diskSizeGauge .Update (int64 (stats .DiskSpaceUsage ()))
494
496
}
495
497
if d .diskReadMeter != nil {
496
498
d .diskReadMeter .Mark (0 ) // pebble doesn't track non-compaction reads
@@ -499,12 +501,20 @@ func (d *Database) meter(refresh time.Duration) {
499
501
d .diskWriteMeter .Mark (nWrites [i % 2 ] - nWrites [(i - 1 )% 2 ])
500
502
}
501
503
// 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 )
503
505
d .manualMemAllocGauge .Update (manuallyAllocated )
504
- d .memCompGauge .Update (metrics .Flush .Count )
506
+ d .memCompGauge .Update (stats .Flush .Count )
505
507
d .nonlevel0CompGauge .Update (nonLevel0CompCount )
506
508
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
+ }
508
518
509
519
// Sleep a bit, then repeat the stats collection
510
520
select {
0 commit comments