diff --git a/util/metrics/gauge.go b/util/metrics/gauge.go index 025d84bd32..c1497489e4 100644 --- a/util/metrics/gauge.go +++ b/util/metrics/gauge.go @@ -20,9 +20,25 @@ import ( "math" "strconv" "strings" - "time" + + "github.com/algorand/go-deadlock" ) +// Gauge represent a single gauge variable. +type Gauge struct { + deadlock.Mutex + name string + description string + labels map[string]int // map each label ( i.e. httpErrorCode ) to an index. + valuesIndices map[int]*gaugeValues // maps each set of labels into a concrete gauge +} + +type gaugeValues struct { + gauge float64 + labels map[string]string + formattedLabels string +} + // MakeGauge create a new gauge with the provided name and description. func MakeGauge(metric MetricName) *Gauge { c := &Gauge{ @@ -35,15 +51,6 @@ func MakeGauge(metric MetricName) *Gauge { return c } -// TouchAll updates all the timestamps associated with this metric. -func (gauge *Gauge) TouchAll() { - gauge.Lock() - defer gauge.Unlock() - for _, val := range gauge.valuesIndices { - val.timestamp = time.Now() - } -} - // Register registers the gauge with the default/specific registry func (gauge *Gauge) Register(reg *Registry) { if reg == nil { @@ -73,16 +80,14 @@ func (gauge *Gauge) Add(x float64, labels map[string]string) { if gaugeObj, has := gauge.valuesIndices[labelIndex]; !has { // we need to add a new gauge. val := &gaugeValues{ - gauge: x, - labels: labels, - timestamp: time.Now(), + gauge: x, + labels: labels, } val.createFormattedLabel() gauge.valuesIndices[labelIndex] = val } else { // update existing value. gaugeObj.gauge += x - gaugeObj.timestamp = time.Now() } } @@ -97,16 +102,14 @@ func (gauge *Gauge) Set(x float64, labels map[string]string) { if gaugeObj, has := gauge.valuesIndices[labelIndex]; !has { // we need to add a new gauge. val := &gaugeValues{ - gauge: x, - labels: labels, - timestamp: time.Now(), + gauge: x, + labels: labels, } val.createFormattedLabel() gauge.valuesIndices[labelIndex] = val } else { // update existing value. gaugeObj.gauge = x - gaugeObj.timestamp = time.Now() } } @@ -139,26 +142,11 @@ func (cv *gaugeValues) createFormattedLabel() { cv.formattedLabels = buf.String()[1:] } -// filterExpiredMetrics scans the gauge.valuesIndices map and removing all the gauges that -// haven't been updated in the past units of time. -func (gauge *Gauge) filterExpiredMetrics() { - // find out what the cut off date is - metricRetensionThreshold := time.Now().Add(-maxMetricRetensionDuration) - - for gaugeKey, gaugeObj := range gauge.valuesIndices { - if gaugeObj.timestamp.Before(metricRetensionThreshold) { - delete(gauge.valuesIndices, gaugeKey) - } - } -} - // WriteMetric writes the metric into the output stream func (gauge *Gauge) WriteMetric(buf *strings.Builder, parentLabels string) { gauge.Lock() defer gauge.Unlock() - gauge.filterExpiredMetrics() - if len(gauge.valuesIndices) < 1 { return } @@ -190,8 +178,6 @@ func (gauge *Gauge) AddMetric(values map[string]string) { gauge.Lock() defer gauge.Unlock() - gauge.filterExpiredMetrics() - if len(gauge.valuesIndices) < 1 { return } diff --git a/util/metrics/gaugeCommon.go b/util/metrics/gaugeCommon.go deleted file mode 100644 index 49b617cb8f..0000000000 --- a/util/metrics/gaugeCommon.go +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright (C) 2019-2021 Algorand, Inc. -// This file is part of go-algorand -// -// go-algorand is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as -// published by the Free Software Foundation, either version 3 of the -// License, or (at your option) any later version. -// -// go-algorand is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with go-algorand. If not, see . - -package metrics - -import ( - "time" - - "github.com/algorand/go-deadlock" -) - -// Gauge represent a single gauge variable. -type Gauge struct { - deadlock.Mutex - name string - description string - labels map[string]int // map each label ( i.e. httpErrorCode ) to an index. - valuesIndices map[int]*gaugeValues // maps each set of labels into a concrete gauge -} - -type gaugeValues struct { - gauge float64 - timestamp time.Time - labels map[string]string - formattedLabels string -}