Skip to content

Commit 9264d44

Browse files
authored
cre-5198: cache metrics via metrics endpoint (#2212)
1 parent 8b7760f commit 9264d44

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

pkg/diskmonitor/diskmonitor.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ type int64Gauge interface {
1818
Record(ctx context.Context, value int64, options ...metric.RecordOption)
1919
}
2020

21+
type float64Gauge interface {
22+
Set(float64)
23+
}
24+
25+
// Option configures a [DiskMonitor].
26+
type Option func(*DiskMonitor)
27+
28+
// WithPrometheusGauge mirrors OTel gauge samples to a Prometheus gauge (e.g. node /metrics).
29+
func WithPrometheusGauge(g float64Gauge) Option {
30+
return func(dm *DiskMonitor) {
31+
dm.promGauge = g
32+
}
33+
}
34+
2135
// totalRegularFileSizeBytes sums on-disk file sizes for every non-directory under dirPath (recursive), using filepath.WalkDir.
2236
func totalRegularFileSizeBytes(dirPath string) (int64, error) {
2337
var totalSize int64
@@ -44,10 +58,11 @@ type DiskMonitor struct {
4458
lggr logger.Logger
4559
sizeOfDir func() (int64, error)
4660
gauge int64Gauge
61+
promGauge float64Gauge
4762
}
4863

4964
// NewDiskMonitor returns a [DiskMonitor] for dirPath using gaugeName at tickInterval.
50-
func NewDiskMonitor(lggr logger.Logger, dirPath string, gaugeName string, tickInterval time.Duration) (*DiskMonitor, error) {
65+
func NewDiskMonitor(lggr logger.Logger, dirPath string, gaugeName string, tickInterval time.Duration, opts ...Option) (*DiskMonitor, error) {
5166
g, err := beholder.GetMeter().Int64Gauge(gaugeName)
5267
if err != nil {
5368
return nil, fmt.Errorf("int64 gauge %q: %w", gaugeName, err)
@@ -70,6 +85,10 @@ func NewDiskMonitor(lggr logger.Logger, dirPath string, gaugeName string, tickIn
7085
"gaugeName", gaugeName,
7186
))
7287
dm.lggr = dm.eng.SugaredLogger
88+
89+
for _, opt := range opts {
90+
opt(dm)
91+
}
7392
return dm, nil
7493
}
7594

@@ -88,4 +107,7 @@ func (dm *DiskMonitor) emitDirSizeMetric(ctx context.Context) {
88107

89108
dm.lggr.Debugw("Emitting directory size metric", "sizeBytes", totalSize)
90109
dm.gauge.Record(ctx, totalSize)
110+
if dm.promGauge != nil {
111+
dm.promGauge.Set(float64(totalSize))
112+
}
91113
}

pkg/diskmonitor/diskmonitor_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,26 @@ func TestDiskMonitor_emitDirSizeMetric_error(t *testing.T) {
8484

8585
assert.Len(t, observed.FilterMessage("Failed to measure directory size").All(), 1)
8686
}
87+
88+
func TestDiskMonitor_emitDirSizeMetric_prometheusGauge(t *testing.T) {
89+
dm := &DiskMonitor{
90+
sizeOfDir: func() (int64, error) {
91+
return 99, nil
92+
},
93+
gauge: &mockGauge{},
94+
promGauge: &mockPromGauge{},
95+
lggr: logger.Test(t),
96+
}
97+
98+
dm.emitDirSizeMetric(t.Context())
99+
assert.Equal(t, int64(99), dm.gauge.(*mockGauge).gotValue)
100+
assert.Equal(t, float64(99), dm.promGauge.(*mockPromGauge).gotValue)
101+
}
102+
103+
type mockPromGauge struct {
104+
gotValue float64
105+
}
106+
107+
func (m *mockPromGauge) Set(v float64) {
108+
m.gotValue = v
109+
}

0 commit comments

Comments
 (0)