Skip to content

Commit 4af12a4

Browse files
gudvinrfguillot
authored andcommitted
refactor(metric): use time.Duration for refresh duration
1 parent c6536e8 commit 4af12a4

File tree

4 files changed

+64
-9
lines changed

4 files changed

+64
-9
lines changed

internal/config/config_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,3 +2230,58 @@ func TestCustomPollingLimitPerHost(t *testing.T) {
22302230
t.Fatalf(`Unexpected custom PollingLimitPerHost value, got %v instead of %v`, result, expected)
22312231
}
22322232
}
2233+
2234+
func TestMetricsRefreshInterval(t *testing.T) {
2235+
os.Clearenv()
2236+
os.Setenv("METRICS_REFRESH_INTERVAL", "33")
2237+
2238+
parser := NewParser()
2239+
opts, err := parser.ParseEnvironmentVariables()
2240+
if err != nil {
2241+
t.Fatalf(`Parsing failure: %v`, err)
2242+
}
2243+
2244+
expected := 33 * time.Second
2245+
result := opts.MetricsRefreshInterval()
2246+
2247+
if result != expected {
2248+
t.Fatalf(`Unexpected METRICS_REFRESH_INTERVAL value, got %d instead of %d`, result, expected)
2249+
}
2250+
2251+
sorted := opts.SortedOptions(false)
2252+
i := slices.IndexFunc(sorted, func(opt *option) bool {
2253+
return opt.Key == "METRICS_REFRESH_INTERVAL"
2254+
})
2255+
2256+
expectedSerialized := 33
2257+
if got := sorted[i].Value; got != expectedSerialized {
2258+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
2259+
}
2260+
}
2261+
2262+
func TestDefaultMetricsRefreshInterval(t *testing.T) {
2263+
os.Clearenv()
2264+
2265+
parser := NewParser()
2266+
opts, err := parser.ParseEnvironmentVariables()
2267+
if err != nil {
2268+
t.Fatalf(`Parsing failure: %v`, err)
2269+
}
2270+
2271+
expected := defaultMetricsRefreshInterval
2272+
result := opts.MetricsRefreshInterval()
2273+
2274+
if result != expected {
2275+
t.Fatalf(`Unexpected METRICS_REFRESH_INTERVAL value, got %d instead of %d`, result, expected)
2276+
}
2277+
2278+
sorted := opts.SortedOptions(false)
2279+
i := slices.IndexFunc(sorted, func(opt *option) bool {
2280+
return opt.Key == "METRICS_REFRESH_INTERVAL"
2281+
})
2282+
2283+
expectedSerialized := int(defaultMetricsRefreshInterval / time.Second)
2284+
if got := sorted[i].Value; got != expectedSerialized {
2285+
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
2286+
}
2287+
}

internal/config/options.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const (
8383
defaultMaintenanceMode = false
8484
defaultMaintenanceMessage = "Miniflux is currently under maintenance"
8585
defaultMetricsCollector = false
86-
defaultMetricsRefreshInterval = 60
86+
defaultMetricsRefreshInterval = 60 * time.Second
8787
defaultMetricsAllowedNetworks = "127.0.0.1/8"
8888
defaultMetricsUsername = ""
8989
defaultMetricsPassword = ""
@@ -176,7 +176,7 @@ type options struct {
176176
maintenanceMode bool
177177
maintenanceMessage string
178178
metricsCollector bool
179-
metricsRefreshInterval int
179+
metricsRefreshInterval time.Duration
180180
metricsAllowedNetworks []string
181181
metricsUsername string
182182
metricsPassword string
@@ -639,8 +639,8 @@ func (o *options) HasMetricsCollector() bool {
639639
return o.metricsCollector
640640
}
641641

642-
// MetricsRefreshInterval returns the refresh interval in seconds.
643-
func (o *options) MetricsRefreshInterval() int {
642+
// MetricsRefreshInterval returns the refresh interval.
643+
func (o *options) MetricsRefreshInterval() time.Duration {
644644
return o.metricsRefreshInterval
645645
}
646646

@@ -759,7 +759,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
759759
"METRICS_ALLOWED_NETWORKS": strings.Join(o.metricsAllowedNetworks, ","),
760760
"METRICS_COLLECTOR": o.metricsCollector,
761761
"METRICS_PASSWORD": redactSecretValue(o.metricsPassword, redactSecret),
762-
"METRICS_REFRESH_INTERVAL": o.metricsRefreshInterval,
762+
"METRICS_REFRESH_INTERVAL": int(o.metricsRefreshInterval.Seconds()),
763763
"METRICS_USERNAME": o.metricsUsername,
764764
"OAUTH2_CLIENT_ID": o.oauth2ClientID,
765765
"OAUTH2_CLIENT_SECRET": redactSecretValue(o.oauth2ClientSecret, redactSecret),

internal/config/parser.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (p *parser) parseLines(lines []string) (err error) {
231231
case "METRICS_COLLECTOR":
232232
p.opts.metricsCollector = parseBool(value, defaultMetricsCollector)
233233
case "METRICS_REFRESH_INTERVAL":
234-
p.opts.metricsRefreshInterval = parseInt(value, defaultMetricsRefreshInterval)
234+
p.opts.metricsRefreshInterval = parseInterval(value, time.Second, defaultMetricsRefreshInterval)
235235
case "METRICS_ALLOWED_NETWORKS":
236236
p.opts.metricsAllowedNetworks = parseStringList(value, []string{defaultMetricsAllowedNetworks})
237237
case "METRICS_USERNAME":

internal/metric/metric.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ var (
138138
// collector represents a metric collector.
139139
type collector struct {
140140
store *storage.Storage
141-
refreshInterval int
141+
refreshInterval time.Duration
142142
}
143143

144144
// NewCollector initializes a new metric collector.
145-
func NewCollector(store *storage.Storage, refreshInterval int) *collector {
145+
func NewCollector(store *storage.Storage, refreshInterval time.Duration) *collector {
146146
prometheus.MustRegister(BackgroundFeedRefreshDuration)
147147
prometheus.MustRegister(ScraperRequestDuration)
148148
prometheus.MustRegister(ArchiveEntriesDuration)
@@ -163,7 +163,7 @@ func NewCollector(store *storage.Storage, refreshInterval int) *collector {
163163

164164
// GatherStorageMetrics polls the database to fetch metrics.
165165
func (c *collector) GatherStorageMetrics() {
166-
for range time.Tick(time.Duration(c.refreshInterval) * time.Second) {
166+
for range time.Tick(c.refreshInterval) {
167167
slog.Debug("Collecting metrics from the database")
168168

169169
usersGauge.Set(float64(c.store.CountUsers()))

0 commit comments

Comments
 (0)