mirror of
https://github.com/crazy-max/diun.git
synced 2026-01-02 02:57:28 +01:00
Add support for Healthchecks to monitor Diun watcher (#78)
This commit is contained in:
59
internal/app/hc.go
Normal file
59
internal/app/hc.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"text/template"
|
||||
|
||||
"github.com/crazy-max/diun/v4/internal/model"
|
||||
"github.com/crazy-max/gohealthchecks"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func (di *Diun) HealthchecksStart() {
|
||||
if di.hc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := di.hc.Start(context.Background(), gohealthchecks.PingingOptions{
|
||||
UUID: di.cfg.Watch.Healthchecks.UUID,
|
||||
}); err != nil {
|
||||
log.Error().Err(err).Msgf("Cannot send Healthchecks start event")
|
||||
}
|
||||
}
|
||||
|
||||
func (di *Diun) HealthchecksSuccess(entries *model.NotifEntries) {
|
||||
if di.hc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var logsBuf bytes.Buffer
|
||||
logsTpl := template.Must(template.New("").Parse(`{{ .CountTotal }} tag(s) have been scanned:
|
||||
* {{ .CountNew }} new tag(s) found
|
||||
* {{ .CountUpdate }} tag(s) updated
|
||||
* {{ .CountUnchange }} tag(s) unchanged
|
||||
* {{ .CountError }} tag(s) with error`))
|
||||
if err := logsTpl.Execute(&logsBuf, entries); err != nil {
|
||||
log.Error().Err(err).Msgf("Cannot create logs for Healthchecks success event")
|
||||
}
|
||||
|
||||
if err := di.hc.Success(context.Background(), gohealthchecks.PingingOptions{
|
||||
UUID: di.cfg.Watch.Healthchecks.UUID,
|
||||
Logs: logsBuf.String(),
|
||||
}); err != nil {
|
||||
log.Error().Err(err).Msgf("Cannot send Healthchecks success event")
|
||||
}
|
||||
}
|
||||
|
||||
func (di *Diun) HealthchecksFail(logs string) {
|
||||
if di.hc == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := di.hc.Fail(context.Background(), gohealthchecks.PingingOptions{
|
||||
UUID: di.cfg.Watch.Healthchecks.UUID,
|
||||
Logs: logs,
|
||||
}); err != nil {
|
||||
log.Error().Err(err).Msgf("Cannot send Healthchecks fail event")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user