From 8c9850c2bb426eea286f0ec79b3340f823adf24e Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Thu, 15 Oct 2020 23:53:59 +0200 Subject: [PATCH] Remote: Do not collect non-initialized timestamp metrics (#8060) * Remote: Do not collect non-initialized timestamp metrics Signed-off-by: Julien Pivotto --- storage/remote/{max_gauge.go => max_timestamp.go} | 12 +++++++++--- storage/remote/queue_manager.go | 8 ++++---- storage/remote/queue_manager_test.go | 4 ++-- storage/remote/write.go | 6 +++--- 4 files changed, 18 insertions(+), 12 deletions(-) rename storage/remote/{max_gauge.go => max_timestamp.go} (80%) diff --git a/storage/remote/max_gauge.go b/storage/remote/max_timestamp.go similarity index 80% rename from storage/remote/max_gauge.go rename to storage/remote/max_timestamp.go index a56c2047d..3a0a6d6fd 100644 --- a/storage/remote/max_gauge.go +++ b/storage/remote/max_timestamp.go @@ -19,13 +19,13 @@ import ( "github.com/prometheus/client_golang/prometheus" ) -type maxGauge struct { +type maxTimestamp struct { mtx sync.Mutex value float64 prometheus.Gauge } -func (m *maxGauge) Set(value float64) { +func (m *maxTimestamp) Set(value float64) { m.mtx.Lock() defer m.mtx.Unlock() if value > m.value { @@ -34,8 +34,14 @@ func (m *maxGauge) Set(value float64) { } } -func (m *maxGauge) Get() float64 { +func (m *maxTimestamp) Get() float64 { m.mtx.Lock() defer m.mtx.Unlock() return m.value } + +func (m *maxTimestamp) Collect(c chan<- prometheus.Metric) { + if m.Get() > 0 { + m.Gauge.Collect(c) + } +} diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index 1a79d6c0c..47b5726bd 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -56,7 +56,7 @@ type queueManagerMetrics struct { droppedSamplesTotal prometheus.Counter enqueueRetriesTotal prometheus.Counter sentBatchDuration prometheus.Histogram - highestSentTimestamp *maxGauge + highestSentTimestamp *maxTimestamp pendingSamples prometheus.Gauge shardCapacity prometheus.Gauge numShards prometheus.Gauge @@ -118,7 +118,7 @@ func newQueueManagerMetrics(r prometheus.Registerer, rn, e string) *queueManager Buckets: append(prometheus.DefBuckets, 25, 60, 120, 300), ConstLabels: constLabels, }) - m.highestSentTimestamp = &maxGauge{ + m.highestSentTimestamp = &maxTimestamp{ Gauge: prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: namespace, Subsystem: subsystem, @@ -262,7 +262,7 @@ type QueueManager struct { metrics *queueManagerMetrics interner *pool - highestRecvTimestamp *maxGauge + highestRecvTimestamp *maxTimestamp } // NewQueueManager builds a new QueueManager. @@ -279,7 +279,7 @@ func NewQueueManager( client WriteClient, flushDeadline time.Duration, interner *pool, - highestRecvTimestamp *maxGauge, + highestRecvTimestamp *maxTimestamp, ) *QueueManager { if logger == nil { logger = log.NewNopLogger() diff --git a/storage/remote/queue_manager_test.go b/storage/remote/queue_manager_test.go index d8927d9e4..d8236f8de 100644 --- a/storage/remote/queue_manager_test.go +++ b/storage/remote/queue_manager_test.go @@ -47,8 +47,8 @@ import ( const defaultFlushDeadline = 1 * time.Minute -func newHighestTimestampMetric() *maxGauge { - return &maxGauge{ +func newHighestTimestampMetric() *maxTimestamp { + return &maxTimestamp{ Gauge: prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: namespace, Subsystem: subsystem, diff --git a/storage/remote/write.go b/storage/remote/write.go index 8b0bf7622..9e93e0c18 100644 --- a/storage/remote/write.go +++ b/storage/remote/write.go @@ -53,7 +53,7 @@ type WriteStorage struct { interner *pool // For timestampTracker. - highestTimestamp *maxGauge + highestTimestamp *maxTimestamp } // NewWriteStorage creates and runs a WriteStorage. @@ -71,7 +71,7 @@ func NewWriteStorage(logger log.Logger, reg prometheus.Registerer, walDir string samplesIn: newEWMARate(ewmaWeight, shardUpdateDuration), walDir: walDir, interner: newPool(), - highestTimestamp: &maxGauge{ + highestTimestamp: &maxTimestamp{ Gauge: prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: namespace, Subsystem: subsystem, @@ -202,7 +202,7 @@ type timestampTracker struct { writeStorage *WriteStorage samples int64 highestTimestamp int64 - highestRecvTimestamp *maxGauge + highestRecvTimestamp *maxTimestamp } // Add implements storage.Appender. -- GitLab