From 38ae71cc7d4f2221a7771c98c836598c4f79d265 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Wed, 5 Apr 2023 12:28:41 +0300 Subject: [PATCH] [#213] metrics: Refactor engine metrics Resolve funlen linter for newEngineMetrics function. Signed-off-by: Dmitrii Stepanov --- pkg/metrics/engine.go | 147 ++++++++++++------------------------------ 1 file changed, 40 insertions(+), 107 deletions(-) diff --git a/pkg/metrics/engine.go b/pkg/metrics/engine.go index fbc184832..4c51a55aa 100644 --- a/pkg/metrics/engine.go +++ b/pkg/metrics/engine.go @@ -1,6 +1,8 @@ package metrics import ( + "fmt" + "strings" "time" "github.com/prometheus/client_golang/prometheus" @@ -26,118 +28,49 @@ type ( const engineSubsystem = "engine" -// nolint: funlen func newEngineMetrics() engineMetrics { - var ( - listContainersDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "list_containers_duration", - Help: "Accumulated duration of engine list containers operations", - }) - - estimateContainerSizeDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "estimate_container_size_duration", - Help: "Accumulated duration of engine container size estimate operations", - }) - - deleteDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "delete_duration", - Help: "Accumulated duration of engine delete operations", - }) - - existsDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "exists_duration", - Help: "Accumulated duration of engine exists operations", - }) - - getDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "get_duration", - Help: "Accumulated duration of engine get operations", - }) - - headDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "head_duration", - Help: "Accumulated duration of engine head operations", - }) - - inhumeDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "inhume_duration", - Help: "Accumulated duration of engine inhume operations", - }) - - putDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "put_duration", - Help: "Accumulated duration of engine put operations", - }) - - rangeDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "range_duration", - Help: "Accumulated duration of engine range operations", - }) - - searchDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "search_duration", - Help: "Accumulated duration of engine search operations", - }) - - listObjectsDuration = prometheus.NewCounter(prometheus.CounterOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "list_objects_duration", - Help: "Accumulated duration of engine list objects operations", - }) - - containerSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "container_size", - Help: "Accumulated size of all objects in a container", - }, []string{containerIDLabelKey}) - - payloadSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: namespace, - Subsystem: engineSubsystem, - Name: "payload_size", - Help: "Accumulated size of all objects in a shard", - }, []string{shardIDLabelKey}) - ) - return engineMetrics{ - listContainersDuration: listContainersDuration, - estimateContainerSizeDuration: estimateContainerSizeDuration, - deleteDuration: deleteDuration, - existsDuration: existsDuration, - getDuration: getDuration, - headDuration: headDuration, - inhumeDuration: inhumeDuration, - putDuration: putDuration, - rangeDuration: rangeDuration, - searchDuration: searchDuration, - listObjectsDuration: listObjectsDuration, - containerSize: *containerSize, - payloadSize: *payloadSize, + listContainersDuration: newEngineMethodDurationCounter("list_containers_"), + estimateContainerSizeDuration: newEngineCounter("estimate_container_size_duration", "Accumulated duration of engine container size estimate operations"), + deleteDuration: newEngineMethodDurationCounter("delete"), + existsDuration: newEngineMethodDurationCounter("exists"), + getDuration: newEngineMethodDurationCounter("get"), + headDuration: newEngineMethodDurationCounter("head"), + inhumeDuration: newEngineMethodDurationCounter("inhume"), + putDuration: newEngineMethodDurationCounter("put"), + rangeDuration: newEngineMethodDurationCounter("range"), + searchDuration: newEngineMethodDurationCounter("search"), + listObjectsDuration: newEngineMethodDurationCounter("list_objects"), + containerSize: *newEngineGaugeVector("container_size", "Accumulated size of all objects in a container", []string{containerIDLabelKey}), + payloadSize: *newEngineGaugeVector("payload_size", "Accumulated size of all objects in a shard", []string{shardIDLabelKey}), } } +func newEngineCounter(name, help string) prometheus.Counter { + return prometheus.NewCounter(prometheus.CounterOpts{ + Namespace: namespace, + Subsystem: engineSubsystem, + Name: name, + Help: help, + }) +} + +func newEngineMethodDurationCounter(method string) prometheus.Counter { + return newEngineCounter( + fmt.Sprintf("%s_duration", method), + fmt.Sprintf("Accumulated duration of engine %s operations", strings.ReplaceAll(method, "_", " ")), + ) +} + +func newEngineGaugeVector(name, help string, labels []string) *prometheus.GaugeVec { + return prometheus.NewGaugeVec(prometheus.GaugeOpts{ + Namespace: namespace, + Subsystem: engineSubsystem, + Name: name, + Help: help, + }, labels) +} + func (m engineMetrics) register() { prometheus.MustRegister(m.listContainersDuration) prometheus.MustRegister(m.estimateContainerSizeDuration)