From 1f08d4f02f731214abb5b02fe530e92d50b75c42 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 19 Sep 2023 16:44:54 +0200 Subject: [PATCH 1/2] Add method to reset key of record --- database/record/base.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/database/record/base.go b/database/record/base.go index bb24bcb..ef157f4 100644 --- a/database/record/base.go +++ b/database/record/base.go @@ -44,6 +44,13 @@ func (b *Base) SetKey(key string) { } } +// ResetKey resets the database name and key. +// Use with caution! +func (b *Base) ResetKey() { + b.dbName = "" + b.dbKey = "" +} + // Key returns the key of the database record. // As the key must be set before any usage and can only be set once, this // function may be used without locking the record. From 3d8c3de6a2c9bb46bbd5a7aa5f6a0591ceacf534 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 19 Sep 2023 16:55:51 +0200 Subject: [PATCH 2/2] Wait for metrics pusher before persisting metrics --- metrics/api.go | 5 +++++ metrics/module.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/metrics/api.go b/metrics/api.go index 4597882..638107d 100644 --- a/metrics/api.go +++ b/metrics/api.go @@ -11,6 +11,7 @@ import ( "github.com/safing/portbase/api" "github.com/safing/portbase/config" "github.com/safing/portbase/log" + "github.com/safing/portbase/utils" ) func registerAPI() error { @@ -139,7 +140,11 @@ 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() diff --git a/metrics/module.go b/metrics/module.go index 095dee8..4dbd9b2 100644 --- a/metrics/module.go +++ b/metrics/module.go @@ -5,6 +5,7 @@ import ( "fmt" "sort" "sync" + "time" "github.com/safing/portbase/modules" ) @@ -78,6 +79,20 @@ func start() error { } 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 + // 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): + } + } + storePersistentMetrics() return nil