8000 [pull] main from miniflux:main by pull[bot] · Pull Request #262 · suhaoGe/miniflux · GitHub
[go: up one dir, main page]

Skip to content
8000
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ func startDaemon(store *storage.Storage) {
httpServers = server.StartWebServer(store, pool)
}

metricsCtx, cancelMetrics := context.WithCancel(context.Background())
if config.Opts.HasMetricsCollector() {
collector := metric.NewCollector(store, config.Opts.MetricsRefreshInterval())
go collector.GatherStorageMetrics()
go collector.GatherStorageMetrics(metricsCtx)
}

if systemd.HasNotifySocket() {
Expand Down Expand Up @@ -75,6 +76,7 @@ func startDaemon(store *storage.Storage) {

<-stop
slog.Debug("Shutting down the process")
cancelMetrics()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

Expand Down
51 changes: 41 additions & 10 deletions internal/metric/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package metric // import "miniflux.app/v2/internal/metric"

import (
"context"
"log/slog"
"time"

Expand All @@ -12,6 +13,12 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

// Status label values for histogram metrics.
const (
StatusSuccess = "success"
StatusError = "error"
)

// Prometheus Metrics.
var (
BackgroundFeedRefreshDuration = prometheus.NewHistogramVec(
Expand Down 8000 Expand Up @@ -162,21 +169,45 @@ func NewCollector(store *storage.Storage, refreshInterval time.Duration) *collec
}

// GatherStorageMetrics polls the database to fetch metrics.
func (c *collector) GatherStorageMetrics() {
for range time.Tick(c.refreshInterval) {
func (c *collector) GatherStorageMetrics(ctx context.Context) {
ticker := time.NewTicker(c.refreshInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
slog.Debug("Stopping metric collector")
return
case <-ticker.C:
}
slog.Debug("Collecting metrics from the database")

usersGauge.Set(float64(c.store.CountUsers()))
brokenFeedsGauge.Set(float64(c.store.CountAllFeedsWithErrors()))
if usersCount, err := c.store.CountUsers(); err != nil {
slog.Warn("Unable to collect users metric", slog.Any("error", err))
} else {
usersGauge.Set(float64(usersCount))
}

if brokenFeedsCount, err := c.store.CountAllFeedsWithErrors(); err != nil {
slog.Warn("Unable to collect broken feeds metric", slog.Any("error", err))
} else {
brokenFeedsGauge.Set(float64(brokenFeedsCount))
}

feedsCount := c.store.CountAllFeeds()
for status, count := range feedsCount {
feedsGauge.WithLabelValues(status).Set(float64(count))
if feedsCount, err := c.store.CountAllFeeds(); err != nil {
slog.Warn("Unable to collect feeds metric", slog.Any("error", err))
} else {
for status, count := range feedsCount {
feedsGauge.WithLabelValues(status).Set(float64(count))
}
}

entriesCount := c.store.CountAllEntries()
for status, count := range entriesCount {
entriesGauge.WithLabelValues(status).Set(float64(count))
if entriesCount, err := c.store.CountAllEntries(); err != nil {
slog.Warn("Unable to collect entries metric", slog.Any("error", err))
} else {
for status, count := range entriesCount {
entriesGauge.WithLabelValues(status).Set(float64(count))
}
}

dbStats := c.store.DBStats()
Expand Down
8 changes: 4 additions & 4 deletions internal/reader/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ func ProcessFeedEntries(store *storage.Storage, feed *model.Feed, userID int64,
}

if config.Opts.HasMetricsCollector() {
status := "success"
status := metric.StatusSuccess
if scraperErr != nil {
status = "error"
status = metric.StatusError
}
metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
}
Expand Down Expand Up @@ -199,9 +199,9 @@ func ProcessEntryWebPage(feed *model.Feed, entry *model.Entry, user *model.User)
)

if config.Opts.HasMetricsCollector() {
status := "success"
status := metric.StatusSuccess
if scraperErr != nil {
status = "error"
status = metric.StatusError
}
metric.ScraperRequestDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
}
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (
)

// CountAllEntries returns the number of entries for each status in the database.
func (s *Storage) CountAllEntries() map[string]int64 {
func (s *Storage) CountAllEntries() (map[string]int64, error) {
rows, err := s.db.Query(`SELECT status, count(*) FROM entries GROUP BY status`)
if err != nil {
return nil
return nil, fmt.Errorf("storage: unable to count entries: %w", err)
}
defer rows.Close()

Expand All @@ -41,7 +41,7 @@ func (s *Storage) CountAllEntries() map[string]int64 {
}

results["total"] = results[model.EntryStatusUnread] + results[model.EntryStatusRead] + results[model.EntryStatusRemoved]
return results
return results, nil
}

// CountUnreadEntries returns the number of unread entries.
Expand Down
12 changes: 6 additions & 6 deletions internal/storage/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func (s *Storage) AnotherFeedURLExists(userID, feedID int64, feedURL string) boo
}

// CountAllFeeds returns the number of feeds in the database.
func (s *Storage) CountAllFeeds() map[string]int64 {
func (s *Storage) CountAllFeeds() (map[string]int64, error) {
rows, err := s.db.Query(`SELECT disabled, count(*) FROM feeds GROUP BY disabled`)
if err != nil {
return nil
return nil, fmt.Errorf("storage: unable to count feeds: %w", err)
}
defer rows.Close()

Expand All @@ -106,7 +106,7 @@ func (s *Storage) CountAllFeeds() map[string]int64 {
}

results["total"] = results["disabled"] + results["enabled"]
return results
return results, nil
}

// CountUserFeedsWithErrors returns the number of feeds with parsing errors that belong to the given user.
Expand All @@ -126,7 +126,7 @@ func (s *Storage) CountUserFeedsWithErrors(userID int64) int {
}

// CountAllFeedsWithErrors returns the number of feeds with parsing errors.
func (s *Storage) CountAllFeedsWithErrors() int {
func (s *Storage) CountAllFeedsWithErrors() (int, error) {
pollingParsingErrorLimit := config.Opts.PollingParsingErrorLimit()
if pollingParsingErrorLimit <= 0 {
pollingParsingErrorLimit = 1
Expand All @@ -135,10 +135,10 @@ func (s *Storage) CountAllFeedsWithErrors() int {
var result int
err := s.db.QueryRow(query, pollingParsingErrorLimit).Scan(&result)
if err != nil {
return 0
return 0, fmt.Errorf("storage: unable to count feeds with errors: %w", err)
}

return result
return result, nil
}

// Feeds returns all feeds that belongs to the given user.
Expand Down
6 changes: 3 additions & 3 deletions internal/storage/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import (
)

// CountUsers returns the total number of users.
func (s *Storage) CountUsers() int {
func (s *Storage) CountUsers() (int, error) {
var result int
err := s.db.QueryRow(`SELECT count(*) FROM users`).Scan(&result)
if err != nil {
return 0
return 0, fmt.Errorf("storage: unable to count users: %w", err)
}

return result
return result, nil
}

// SetLastLogin updates the last login date of a user.
Expand Down
4 changes: 2 additions & 2 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func (w *worker) Run(c <-chan model.Job) {
localizedError := feedHandler.RefreshFeed(w.store, job.UserID, job.FeedID, false)

if config.Opts.HasMetricsCollector() {
status := "success"
status := metric.StatusSuccess
if localizedError != nil {
status = "error"
status = metric.StatusError
}
metric.BackgroundFeedRefreshDuration.WithLabelValues(status).Observe(time.Since(startTime).Seconds())
}
Expand Down
Loading
0