1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-26 18:21:01 +00:00

refactor(metric): use time.Duration for refresh duration

This commit is contained in:
gudvinr 2025-08-18 23:10:18 +03:00 committed by Frédéric Guillot
parent c6536e8d90
commit 4af12a4129
4 changed files with 64 additions and 9 deletions

View file

@ -2230,3 +2230,58 @@ func TestCustomPollingLimitPerHost(t *testing.T) {
t.Fatalf(`Unexpected custom PollingLimitPerHost value, got %v instead of %v`, result, expected) t.Fatalf(`Unexpected custom PollingLimitPerHost value, got %v instead of %v`, result, expected)
} }
} }
func TestMetricsRefreshInterval(t *testing.T) {
os.Clearenv()
os.Setenv("METRICS_REFRESH_INTERVAL", "33")
parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
expected := 33 * time.Second
result := opts.MetricsRefreshInterval()
if result != expected {
t.Fatalf(`Unexpected METRICS_REFRESH_INTERVAL value, got %d instead of %d`, result, expected)
}
sorted := opts.SortedOptions(false)
i := slices.IndexFunc(sorted, func(opt *option) bool {
return opt.Key == "METRICS_REFRESH_INTERVAL"
})
expectedSerialized := 33
if got := sorted[i].Value; got != expectedSerialized {
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
}
}
func TestDefaultMetricsRefreshInterval(t *testing.T) {
os.Clearenv()
parser := NewParser()
opts, err := parser.ParseEnvironmentVariables()
if err != nil {
t.Fatalf(`Parsing failure: %v`, err)
}
expected := defaultMetricsRefreshInterval
result := opts.MetricsRefreshInterval()
if result != expected {
t.Fatalf(`Unexpected METRICS_REFRESH_INTERVAL value, got %d instead of %d`, result, expected)
}
sorted := opts.SortedOptions(false)
i := slices.IndexFunc(sorted, func(opt *option) bool {
return opt.Key == "METRICS_REFRESH_INTERVAL"
})
expectedSerialized := int(defaultMetricsRefreshInterval / time.Second)
if got := sorted[i].Value; got != expectedSerialized {
t.Fatalf(`Unexpected value in option output, got %q instead of %q`, got, expectedSerialized)
}
}

View file

@ -83,7 +83,7 @@ const (
defaultMaintenanceMode = false defaultMaintenanceMode = false
defaultMaintenanceMessage = "Miniflux is currently under maintenance" defaultMaintenanceMessage = "Miniflux is currently under maintenance"
defaultMetricsCollector = false defaultMetricsCollector = false
defaultMetricsRefreshInterval = 60 defaultMetricsRefreshInterval = 60 * time.Second
defaultMetricsAllowedNetworks = "127.0.0.1/8" defaultMetricsAllowedNetworks = "127.0.0.1/8"
defaultMetricsUsername = "" defaultMetricsUsername = ""
defaultMetricsPassword = "" defaultMetricsPassword = ""
@ -176,7 +176,7 @@ type options struct {
maintenanceMode bool maintenanceMode bool
maintenanceMessage string maintenanceMessage string
metricsCollector bool metricsCollector bool
metricsRefreshInterval int metricsRefreshInterval time.Duration
metricsAllowedNetworks []string metricsAllowedNetworks []string
metricsUsername string metricsUsername string
metricsPassword string metricsPassword string
@ -639,8 +639,8 @@ func (o *options) HasMetricsCollector() bool {
return o.metricsCollector return o.metricsCollector
} }
// MetricsRefreshInterval returns the refresh interval in seconds. // MetricsRefreshInterval returns the refresh interval.
func (o *options) MetricsRefreshInterval() int { func (o *options) MetricsRefreshInterval() time.Duration {
return o.metricsRefreshInterval return o.metricsRefreshInterval
} }
@ -759,7 +759,7 @@ func (o *options) SortedOptions(redactSecret bool) []*option {
"METRICS_ALLOWED_NETWORKS": strings.Join(o.metricsAllowedNetworks, ","), "METRICS_ALLOWED_NETWORKS": strings.Join(o.metricsAllowedNetworks, ","),
"METRICS_COLLECTOR": o.metricsCollector, "METRICS_COLLECTOR": o.metricsCollector,
"METRICS_PASSWORD": redactSecretValue(o.metricsPassword, redactSecret), "METRICS_PASSWORD": redactSecretValue(o.metricsPassword, redactSecret),
"METRICS_REFRESH_INTERVAL": o.metricsRefreshInterval, "METRICS_REFRESH_INTERVAL": int(o.metricsRefreshInterval.Seconds()),
"METRICS_USERNAME": o.metricsUsername, "METRICS_USERNAME": o.metricsUsername,
"OAUTH2_CLIENT_ID": o.oauth2ClientID, "OAUTH2_CLIENT_ID": o.oauth2ClientID,
"OAUTH2_CLIENT_SECRET": redactSecretValue(o.oauth2ClientSecret, redactSecret), "OAUTH2_CLIENT_SECRET": redactSecretValue(o.oauth2ClientSecret, redactSecret),

View file

@ -231,7 +231,7 @@ func (p *parser) parseLines(lines []string) (err error) {
case "METRICS_COLLECTOR": case "METRICS_COLLECTOR":
p.opts.metricsCollector = parseBool(value, defaultMetricsCollector) p.opts.metricsCollector = parseBool(value, defaultMetricsCollector)
case "METRICS_REFRESH_INTERVAL": case "METRICS_REFRESH_INTERVAL":
p.opts.metricsRefreshInterval = parseInt(value, defaultMetricsRefreshInterval) p.opts.metricsRefreshInterval = parseInterval(value, time.Second, defaultMetricsRefreshInterval)
case "METRICS_ALLOWED_NETWORKS": case "METRICS_ALLOWED_NETWORKS":
p.opts.metricsAllowedNetworks = parseStringList(value, []string{defaultMetricsAllowedNetworks}) p.opts.metricsAllowedNetworks = parseStringList(value, []string{defaultMetricsAllowedNetworks})
case "METRICS_USERNAME": case "METRICS_USERNAME":

View file

@ -138,11 +138,11 @@ var (
// collector represents a metric collector. // collector represents a metric collector.
type collector struct { type collector struct {
store *storage.Storage store *storage.Storage
refreshInterval int refreshInterval time.Duration
} }
// NewCollector initializes a new metric collector. // NewCollector initializes a new metric collector.
func NewCollector(store *storage.Storage, refreshInterval int) *collector { func NewCollector(store *storage.Storage, refreshInterval time.Duration) *collector {
prometheus.MustRegister(BackgroundFeedRefreshDuration) prometheus.MustRegister(BackgroundFeedRefreshDuration)
prometheus.MustRegister(ScraperRequestDuration) prometheus.MustRegister(ScraperRequestDuration)
prometheus.MustRegister(ArchiveEntriesDuration) prometheus.MustRegister(ArchiveEntriesDuration)
@ -163,7 +163,7 @@ func NewCollector(store *storage.Storage, refreshInterval int) *collector {
// GatherStorageMetrics polls the database to fetch metrics. // GatherStorageMetrics polls the database to fetch metrics.
func (c *collector) GatherStorageMetrics() { func (c *collector) GatherStorageMetrics() {
for range time.Tick(time.Duration(c.refreshInterval) * time.Second) { for range time.Tick(c.refreshInterval) {
slog.Debug("Collecting metrics from the database") slog.Debug("Collecting metrics from the database")
usersGauge.Set(float64(c.store.CountUsers())) usersGauge.Set(float64(c.store.CountUsers()))