Commit aa8e5d36 authored by acud's avatar acud Committed by GitHub

localstore: improve metrics (#1427)

parent 44543853
...@@ -79,12 +79,12 @@ func (db *DB) collectGarbageWorker() { ...@@ -79,12 +79,12 @@ func (db *DB) collectGarbageWorker() {
// This function is called in collectGarbageWorker. // This function is called in collectGarbageWorker.
func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
db.metrics.GCCounter.Inc() db.metrics.GCCounter.Inc()
defer totalTimeMetric(db.metrics.TotalTimeCollectGarbage, time.Now()) defer func(start time.Time) {
defer func() {
if err != nil { if err != nil {
db.metrics.GCErrorCounter.Inc() db.metrics.GCErrorCounter.Inc()
} }
}() totalTimeMetric(db.metrics.TotalTimeCollectGarbage, start)
}(time.Now())
batch := new(leveldb.Batch) batch := new(leveldb.Batch)
target := db.gcTarget() target := db.gcTarget()
...@@ -107,7 +107,13 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { ...@@ -107,7 +107,13 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
db.metrics.GCSize.Set(float64(gcSize)) db.metrics.GCSize.Set(float64(gcSize))
done = true done = true
first := true
start := time.Now()
err = db.gcIndex.Iterate(func(item shed.Item) (stop bool, err error) { err = db.gcIndex.Iterate(func(item shed.Item) (stop bool, err error) {
if first {
totalTimeMetric(db.metrics.TotalTimeGCFirstItem, start)
first = false
}
if gcSize-collectedCount <= target { if gcSize-collectedCount <= target {
return true, nil return true, nil
} }
...@@ -144,13 +150,12 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) { ...@@ -144,13 +150,12 @@ func (db *DB) collectGarbage() (collectedCount uint64, done bool, err error) {
if err != nil { if err != nil {
return 0, false, err return 0, false, err
} }
db.metrics.GCCollectedCounter.Inc() db.metrics.GCCollectedCounter.Add(float64(collectedCount))
db.metrics.GCSize.Set(float64(gcSize - collectedCount))
db.gcSize.PutInBatch(batch, gcSize-collectedCount) db.gcSize.PutInBatch(batch, gcSize-collectedCount)
err = db.shed.WriteBatch(batch) err = db.shed.WriteBatch(batch)
if err != nil { if err != nil {
db.metrics.GCExcludeWriteBatchError.Inc() db.metrics.GCErrorCounter.Inc()
return 0, false, err return 0, false, err
} }
return collectedCount, done, nil return collectedCount, done, nil
...@@ -216,7 +221,7 @@ func (db *DB) removeChunksInExcludeIndexFromGC() (err error) { ...@@ -216,7 +221,7 @@ func (db *DB) removeChunksInExcludeIndexFromGC() (err error) {
return err return err
} }
db.metrics.GCExcludeCounter.Inc() db.metrics.GCExcludeCounter.Add(float64(excludedCount))
err = db.shed.WriteBatch(batch) err = db.shed.WriteBatch(batch)
if err != nil { if err != nil {
db.metrics.GCExcludeWriteBatchError.Inc() db.metrics.GCExcludeWriteBatchError.Inc()
......
...@@ -10,10 +10,7 @@ import ( ...@@ -10,10 +10,7 @@ import (
) )
type metrics struct { type metrics struct {
// all metrics fields must be exported TotalTimeGCFirstItem prometheus.Counter
// to be able to return them by Metrics()
// using reflection
TotalTimeCollectGarbage prometheus.Counter TotalTimeCollectGarbage prometheus.Counter
TotalTimeGCExclude prometheus.Counter TotalTimeGCExclude prometheus.Counter
TotalTimeGet prometheus.Counter TotalTimeGet prometheus.Counter
...@@ -29,10 +26,8 @@ type metrics struct { ...@@ -29,10 +26,8 @@ type metrics struct {
GCCounter prometheus.Counter GCCounter prometheus.Counter
GCErrorCounter prometheus.Counter GCErrorCounter prometheus.Counter
GCCollectedCounter prometheus.Counter GCCollectedCounter prometheus.Counter
GCWriteBatchError prometheus.Counter
GCExcludeCounter prometheus.Counter GCExcludeCounter prometheus.Counter
GCExcludeError prometheus.Counter GCExcludeError prometheus.Counter
GCExcludedCounter prometheus.Counter
GCExcludeWriteBatchError prometheus.Counter GCExcludeWriteBatchError prometheus.Counter
GCUpdate prometheus.Counter GCUpdate prometheus.Counter
GCUpdateError prometheus.Counter GCUpdateError prometheus.Counter
...@@ -68,6 +63,12 @@ func newMetrics() metrics { ...@@ -68,6 +63,12 @@ func newMetrics() metrics {
subsystem := "localstore" subsystem := "localstore"
return metrics{ return metrics{
TotalTimeGCFirstItem: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "gc_first_item_time",
Help: "Total time taken till first item in gc comes out of gcIndex iterator.",
}),
TotalTimeCollectGarbage: prometheus.NewCounter(prometheus.CounterOpts{ TotalTimeCollectGarbage: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace, Namespace: m.Namespace,
Subsystem: subsystem, Subsystem: subsystem,
...@@ -152,12 +153,6 @@ func newMetrics() metrics { ...@@ -152,12 +153,6 @@ func newMetrics() metrics {
Name: "gc_collected_count", Name: "gc_collected_count",
Help: "Number of times the GC_COLLECTED operation is done.", Help: "Number of times the GC_COLLECTED operation is done.",
}), }),
GCWriteBatchError: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "gc_write_batch_error_count",
Help: "Number of times the GC_WRITE_BATCH operation failed.",
}),
GCExcludeCounter: prometheus.NewCounter(prometheus.CounterOpts{ GCExcludeCounter: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace, Namespace: m.Namespace,
Subsystem: subsystem, Subsystem: subsystem,
...@@ -170,12 +165,6 @@ func newMetrics() metrics { ...@@ -170,12 +165,6 @@ func newMetrics() metrics {
Name: "gc_exclude_fail_count", Name: "gc_exclude_fail_count",
Help: "Number of times the GC_EXCLUDE operation failed.", Help: "Number of times the GC_EXCLUDE operation failed.",
}), }),
GCExcludedCounter: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace,
Subsystem: subsystem,
Name: "gc_excluded_count",
Help: "Number of times the GC_EXCLUDED operation is done.",
}),
GCExcludeWriteBatchError: prometheus.NewCounter(prometheus.CounterOpts{ GCExcludeWriteBatchError: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace, Namespace: m.Namespace,
Subsystem: subsystem, Subsystem: subsystem,
...@@ -191,7 +180,7 @@ func newMetrics() metrics { ...@@ -191,7 +180,7 @@ func newMetrics() metrics {
GCUpdateError: prometheus.NewCounter(prometheus.CounterOpts{ GCUpdateError: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: m.Namespace, Namespace: m.Namespace,
Subsystem: subsystem, Subsystem: subsystem,
Name: "fc_update_error_count", Name: "gc_update_error_count",
Help: "Number of times the gc update had error.", Help: "Number of times the gc update had error.",
}), }),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment