[#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) {
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) {
m.metrics.AddDeleteDuration(m.shardID, success, d, st.String())
m.metrics.AddMethodDuration(m.shardID, "Delete", success, d, st.String())
if success {
m.metrics.DecActualCount(m.shardID, st.String())
}
}
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 {
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) {
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) {
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 (
"fmt"
"strconv"
"sync"
"time"
@ -16,6 +15,8 @@ const (
wcSuccess = "success"
wcStorage = "storage"
wcMode = "mode"
wcMethod = "method"
wcOperation = "operation"
)
type shardIDMode struct {
@ -23,9 +24,7 @@ type shardIDMode struct {
}
type WriteCacheMetrics interface {
AddGetDuration(shardID 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)
AddMethodDuration(shardID string, method string, success bool, d time.Duration, storageType string)
IncActualCount(shardID string, storageType string)
DecActualCount(shardID string, storageType string)
@ -34,17 +33,12 @@ type WriteCacheMetrics interface {
SetEstimateSize(shardID string, size uint64, storageType string)
SetMode(shardID string, mode string)
IncFlushCounter(shardID string, success bool, storageType string)
IncEvictCounter(shardID string, storageType string)
IncOperationCounter(shardID string, operation string, success NullBool, storageType string)
}
type writeCacheMetrics struct {
getDuration *prometheus.HistogramVec
putDuration *prometheus.HistogramVec
deleteDuration *prometheus.HistogramVec
flushCounter *prometheus.CounterVec
evictCounter *prometheus.CounterVec
methodDuration *prometheus.HistogramVec
operationCounter *prometheus.CounterVec
actualCount *prometheus.GaugeVec
@ -57,11 +51,18 @@ type writeCacheMetrics struct {
func newWriteCacheMetrics() *writeCacheMetrics {
return &writeCacheMetrics{
getDuration: newWCMethodDurationCounter("get"),
putDuration: newWCMethodDurationCounter("put"),
deleteDuration: newWCMethodDurationCounter("delete"),
flushCounter: newWCOperationCounterVec("flush", []string{wcShardID, wcStorage, wcSuccess}),
evictCounter: newWCOperationCounterVec("evict", []string{wcShardID, wcStorage}),
methodDuration: metrics.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Subsystem: wcSubsystem,
Name: "request_duration_seconds",
Help: "Writecache request process duration",
}, []string{wcShardID, wcSuccess, wcStorage, wcMethod}),
operationCounter: metrics.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: wcSubsystem,
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{},
@ -70,16 +71,15 @@ func newWriteCacheMetrics() *writeCacheMetrics {
}
}
func (m *writeCacheMetrics) AddGetDuration(shardID string, success bool, d time.Duration, storageType string) {
setWriteCacheDuration(m.getDuration, shardID, success, d, storageType)
}
func (m *writeCacheMetrics) AddDeleteDuration(shardID string, success bool, d time.Duration, storageType string) {
setWriteCacheDuration(m.deleteDuration, shardID, success, d, storageType)
}
func (m *writeCacheMetrics) AddPutDuration(shardID string, success bool, d time.Duration, storageType string) {
setWriteCacheDuration(m.putDuration, shardID, success, d, storageType)
func (m *writeCacheMetrics) AddMethodDuration(shardID string, method string, success bool, d time.Duration, storageType string) {
m.methodDuration.With(
prometheus.Labels{
wcShardID: shardID,
wcSuccess: fmt.Sprintf("%v", success),
wcStorage: storageType,
wcMethod: method,
},
).Observe(d.Seconds())
}
func (m *writeCacheMetrics) IncActualCount(shardID string, storageType string) {
@ -146,49 +146,15 @@ func (m *writeCacheMetrics) SetMode(shardID string, mode string) {
m.modeMetrics[key] = metric
}
func (m *writeCacheMetrics) IncFlushCounter(shardID string, success bool, storageType string) {
m.flushCounter.With(prometheus.Labels{
func (m *writeCacheMetrics) IncOperationCounter(shardID string, operation string, success NullBool, storageType string) {
m.operationCounter.With(prometheus.Labels{
wcShardID: shardID,
wcSuccess: strconv.FormatBool(success),
wcStorage: storageType,
wcOperation: operation,
wcSuccess: success.String(),
}).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 {
return metrics.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,