From 0aa2f1192a2921c695a041e76d06fe5a492410d8 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Mon, 5 Oct 2015 16:15:44 -0700 Subject: [PATCH] storage: add metrics for db total size --- storage/backend/backend.go | 8 ++++++++ storage/backend/batch_tx.go | 2 ++ storage/kvstore.go | 2 ++ storage/kvstore_test.go | 1 + storage/metrics.go | 8 ++++++++ 5 files changed, 21 insertions(+) diff --git a/storage/backend/backend.go b/storage/backend/backend.go index d4f054b32..b8a2f2ad4 100644 --- a/storage/backend/backend.go +++ b/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) diff --git a/storage/backend/batch_tx.go b/storage/backend/batch_tx.go index 403797e0d..2fec3c6e8 100644 --- a/storage/backend/batch_tx.go +++ b/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()) } diff --git a/storage/kvstore.go b/storage/kvstore.go index 628348801..835c48106 100644 --- a/storage/kvstore.go +++ b/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 } diff --git a/storage/kvstore_test.go b/storage/kvstore_test.go index c1bc0d195..192460f82 100644 --- a/storage/kvstore_test.go +++ b/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 } diff --git a/storage/metrics.go b/storage/metrics.go index 3763990c0..772b7e242 100644 --- a/storage/metrics.go +++ b/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.