[#424] metrics: Use labels for writecache methods and operations

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
Dmitrii Stepanov 2023-06-13 15:05:45 +03:00
parent 71bbeddb64
commit 07f155ac77
3 changed files with 60 additions and 77 deletions

View file

@ -63,18 +63,18 @@ type writeCacheMetrics struct {
} }
func (m *writeCacheMetrics) Get(d time.Duration, success bool, st writecache.StorageType) { func (m *writeCacheMetrics) Get(d time.Duration, success bool, st writecache.StorageType) {
m.metrics.AddGetDuration(m.shardID, success, d, st.String()) m.metrics.AddMethodDuration(m.shardID, "Get", success, d, st.String())
} }
func (m *writeCacheMetrics) Delete(d time.Duration, success bool, st writecache.StorageType) { func (m *writeCacheMetrics) Delete(d time.Duration, success bool, st writecache.StorageType) {
m.metrics.AddDeleteDuration(m.shardID, success, d, st.String()) m.metrics.AddMethodDuration(m.shardID, "Delete", success, d, st.String())
if success { if success {
m.metrics.DecActualCount(m.shardID, st.String()) m.metrics.DecActualCount(m.shardID, st.String())
} }
} }
func (m *writeCacheMetrics) Put(d time.Duration, success bool, st writecache.StorageType) { func (m *writeCacheMetrics) Put(d time.Duration, success bool, st writecache.StorageType) {
m.metrics.AddPutDuration(m.shardID, success, d, st.String()) m.metrics.AddMethodDuration(m.shardID, "Put", success, d, st.String())
if success { if success {
m.metrics.IncActualCount(m.shardID, st.String()) m.metrics.IncActualCount(m.shardID, st.String())
} }
@ -95,10 +95,10 @@ func (m *writeCacheMetrics) SetActualCounters(db, fstree uint64) {
} }
func (m *writeCacheMetrics) Flush(success bool, st writecache.StorageType) { func (m *writeCacheMetrics) Flush(success bool, st writecache.StorageType) {
m.metrics.IncFlushCounter(m.shardID, success, st.String()) m.metrics.IncOperationCounter(m.shardID, "Flush", metrics.NullBool{Bool: success, Valid: true}, st.String())
} }
func (m *writeCacheMetrics) Evict(st writecache.StorageType) { func (m *writeCacheMetrics) Evict(st writecache.StorageType) {
m.metrics.DecActualCount(m.shardID, st.String()) m.metrics.DecActualCount(m.shardID, st.String())
m.metrics.IncEvictCounter(m.shardID, st.String()) m.metrics.IncOperationCounter(m.shardID, "Evict", metrics.NullBool{}, st.String())
} }

17
pkg/metrics/types.go Normal file
View file

@ -0,0 +1,17 @@
package metrics
import (
"strconv"
)
type NullBool struct {
Bool bool
Valid bool // Valid is true if Bool is not NULL
}
func (v NullBool) String() string {
if !v.Valid {
return ""
}
return strconv.FormatBool(v.Bool)
}

View file

@ -2,7 +2,6 @@ package metrics
import ( import (
"fmt" "fmt"
"strconv"
"sync" "sync"
"time" "time"
@ -16,6 +15,8 @@ const (
wcSuccess = "success" wcSuccess = "success"
wcStorage = "storage" wcStorage = "storage"
wcMode = "mode" wcMode = "mode"
wcMethod = "method"
wcOperation = "operation"
) )
type shardIDMode struct { type shardIDMode struct {
@ -23,9 +24,7 @@ type shardIDMode struct {
} }
type WriteCacheMetrics interface { type WriteCacheMetrics interface {
AddGetDuration(shardID string, success bool, d time.Duration, storageType string) AddMethodDuration(shardID string, method string, success bool, d time.Duration, storageType string)
AddDeleteDuration(shardID string, success bool, d time.Duration, storageType string)
AddPutDuration(shardID string, success bool, d time.Duration, storageType string)
IncActualCount(shardID string, storageType string) IncActualCount(shardID string, storageType string)
DecActualCount(shardID string, storageType string) DecActualCount(shardID string, storageType string)
@ -34,17 +33,12 @@ type WriteCacheMetrics interface {
SetEstimateSize(shardID string, size uint64, storageType string) SetEstimateSize(shardID string, size uint64, storageType string)
SetMode(shardID string, mode string) SetMode(shardID string, mode string)
IncFlushCounter(shardID string, success bool, storageType string) IncOperationCounter(shardID string, operation string, success NullBool, storageType string)
IncEvictCounter(shardID string, storageType string)
} }
type writeCacheMetrics struct { type writeCacheMetrics struct {
getDuration *prometheus.HistogramVec methodDuration *prometheus.HistogramVec
putDuration *prometheus.HistogramVec operationCounter *prometheus.CounterVec
deleteDuration *prometheus.HistogramVec
flushCounter *prometheus.CounterVec
evictCounter *prometheus.CounterVec
actualCount *prometheus.GaugeVec actualCount *prometheus.GaugeVec
@ -57,29 +51,35 @@ type writeCacheMetrics struct {
func newWriteCacheMetrics() *writeCacheMetrics { func newWriteCacheMetrics() *writeCacheMetrics {
return &writeCacheMetrics{ return &writeCacheMetrics{
getDuration: newWCMethodDurationCounter("get"), methodDuration: metrics.NewHistogramVec(prometheus.HistogramOpts{
putDuration: newWCMethodDurationCounter("put"), Namespace: namespace,
deleteDuration: newWCMethodDurationCounter("delete"), Subsystem: wcSubsystem,
flushCounter: newWCOperationCounterVec("flush", []string{wcShardID, wcStorage, wcSuccess}), Name: "request_duration_seconds",
evictCounter: newWCOperationCounterVec("evict", []string{wcShardID, wcStorage}), Help: "Writecache request process duration",
actualCount: newWCGaugeVec("actual_objects_count", "Actual objects count in writecache", []string{wcShardID, wcStorage}), }, []string{wcShardID, wcSuccess, wcStorage, wcMethod}),
estimatedSize: newWCGaugeVec("estimated_size_bytes", "Estimated writecache size", []string{wcShardID, wcStorage}), operationCounter: metrics.NewCounterVec(prometheus.CounterOpts{
modeMtx: sync.RWMutex{}, Namespace: namespace,
modeMetrics: make(map[shardIDMode]prometheus.GaugeFunc), Subsystem: wcSubsystem,
modeValues: make(map[string]string), Name: "operation_count",
Help: "The number of writecache operations processed",
}, []string{wcShardID, wcStorage, wcSuccess, wcOperation}),
actualCount: newWCGaugeVec("actual_objects_count", "Actual objects count in writecache", []string{wcShardID, wcStorage}),
estimatedSize: newWCGaugeVec("estimated_size_bytes", "Estimated writecache size", []string{wcShardID, wcStorage}),
modeMtx: sync.RWMutex{},
modeMetrics: make(map[shardIDMode]prometheus.GaugeFunc),
modeValues: make(map[string]string),
} }
} }
func (m *writeCacheMetrics) AddGetDuration(shardID string, success bool, d time.Duration, storageType string) { func (m *writeCacheMetrics) AddMethodDuration(shardID string, method string, success bool, d time.Duration, storageType string) {
setWriteCacheDuration(m.getDuration, shardID, success, d, storageType) m.methodDuration.With(
} prometheus.Labels{
wcShardID: shardID,
func (m *writeCacheMetrics) AddDeleteDuration(shardID string, success bool, d time.Duration, storageType string) { wcSuccess: fmt.Sprintf("%v", success),
setWriteCacheDuration(m.deleteDuration, shardID, success, d, storageType) wcStorage: storageType,
} wcMethod: method,
},
func (m *writeCacheMetrics) AddPutDuration(shardID string, success bool, d time.Duration, storageType string) { ).Observe(d.Seconds())
setWriteCacheDuration(m.putDuration, shardID, success, d, storageType)
} }
func (m *writeCacheMetrics) IncActualCount(shardID string, storageType string) { func (m *writeCacheMetrics) IncActualCount(shardID string, storageType string) {
@ -146,49 +146,15 @@ func (m *writeCacheMetrics) SetMode(shardID string, mode string) {
m.modeMetrics[key] = metric m.modeMetrics[key] = metric
} }
func (m *writeCacheMetrics) IncFlushCounter(shardID string, success bool, storageType string) { func (m *writeCacheMetrics) IncOperationCounter(shardID string, operation string, success NullBool, storageType string) {
m.flushCounter.With(prometheus.Labels{ m.operationCounter.With(prometheus.Labels{
wcShardID: shardID, wcShardID: shardID,
wcSuccess: strconv.FormatBool(success), wcStorage: storageType,
wcStorage: storageType, wcOperation: operation,
wcSuccess: success.String(),
}).Inc() }).Inc()
} }
func (m *writeCacheMetrics) IncEvictCounter(shardID string, storageType string) {
m.evictCounter.With(prometheus.Labels{
wcShardID: shardID,
wcStorage: storageType,
}).Inc()
}
func setWriteCacheDuration(m *prometheus.HistogramVec, shardID string, success bool, d time.Duration, storageType string) {
m.With(
prometheus.Labels{
wcShardID: shardID,
wcSuccess: strconv.FormatBool(success),
wcStorage: storageType,
},
).Observe(d.Seconds())
}
func newWCMethodDurationCounter(method string) *prometheus.HistogramVec {
return metrics.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: wcSubsystem,
Name: fmt.Sprintf("%s_req_duration_seconds", method),
Help: fmt.Sprintf("Accumulated %s request process duration", method),
}, []string{wcShardID, wcSuccess, wcStorage})
}
func newWCOperationCounterVec(operation string, labels []string) *prometheus.CounterVec {
return metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: wcSubsystem,
Name: fmt.Sprintf("%s_operation_count", operation),
Help: fmt.Sprintf("The number of %s operations processed", operation),
}, labels)
}
func newWCGaugeVec(name, help string, labels []string) *prometheus.GaugeVec { func newWCGaugeVec(name, help string, labels []string) *prometheus.GaugeVec {
return metrics.NewGaugeVec(prometheus.GaugeOpts{ return metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace, Namespace: namespace,