Skip to content

Commit

Permalink
Fix waiting for log writers on shutdown, improve persistence enabling
Browse files Browse the repository at this point in the history
  • Loading branch information
dhaavi committed Oct 12, 2023
1 parent 7872911 commit 05bdc44
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 21 deletions.
5 changes: 0 additions & 5 deletions metrics/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/safing/portbase/api"
"github.com/safing/portbase/config"
"github.com/safing/portbase/log"
"github.com/safing/portbase/utils"
)

func registerAPI() error {
Expand Down Expand Up @@ -140,11 +139,7 @@ func writeMetricsTo(ctx context.Context, url string) error {
)
}

var metricsPusherDone = utils.NewBroadcastFlag()

func metricsWriter(ctx context.Context) error {
defer metricsPusherDone.NotifyAndReset()

pushURL := pushOption()
ticker := module.NewSleepyTicker(1*time.Minute, 0)
defer ticker.Stop()
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (

const hostStatTTL = 1 * time.Second

func registeHostMetrics() (err error) {
func registerHostMetrics() (err error) {
// Register load average metrics.
_, err = NewGauge("host/load/avg/1", nil, getFloat64HostStat(LoadAvg1), &Options{Name: "Host Load Avg 1min", Permission: api.PermitUser})
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion metrics/metrics_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/safing/portbase/log"
)

func registeLogMetrics() (err error) {
func registerLogMetrics() (err error) {
_, err = NewFetchingCounter(
"logs/warning/total",
nil,
Expand Down
27 changes: 14 additions & 13 deletions metrics/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"sort"
"sync"
"time"

"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ var (
)

func init() {
module = modules.Register("metrics", prep, start, stop, "config", "database", "api")
module = modules.Register("metrics", prep, start, stop, "config", "database", "api", "base")
}

func prep() error {
Expand All @@ -59,18 +59,22 @@ func start() error {
return err
}

if err := registeHostMetrics(); err != nil {
if err := registerHostMetrics(); err != nil {
return err
}

if err := registeLogMetrics(); err != nil {
if err := registerLogMetrics(); err != nil {
return err
}

if err := registerAPI(); err != nil {
return err
}

if err := loadPersistentMetrics(); err != nil {
log.Errorf("metrics: failed to load persistent metrics: %s", err)
}

if pushOption() != "" {
module.StartServiceWorker("metric pusher", 0, metricsWriter)
}
Expand All @@ -82,16 +86,13 @@ func stop() error {
// Wait until the metrics pusher is done, as it may have started reporting
// and may report a higher number than we store to disk. For persistent
// metrics it can then happen that the first report is lower than the
// previous report, making prometheus think that al that happened since the
// previous report, making prometheus think that all that happened since the
// last report, due to the automatic restart detection.
done := metricsPusherDone.NewFlag()
done.Refresh()
if !done.IsSet() {
select {
case <-done.Signal():
case <-time.After(10 * time.Second):
}
}

// The registry is read locked when writing metrics.
// Write lock the registry to make sure all writes are finished.
registryLock.Lock()
registryLock.Unlock() //nolint:staticcheck

storePersistentMetrics()

Expand Down
10 changes: 9 additions & 1 deletion metrics/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@ func EnableMetricPersistence(key string) error {

// Set storage key.
storageKey = key
return nil
}

func loadPersistentMetrics() error {
// Abort if storage is not enabled.
if storageInit.SetToIf(false, true) {
return nil
}

// Load metrics from storage.
var err error
storage, err = getMetricsStorage(key)
storage, err = getMetricsStorage(storageKey)
switch {
case err == nil:
// Continue.
Expand Down

0 comments on commit 05bdc44

Please sign in to comment.