Skip to content

Commit

Permalink
Initialize metrics that depend on labels so they exist from the start…
Browse files Browse the repository at this point in the history
… of the application (#51)
  • Loading branch information
jadolg authored May 16, 2024
1 parent 7824935 commit 5a275e7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
}
}()
}
initMetrics(c.Records)
updateRecords(c)

if !runOnce {
Expand Down
12 changes: 12 additions & 0 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ var (
)
)

// initMetrics initializes the metrics with the records provided in the configuration
// We want to have 0 as a baseline for all possible labels instead of non-existing metrics
func initMetrics(records []Record) {
for _, record := range records {
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "A").Add(0)
updateSuccessTotal.WithLabelValues(record.Host, record.Domain, "AAAA").Add(0)
updateErrorsTotal.WithLabelValues(record.Host, record.Domain).Add(0)
resolveErrorsTotal.WithLabelValues("A").Add(0)
resolveErrorsTotal.WithLabelValues("AAAA").Add(0)
}
}

func healthcheckHandler(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "OK")
Expand Down

0 comments on commit 5a275e7

Please sign in to comment.