Skip to content

Commit

Permalink
fix: fallback to reload when gracefulReload fails
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Davim <[email protected]>
  • Loading branch information
luisdavim committed Feb 9, 2024
1 parent 0dccf18 commit bc982d6
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions config-reloader/fluentd/reloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"github.com/sirupsen/logrus"
)

type RPCMethod string

const (
gracefulReloadConf RPCMethod = "config.gracefulReload"
reloadConf RPCMethod = "config.reload"
)

// Reloader sends a reload signal to fluentd
type Reloader struct {
port int
Expand All @@ -31,14 +38,29 @@ func (r *Reloader) ReloadConfiguration() {
return
}

logrus.Infof("Reloading fluentd configuration gracefully via POST to /api/config.gracefulReload")
logrus.Infof("Reloading fluentd configuration via POST to /api/%s", gracefulReloadConf)
if err := r.rpc(gracefulReloadConf); err != nil {
logrus.Warnf("graceful reload failed: %+v", err)
logrus.Infof("Reloading fluentd configuration via POST to /api/%s", reloadConf)
if err := r.rpc(reloadConf); err != nil {
logrus.Error(err.Error())
}
}
}

resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/api/config.gracefulReload", r.port))
// rpc calls the given fluentd HTTP RPC endpoint
// for more details see: https://docs.fluentd.org/deployment/rpc
func (r *Reloader) rpc(method RPCMethod) error {
resp, err := http.Get(fmt.Sprintf("http://127.0.0.1:%d/api/%s", r.port, method))
if err != nil {
logrus.Errorf("fluentd config.gracefulReload request failed: %+v", err)
} else if resp.StatusCode != 200 {
defer resp.Body.Close()
return fmt.Errorf("fluentd %s request failed: %w", method, err)
}

if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
logrus.Errorf("fluentd config.gracefulReload endpoint returned statuscode %v; response: %v", resp.StatusCode, string(body))
resp.Body.Close()
return fmt.Errorf("fluentd %s endpoint returned statuscode %v; response: %v", method, resp.StatusCode, string(body))
}

return nil
}

0 comments on commit bc982d6

Please sign in to comment.