Skip to content

Commit

Permalink
Fix stale values. (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Jan 23, 2025
1 parent 63ad589 commit 073540b
Show file tree
Hide file tree
Showing 3 changed files with 658 additions and 514 deletions.
38 changes: 21 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ import (
"time"

metalgo "github.com/metal-stack/metal-go"
"github.com/metal-stack/metal-metrics-exporter/pkg/collector"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
client metalgo.Client
)

func main() {
envOrDefault := func(key, fallback string) string {
if val := os.Getenv(key); val != "" {
Expand All @@ -34,7 +32,7 @@ func main() {
err error
)

client, err = metalgo.NewDriver(url, "", hmac)
client, err := metalgo.NewDriver(url, "", hmac)
if err != nil {
log.Error("error creating client", "error", err)
os.Exit(1)
Expand All @@ -52,23 +50,33 @@ func main() {
os.Exit(1)
}

c := collector.New(client, updateTimeout)

prometheus.MustRegister(c)

// initialize metrics before starting to serve them to prevent empty values
log.Info("initializing metrics...")

err = c.Update()
if err != nil {
log.Error("error during initial update", "error", err)
os.Exit(1)
}

go func() {
var (
initialUpdateSuccess = false
failCount = 0
failCount = 0
)

for {
log.Info("next fetch in " + fetchInterval.String())
time.Sleep(fetchInterval)

log.Info("updating metrics...")
start := time.Now()

err = update(updateTimeout)
err = c.Update()
if err != nil {
if !initialUpdateSuccess {
log.Error("error during initial update", "error", err)
os.Exit(1)
}

log.Error("error during update", "error", err, "took", time.Since(start).String(), "fail-count", failCount)
failCount++

Expand All @@ -77,13 +85,9 @@ func main() {
os.Exit(1)
}
} else {
initialUpdateSuccess = true
failCount = 0
log.Info("metrics updated successfully", "took", time.Since(start).String())
}

log.Info("next fetch in " + fetchInterval.String())
time.Sleep(fetchInterval)
}
}()

Expand Down
Loading

0 comments on commit 073540b

Please sign in to comment.