2023-05-19 08:17:19 +00:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/metrics"
|
2023-05-19 08:17:19 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
wcSubsystem = "writecache"
|
|
|
|
wcShardID = "shard_id"
|
|
|
|
wcSuccess = "success"
|
|
|
|
wcStorage = "storage"
|
|
|
|
wcMode = "mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type shardIDMode struct {
|
|
|
|
shardID, mode string
|
|
|
|
}
|
|
|
|
|
|
|
|
type WriteCacheMetrics interface {
|
2023-05-25 14:46:24 +00:00
|
|
|
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)
|
2023-05-19 08:17:19 +00:00
|
|
|
|
|
|
|
IncActualCount(shardID string, storageType string)
|
|
|
|
DecActualCount(shardID string, storageType string)
|
|
|
|
SetActualCount(shardID string, count uint64, storageType string)
|
|
|
|
|
|
|
|
SetEstimateSize(shardID string, size uint64, storageType string)
|
|
|
|
SetMode(shardID string, mode string)
|
|
|
|
|
|
|
|
IncFlushCounter(shardID string, success bool, storageType string)
|
|
|
|
IncEvictCounter(shardID string, storageType string)
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeCacheMetrics struct {
|
2023-05-31 09:25:32 +00:00
|
|
|
getDuration *prometheus.HistogramVec
|
|
|
|
putDuration *prometheus.HistogramVec
|
|
|
|
deleteDuration *prometheus.HistogramVec
|
2023-05-19 08:17:19 +00:00
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
flushCounter *prometheus.CounterVec
|
|
|
|
evictCounter *prometheus.CounterVec
|
2023-05-19 08:17:19 +00:00
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
actualCount *prometheus.GaugeVec
|
2023-05-19 08:17:19 +00:00
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
estimatedSize *prometheus.GaugeVec
|
2023-05-19 08:17:19 +00:00
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
modeMetrics map[shardIDMode]prometheus.GaugeFunc
|
2023-05-19 08:17:19 +00:00
|
|
|
modeValues map[string]string
|
|
|
|
modeMtx sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
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}),
|
|
|
|
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{},
|
2023-05-31 09:25:32 +00:00
|
|
|
modeMetrics: make(map[shardIDMode]prometheus.GaugeFunc),
|
2023-05-19 08:17:19 +00:00
|
|
|
modeValues: make(map[string]string),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:46:24 +00:00
|
|
|
func (m *writeCacheMetrics) AddGetDuration(shardID string, success bool, d time.Duration, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
setWriteCacheDuration(m.getDuration, shardID, success, d, storageType)
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 14:46:24 +00:00
|
|
|
func (m *writeCacheMetrics) AddDeleteDuration(shardID string, success bool, d time.Duration, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
setWriteCacheDuration(m.deleteDuration, shardID, success, d, storageType)
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 14:46:24 +00:00
|
|
|
func (m *writeCacheMetrics) AddPutDuration(shardID string, success bool, d time.Duration, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
setWriteCacheDuration(m.putDuration, shardID, success, d, storageType)
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) IncActualCount(shardID string, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.actualCount.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) DecActualCount(shardID string, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.actualCount.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Dec()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetActualCount(shardID string, count uint64, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.actualCount.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Set(float64(count))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetEstimateSize(shardID string, size uint64, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.estimatedSize.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Set(float64(size))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) SetMode(shardID string, mode string) {
|
|
|
|
m.modeMtx.Lock()
|
|
|
|
defer m.modeMtx.Unlock()
|
|
|
|
|
|
|
|
m.modeValues[shardID] = mode
|
|
|
|
key := shardIDMode{
|
|
|
|
shardID: shardID,
|
|
|
|
mode: mode,
|
|
|
|
}
|
|
|
|
if _, found := m.modeMetrics[key]; found {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
metric := metrics.NewGaugeFunc(
|
2023-05-19 08:17:19 +00:00
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: wcSubsystem,
|
|
|
|
Name: "writecache_mode",
|
|
|
|
Help: "Writecache mode value",
|
|
|
|
ConstLabels: prometheus.Labels{
|
|
|
|
wcShardID: shardID,
|
|
|
|
wcMode: mode,
|
|
|
|
},
|
|
|
|
}, func() float64 {
|
|
|
|
m.modeMtx.RLock()
|
|
|
|
defer m.modeMtx.RUnlock()
|
|
|
|
|
|
|
|
value := m.modeValues[shardID]
|
|
|
|
if value == mode {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
})
|
|
|
|
m.modeMetrics[key] = metric
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) IncFlushCounter(shardID string, success bool, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.flushCounter.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcSuccess: fmt.Sprintf("%v", success),
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Inc()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *writeCacheMetrics) IncEvictCounter(shardID string, storageType string) {
|
2023-05-31 09:25:32 +00:00
|
|
|
m.evictCounter.With(prometheus.Labels{
|
2023-05-19 08:17:19 +00:00
|
|
|
wcShardID: shardID,
|
|
|
|
wcStorage: storageType,
|
|
|
|
}).Inc()
|
|
|
|
}
|
|
|
|
|
2023-05-25 14:46:24 +00:00
|
|
|
func setWriteCacheDuration(m *prometheus.HistogramVec, shardID string, success bool, d time.Duration, storageType string) {
|
2023-05-19 08:17:19 +00:00
|
|
|
m.With(
|
|
|
|
prometheus.Labels{
|
|
|
|
wcShardID: shardID,
|
|
|
|
wcSuccess: fmt.Sprintf("%v", success),
|
2023-05-25 14:46:24 +00:00
|
|
|
wcStorage: storageType,
|
2023-05-19 08:17:19 +00:00
|
|
|
},
|
|
|
|
).Observe(float64(d))
|
|
|
|
}
|
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
func newWCMethodDurationCounter(method string) *prometheus.HistogramVec {
|
|
|
|
return metrics.NewHistogramVec(prometheus.HistogramOpts{
|
2023-05-19 08:17:19 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: wcSubsystem,
|
|
|
|
Name: fmt.Sprintf("%s_req_duration_seconds", method),
|
|
|
|
Help: fmt.Sprintf("Accumulated %s request process duration", method),
|
2023-06-07 06:35:19 +00:00
|
|
|
}, []string{wcShardID, wcSuccess, wcStorage})
|
2023-05-19 08:17:19 +00:00
|
|
|
}
|
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
func newWCOperationCounterVec(operation string, labels []string) *prometheus.CounterVec {
|
|
|
|
return metrics.NewCounterVec(prometheus.CounterOpts{
|
2023-05-19 08:17:19 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: wcSubsystem,
|
|
|
|
Name: fmt.Sprintf("%s_operation_count", operation),
|
|
|
|
Help: fmt.Sprintf("The number of %s operations processed", operation),
|
|
|
|
}, labels)
|
|
|
|
}
|
|
|
|
|
2023-05-31 09:25:32 +00:00
|
|
|
func newWCGaugeVec(name, help string, labels []string) *prometheus.GaugeVec {
|
|
|
|
return metrics.NewGaugeVec(prometheus.GaugeOpts{
|
2023-05-19 08:17:19 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: wcSubsystem,
|
|
|
|
Name: name,
|
|
|
|
Help: help,
|
|
|
|
}, labels)
|
|
|
|
}
|