diff --git a/plugins/inputs/webhooks/github/github_webhooks.go b/plugins/inputs/webhooks/github/github_webhooks.go index 6ef795430..5327363f4 100644 --- a/plugins/inputs/webhooks/github/github_webhooks.go +++ b/plugins/inputs/webhooks/github/github_webhooks.go @@ -8,22 +8,13 @@ import ( "github.com/gorilla/mux" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models" ) -func init() { - webhooks_models.Add("github", func(path string) webhooks_models.Webhook { return NewGithubWebhook(path) }) -} - type GithubWebhook struct { Path string acc telegraf.Accumulator } -func NewGithubWebhook(path string) *GithubWebhook { - return &GithubWebhook{Path: path} -} - func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) { router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST") log.Printf("Started the webhooks_github on %s\n", gh.Path) diff --git a/plugins/inputs/webhooks/github/github_webhooks_test.go b/plugins/inputs/webhooks/github/github_webhooks_test.go index 219e94b82..7bee5372d 100644 --- a/plugins/inputs/webhooks/github/github_webhooks_test.go +++ b/plugins/inputs/webhooks/github/github_webhooks_test.go @@ -11,8 +11,7 @@ import ( func GithubWebhookRequest(event string, jsonString string, t *testing.T) { var acc testutil.Accumulator - gh := NewGithubWebhook("/github") - gh.acc = &acc + gh := &GithubWebhook{Path: "/github", acc: &acc} req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString)) req.Header.Add("X-Github-Event", event) w := httptest.NewRecorder() diff --git a/plugins/inputs/webhooks/rollbar/rollbar_webhooks.go b/plugins/inputs/webhooks/rollbar/rollbar_webhooks.go index c09eb49c1..8b8dada50 100644 --- a/plugins/inputs/webhooks/rollbar/rollbar_webhooks.go +++ b/plugins/inputs/webhooks/rollbar/rollbar_webhooks.go @@ -10,22 +10,13 @@ import ( "github.com/gorilla/mux" "github.com/influxdata/telegraf" - "github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models" ) -func init() { - webhooks_models.Add("rollbar", func(path string) webhooks_models.Webhook { return NewRollbarWebhook(path) }) -} - type RollbarWebhook struct { Path string acc telegraf.Accumulator } -func NewRollbarWebhook(path string) *RollbarWebhook { - return &RollbarWebhook{Path: path} -} - func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator) { router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST") log.Printf("Started the webhooks_rollbar on %s\n", rb.Path) diff --git a/plugins/inputs/webhooks/rollbar/rollbar_webhooks_test.go b/plugins/inputs/webhooks/rollbar/rollbar_webhooks_test.go index 05912f1bb..9b54a8281 100644 --- a/plugins/inputs/webhooks/rollbar/rollbar_webhooks_test.go +++ b/plugins/inputs/webhooks/rollbar/rollbar_webhooks_test.go @@ -21,8 +21,7 @@ func postWebhooks(rb *RollbarWebhook, eventBody string) *httptest.ResponseRecord func TestNewItem(t *testing.T) { var acc testutil.Accumulator - rb := NewRollbarWebhook("/rollbar") - rb.acc = &acc + rb := &RollbarWebhook{Path: "/rollbar", acc: &acc} resp := postWebhooks(rb, NewItemJSON()) if resp.Code != http.StatusOK { t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) @@ -45,8 +44,7 @@ func TestNewItem(t *testing.T) { func TestDeploy(t *testing.T) { var acc testutil.Accumulator - rb := NewRollbarWebhook("/rollbar") - rb.acc = &acc + rb := &RollbarWebhook{Path: "/rollbar", acc: &acc} resp := postWebhooks(rb, DeployJSON()) if resp.Code != http.StatusOK { t.Errorf("POST deploy returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) @@ -66,7 +64,7 @@ func TestDeploy(t *testing.T) { } func TestUnknowItem(t *testing.T) { - rb := NewRollbarWebhook("/rollbar") + rb := &RollbarWebhook{Path: "/rollbar"} resp := postWebhooks(rb, UnknowJSON()) if resp.Code != http.StatusOK { t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK) diff --git a/plugins/inputs/webhooks/webhooks.go b/plugins/inputs/webhooks/webhooks.go index 3e2da25ee..ce27f48f8 100644 --- a/plugins/inputs/webhooks/webhooks.go +++ b/plugins/inputs/webhooks/webhooks.go @@ -4,14 +4,20 @@ import ( "fmt" "log" "net/http" + "reflect" "github.com/gorilla/mux" "github.com/influxdata/telegraf" "github.com/influxdata/telegraf/plugins/inputs" - _ "github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_all" - "github.com/influxdata/telegraf/plugins/inputs/webhooks/webhooks_models" + + "github.com/influxdata/telegraf/plugins/inputs/webhooks/github" + "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar" ) +type Webhook interface { + Register(router *mux.Router, acc telegraf.Accumulator) +} + func init() { inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() }) } @@ -19,12 +25,8 @@ func init() { type Webhooks struct { ServiceAddress string - Webhook []WebhookConfig -} - -type WebhookConfig struct { - Name string - Path string + Github *github.GithubWebhook + Rollbar *rollbar.RollbarWebhook } func NewWebhooks() *Webhooks { @@ -36,14 +38,12 @@ func (wb *Webhooks) SampleConfig() string { ## Address and port to host Webhook listener on service_address = ":1619" - [[inputs.webhooks.webhook]] - name = "github" + [inputs.webhooks.github] path = "/github" - [[inputs.webhooks.webhook]] - name = "rollbar" - path = "/rollbar" -` + [inputs.webhooks.rollbar] + path = "/rollbar" + ` } func (wb *Webhooks) Description() string { @@ -56,20 +56,32 @@ func (wb *Webhooks) Gather(_ telegraf.Accumulator) error { func (wb *Webhooks) Listen(acc telegraf.Accumulator) { r := mux.NewRouter() - for _, webhook := range wb.Webhook { - if plugin, ok := webhooks_models.Webhooks[webhook.Name]; ok { - sub := plugin(webhook.Path) - sub.Register(r, acc) - } else { - log.Printf("Webhook %s is unknow\n", webhook.Name) - } + + for _, webhook := range wb.AvailableWebhooks() { + webhook.Register(r, acc) } + err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r) if err != nil { log.Printf("Error starting server: %v", err) } } +// Looks for fields which implement Webhook interface +func (wb *Webhooks) AvailableWebhooks() []Webhook { + webhooks := make([]Webhook, 0) + s := reflect.ValueOf(wb).Elem() + for i := 0; i < s.NumField(); i++ { + f := s.Field(i) + + if wbPlugin, ok := f.Interface().(Webhook); ok { + webhooks = append(webhooks, wbPlugin) + } + } + + return webhooks +} + func (wb *Webhooks) Start(acc telegraf.Accumulator) error { go wb.Listen(acc) log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress) diff --git a/plugins/inputs/webhooks/webhooks_all/all.go b/plugins/inputs/webhooks/webhooks_all/all.go deleted file mode 100644 index b60566608..000000000 --- a/plugins/inputs/webhooks/webhooks_all/all.go +++ /dev/null @@ -1,6 +0,0 @@ -package webhooks_all - -import ( - _ "github.com/influxdata/telegraf/plugins/inputs/webhooks/github" - _ "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar" -) diff --git a/plugins/inputs/webhooks/webhooks_models/models.go b/plugins/inputs/webhooks/webhooks_models/models.go deleted file mode 100644 index b7a4c76f7..000000000 --- a/plugins/inputs/webhooks/webhooks_models/models.go +++ /dev/null @@ -1,16 +0,0 @@ -package webhooks_models - -import ( - "github.com/gorilla/mux" - "github.com/influxdata/telegraf" -) - -type Webhook interface { - Register(router *mux.Router, acc telegraf.Accumulator) -} - -var Webhooks map[string]func(string) Webhook = make(map[string]func(string) Webhook) - -func Add(name string, fun func(string) Webhook) { - Webhooks[name] = fun -}