Просмотр исходного кода

storage: add metrics for db total size

Xiang Li 10 лет назад
Родитель
Сommit
0aa2f1192a
5 измененных файлов с 21 добавлено и 0 удалено
  1. 8 0
      storage/backend/backend.go
  2. 2 0
      storage/backend/batch_tx.go
  3. 2 0
      storage/kvstore.go
  4. 1 0
      storage/kvstore_test.go
  5. 8 0
      storage/metrics.go

+ 8 - 0
storage/backend/backend.go

@@ -19,6 +19,7 @@ import (
 	"hash/crc32"
 	"io"
 	"log"
+	"sync/atomic"
 	"time"
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
@@ -28,6 +29,8 @@ type Backend interface {
 	BatchTx() BatchTx
 	Snapshot() Snapshot
 	Hash() (uint32, error)
+	// Size returns the current size of the backend.
+	Size() int64
 	ForceCommit()
 	Close() error
 }
@@ -47,6 +50,7 @@ type backend struct {
 	batchInterval time.Duration
 	batchLimit    int
 	batchTx       *batchTx
+	size          int64
 
 	stopc chan struct{}
 	donec chan struct{}
@@ -123,6 +127,10 @@ func (b *backend) Hash() (uint32, error) {
 	return h.Sum32(), nil
 }
 
+func (b *backend) Size() int64 {
+	return atomic.LoadInt64(&b.size)
+}
+
 func (b *backend) run() {
 	defer close(b.donec)
 

+ 2 - 0
storage/backend/batch_tx.go

@@ -18,6 +18,7 @@ import (
 	"bytes"
 	"log"
 	"sync"
+	"sync/atomic"
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
 )
@@ -146,4 +147,5 @@ func (t *batchTx) commit(stop bool) {
 	if err != nil {
 		log.Fatalf("storage: cannot begin tx (%s)", err)
 	}
+	atomic.StoreInt64(&t.backend.size, t.tx.Size())
 }

+ 2 - 0
storage/kvstore.go

@@ -151,6 +151,8 @@ func (s *store) txnEnd(txnID int64) error {
 		s.currentRev.main += 1
 	}
 	s.currentRev.sub = 0
+
+	dbTotalSize.Set(float64(s.b.Size()))
 	s.mu.Unlock()
 	return nil
 }

+ 1 - 0
storage/kvstore_test.go

@@ -721,6 +721,7 @@ type fakeBackend struct {
 
 func (b *fakeBackend) BatchTx() backend.BatchTx   { return b.tx }
 func (b *fakeBackend) Hash() (uint32, error)      { return 0, nil }
+func (b *fakeBackend) Size() int64                { return 0 }
 func (b *fakeBackend) Snapshot() backend.Snapshot { return nil }
 func (b *fakeBackend) ForceCommit()               {}
 func (b *fakeBackend) Close() error               { return nil }

+ 8 - 0
storage/metrics.go

@@ -120,6 +120,13 @@ var (
 			// 100ms -> 800second
 			Buckets: prometheus.ExponentialBuckets(100, 2, 14),
 		})
+
+	dbTotalSize = prometheus.NewGauge(prometheus.GaugeOpts{
+		Namespace: "etcd",
+		Subsystem: "storage",
+		Name:      "db_total_size_in_bytes",
+		Help:      "Total size of the underlying database in bytes.",
+	})
 )
 
 func init() {
@@ -135,6 +142,7 @@ func init() {
 	prometheus.MustRegister(indexCompactionPauseDurations)
 	prometheus.MustRegister(dbCompactionPauseDurations)
 	prometheus.MustRegister(dbCompactionTotalDurations)
+	prometheus.MustRegister(dbTotalSize)
 }
 
 // ReportEventReceived reports that an event is received.