-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
app/promauto: implement reset gauge (#2289)
Implement a ResetGauge to simplify "resetting of previously set labels". category: misc ticket: #2277
- Loading branch information
1 parent
97f8a02
commit a57c120
Showing
8 changed files
with
140 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
||
package promauto | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
const separator = "|" | ||
|
||
// NewResetGaugeVec creates a new ResetGaugeVec. | ||
func NewResetGaugeVec(opts prometheus.GaugeOpts, labelNames []string) *ResetGaugeVec { | ||
return &ResetGaugeVec{ | ||
inner: NewGaugeVec(opts, labelNames), | ||
labels: make(map[string]bool), | ||
} | ||
} | ||
|
||
// ResetGaugeVec is a GaugeVec that can be reset which deletes all previously set labels. | ||
// This is useful to clear out labels that are no longer present. | ||
type ResetGaugeVec struct { | ||
inner *prometheus.GaugeVec | ||
|
||
mu sync.Mutex | ||
labels map[string]bool | ||
} | ||
|
||
func (g *ResetGaugeVec) WithLabelValues(lvs ...string) prometheus.Gauge { | ||
for _, lv := range lvs { | ||
if strings.Contains(lv, separator) { | ||
panic("label value cannot contain separator") | ||
} | ||
} | ||
|
||
g.mu.Lock() | ||
defer g.mu.Unlock() | ||
|
||
g.labels[strings.Join(lvs, separator)] = true | ||
|
||
return g.inner.WithLabelValues(lvs...) | ||
} | ||
|
||
// Reset deletes all previously set labels. | ||
func (g *ResetGaugeVec) Reset() { | ||
g.mu.Lock() | ||
defer g.mu.Unlock() | ||
|
||
for lv := range g.labels { | ||
g.inner.DeleteLabelValues(strings.Split(lv, separator)...) | ||
} | ||
|
||
g.labels = make(map[string]bool) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1 | ||
|
||
package promauto_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/obolnetwork/charon/app/promauto" | ||
) | ||
|
||
const resetTest = "reset_test" | ||
|
||
var testResetGauge = promauto.NewResetGaugeVec(prometheus.GaugeOpts{ | ||
Name: resetTest, | ||
Help: "", | ||
}, []string{"label"}) | ||
|
||
func TestResetGaugeVec(t *testing.T) { | ||
registry, err := promauto.NewRegistry(nil) | ||
require.NoError(t, err) | ||
|
||
testResetGauge.WithLabelValues("1").Set(1) | ||
assertVecLen(t, registry, resetTest, 1) | ||
|
||
testResetGauge.WithLabelValues("2").Set(2) | ||
assertVecLen(t, registry, resetTest, 2) | ||
|
||
testResetGauge.Reset() | ||
assertVecLen(t, registry, resetTest, 0) | ||
|
||
testResetGauge.WithLabelValues("3").Set(3) | ||
assertVecLen(t, registry, resetTest, 1) | ||
} | ||
|
||
func assertVecLen(t *testing.T, registry *prometheus.Registry, name string, l int) { //nolint:unparam // abstracting name is fine even though it is always currently constant | ||
t.Helper() | ||
|
||
metrics, err := registry.Gather() | ||
require.NoError(t, err) | ||
|
||
for _, metricFam := range metrics { | ||
if *metricFam.Name != name { | ||
continue | ||
} | ||
|
||
require.Len(t, metricFam.Metric, l) | ||
|
||
return | ||
} | ||
|
||
if l == 0 { | ||
return | ||
} | ||
|
||
require.Fail(t, "metric not found") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters