summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-08-17 14:41:39 +0700
committerShulhan <ms@kilabit.info>2022-08-17 14:41:39 +0700
commita3ea0c7bda6cae7a88af7dd005cd52ac40d42e57 (patch)
tree4733d2ced445af2e87b173207e35c245cf8da32c
parentc28879b9d5f2f1bea96d5aaf64f1af1fc185e7b0 (diff)
downloadhaminer-a3ea0c7bda6cae7a88af7dd005cd52ac40d42e57.tar.xz
all: make the forwarders configuration fields to be generic
Instead of single forwarder, Influxd, the Config struct now can have one or more forwarders. The kind of forwarders is defined by it subsection name, for example `[forwarder "influxd"]` defined a forwarder for influxd.
-rw-r--r--config.go11
-rw-r--r--config_forwarder.go18
-rw-r--r--config_test.go16
-rw-r--r--haminer.go18
-rw-r--r--influxd_client.go4
5 files changed, 42 insertions, 25 deletions
diff --git a/config.go b/config.go
index a9786eb..3635638 100644
--- a/config.go
+++ b/config.go
@@ -22,7 +22,7 @@ const (
// Config define options to create and run Haminer instance.
type Config struct {
- Influxd ConfigForwarder
+ Forwarders map[string]*ConfigForwarder `ini:"forwarder"`
// Listen is the address where Haminer will bind and receiving
// log from HAProxy.
@@ -68,6 +68,7 @@ func (cfg *Config) Load(path string) (err error) {
logp = `Load`
in *ini.Ini
+ fw *ConfigForwarder
)
in, err = ini.Open(path)
@@ -89,9 +90,11 @@ func (cfg *Config) Load(path string) (err error) {
return fmt.Errorf(`%s: %w`, logp, err)
}
- err = cfg.Influxd.init()
- if err != nil {
- return fmt.Errorf(`%s: %w`, logp, err)
+ for _, fw = range cfg.Forwarders {
+ err = fw.init()
+ if err != nil {
+ return fmt.Errorf(`%s: %w`, logp, err)
+ }
}
return nil
diff --git a/config_forwarder.go b/config_forwarder.go
index 357f562..408d1bd 100644
--- a/config_forwarder.go
+++ b/config_forwarder.go
@@ -10,27 +10,29 @@ const (
influxdVersion1 = `v1`
influxdVersion2 = `v2`
+
+ forwarderInfluxd = `influxd`
)
-// ConfigForwarder contains configuration for forwarding the logs to Influxd.
+// ConfigForwarder contains configuration for forwarding the logs.
type ConfigForwarder struct {
- Version string `ini:"forwarder:influxd:version"`
+ Version string `ini:"::version"`
- Url string `ini:"forwarder:influxd:url"`
+ Url string `ini:"::url"`
apiWrite string
headerToken string
- Bucket string `ini:"forwarder:influxd:bucket"`
+ Bucket string `ini:"::bucket"`
// Fields for HTTP API v1.
- User string `ini:"forwarder:influxd:user"`
- Pass string `ini:"forwarder:influxd:pass"`
+ User string `ini:"::user"`
+ Pass string `ini:"::pass"`
// Fields for HTTP API v2.
- Org string `ini:"forwarder:influxd:org"`
- Token string `ini:"forwarder:influxd:token"`
+ Org string `ini:"::org"`
+ Token string `ini:"::token"`
}
// init check, validate, and initialize the configuration values.
diff --git a/config_test.go b/config_test.go
index 779c59a..244bfa6 100644
--- a/config_test.go
+++ b/config_test.go
@@ -57,13 +57,15 @@ func TestLoad(t *testing.T) {
desc: "With path exist",
in: "testdata/haminer.conf",
exp: &Config{
- Influxd: ConfigForwarder{
- Version: `v2`,
- Url: `http://127.0.0.1:8086`,
- Org: `kilabit.info`,
- Bucket: `haproxy`,
- apiWrite: `http://127.0.0.1:8086/api/v2/write?bucket=haproxy&org=kilabit.info&precision=ns`,
- headerToken: `Token `,
+ Forwarders: map[string]*ConfigForwarder{
+ `influxd`: &ConfigForwarder{
+ Version: `v2`,
+ Url: `http://127.0.0.1:8086`,
+ Org: `kilabit.info`,
+ Bucket: `haproxy`,
+ apiWrite: `http://127.0.0.1:8086/api/v2/write?bucket=haproxy&org=kilabit.info&precision=ns`,
+ headerToken: `Token `,
+ },
},
Listen: `0.0.0.0:8080`,
listenAddr: `0.0.0.0`,
diff --git a/haminer.go b/haminer.go
index 256a068..9b1e080 100644
--- a/haminer.go
+++ b/haminer.go
@@ -39,15 +39,21 @@ func NewHaminer(cfg *Config) (h *Haminer) {
}
func (h *Haminer) createForwarder() {
- if len(h.cfg.Influxd.Url) == 0 {
- return
- }
-
var (
- fwder = NewInfluxdClient(&h.cfg.Influxd)
+ fwCfg *ConfigForwarder
+ influxdc *InfluxdClient
+ fwName string
)
- h.ff = append(h.ff, fwder)
+ for fwName, fwCfg = range h.cfg.Forwarders {
+ switch fwName {
+ case forwarderInfluxd:
+ influxdc = NewInfluxdClient(fwCfg)
+ if influxdc != nil {
+ h.ff = append(h.ff, influxdc)
+ }
+ }
+ }
}
// Start will listen for UDP packet and start consuming log, parse, and
diff --git a/influxd_client.go b/influxd_client.go
index 9c71523..7c8a54a 100644
--- a/influxd_client.go
+++ b/influxd_client.go
@@ -54,6 +54,10 @@ type InfluxdClient struct {
// NewInfluxdClient will create, initialize, and return new Influxd client.
func NewInfluxdClient(cfg *ConfigForwarder) (cl *InfluxdClient) {
+ if len(cfg.Url) == 0 {
+ return nil
+ }
+
cl = &InfluxdClient{
cfg: cfg,
}