-
Notifications
You must be signed in to change notification settings - Fork 99
/
reloader.go
66 lines (54 loc) · 1.64 KB
/
reloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright © 2018 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: BSD-2-Clause
package fluentd
import (
"context"
"fmt"
"io"
"net/http"
"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
}
// NewReloader will notify on the given rpc port
func NewReloader(ctx context.Context, port int) *Reloader {
return &Reloader{
port: port,
}
}
// ReloadConfiguration talks to fluentd's RPC endpoint. If r is nil does nothing
func (r *Reloader) ReloadConfiguration() {
if r == nil {
logrus.Infof("Not reloading fluentd (fake or filesystem datasource used)")
return
}
logrus.Infof("Reloading fluentd configuration via /api/%s", gracefulReloadConf)
if err := r.rpc(gracefulReloadConf); err != nil {
logrus.Warnf("graceful reload failed: %+v", err)
logrus.Infof("Reloading fluentd configuration via /api/%s", reloadConf)
if err := r.rpc(reloadConf); err != nil {
logrus.Error(err.Error())
}
}
}
// 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 {
return fmt.Errorf("fluentd %s request failed: %w", method, err)
}
if resp.StatusCode != 200 {
body, _ := io.ReadAll(resp.Body)
resp.Body.Close()
return fmt.Errorf("fluentd %s endpoint returned statuscode %v; response: %v", method, resp.StatusCode, string(body))
}
return nil
}