Update the config format.
This commit is contained in:
parent
daa8dc2bc6
commit
6894033511
|
@ -8,22 +8,13 @@ import (
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/influxdata/telegraf"
|
"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 {
|
type GithubWebhook struct {
|
||||||
Path string
|
Path string
|
||||||
acc telegraf.Accumulator
|
acc telegraf.Accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGithubWebhook(path string) *GithubWebhook {
|
|
||||||
return &GithubWebhook{Path: path}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
func (gh *GithubWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
||||||
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
router.HandleFunc(gh.Path, gh.eventHandler).Methods("POST")
|
||||||
log.Printf("Started the webhooks_github on %s\n", gh.Path)
|
log.Printf("Started the webhooks_github on %s\n", gh.Path)
|
||||||
|
|
|
@ -11,8 +11,7 @@ import (
|
||||||
|
|
||||||
func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
|
func GithubWebhookRequest(event string, jsonString string, t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
gh := NewGithubWebhook("/github")
|
gh := &GithubWebhook{Path: "/github", acc: &acc}
|
||||||
gh.acc = &acc
|
|
||||||
req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
|
req, _ := http.NewRequest("POST", "/github", strings.NewReader(jsonString))
|
||||||
req.Header.Add("X-Github-Event", event)
|
req.Header.Add("X-Github-Event", event)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
|
@ -10,22 +10,13 @@ import (
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/influxdata/telegraf"
|
"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 {
|
type RollbarWebhook struct {
|
||||||
Path string
|
Path string
|
||||||
acc telegraf.Accumulator
|
acc telegraf.Accumulator
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRollbarWebhook(path string) *RollbarWebhook {
|
|
||||||
return &RollbarWebhook{Path: path}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
func (rb *RollbarWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
||||||
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
|
router.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
|
||||||
log.Printf("Started the webhooks_rollbar on %s\n", rb.Path)
|
log.Printf("Started the webhooks_rollbar on %s\n", rb.Path)
|
||||||
|
|
|
@ -21,8 +21,7 @@ func postWebhooks(rb *RollbarWebhook, eventBody string) *httptest.ResponseRecord
|
||||||
|
|
||||||
func TestNewItem(t *testing.T) {
|
func TestNewItem(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
rb := NewRollbarWebhook("/rollbar")
|
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc}
|
||||||
rb.acc = &acc
|
|
||||||
resp := postWebhooks(rb, NewItemJSON())
|
resp := postWebhooks(rb, NewItemJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
t.Errorf("POST new_item returned HTTP status code %v.\nExpected %v", 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) {
|
func TestDeploy(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
var acc testutil.Accumulator
|
||||||
rb := NewRollbarWebhook("/rollbar")
|
rb := &RollbarWebhook{Path: "/rollbar", acc: &acc}
|
||||||
rb.acc = &acc
|
|
||||||
resp := postWebhooks(rb, DeployJSON())
|
resp := postWebhooks(rb, DeployJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
t.Errorf("POST deploy returned HTTP status code %v.\nExpected %v", 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) {
|
func TestUnknowItem(t *testing.T) {
|
||||||
rb := NewRollbarWebhook("/rollbar")
|
rb := &RollbarWebhook{Path: "/rollbar"}
|
||||||
resp := postWebhooks(rb, UnknowJSON())
|
resp := postWebhooks(rb, UnknowJSON())
|
||||||
if resp.Code != http.StatusOK {
|
if resp.Code != http.StatusOK {
|
||||||
t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
t.Errorf("POST unknow returned HTTP status code %v.\nExpected %v", resp.Code, http.StatusOK)
|
||||||
|
|
|
@ -4,14 +4,20 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/influxdata/telegraf"
|
"github.com/influxdata/telegraf"
|
||||||
"github.com/influxdata/telegraf/plugins/inputs"
|
"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() {
|
func init() {
|
||||||
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
|
inputs.Add("webhooks", func() telegraf.Input { return NewWebhooks() })
|
||||||
}
|
}
|
||||||
|
@ -19,12 +25,8 @@ func init() {
|
||||||
type Webhooks struct {
|
type Webhooks struct {
|
||||||
ServiceAddress string
|
ServiceAddress string
|
||||||
|
|
||||||
Webhook []WebhookConfig
|
Github *github.GithubWebhook
|
||||||
}
|
Rollbar *rollbar.RollbarWebhook
|
||||||
|
|
||||||
type WebhookConfig struct {
|
|
||||||
Name string
|
|
||||||
Path string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWebhooks() *Webhooks {
|
func NewWebhooks() *Webhooks {
|
||||||
|
@ -36,14 +38,12 @@ func (wb *Webhooks) SampleConfig() string {
|
||||||
## Address and port to host Webhook listener on
|
## Address and port to host Webhook listener on
|
||||||
service_address = ":1619"
|
service_address = ":1619"
|
||||||
|
|
||||||
[[inputs.webhooks.webhook]]
|
[inputs.webhooks.github]
|
||||||
name = "github"
|
|
||||||
path = "/github"
|
path = "/github"
|
||||||
|
|
||||||
[[inputs.webhooks.webhook]]
|
[inputs.webhooks.rollbar]
|
||||||
name = "rollbar"
|
path = "/rollbar"
|
||||||
path = "/rollbar"
|
`
|
||||||
`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (wb *Webhooks) Description() string {
|
func (wb *Webhooks) Description() string {
|
||||||
|
@ -56,20 +56,32 @@ func (wb *Webhooks) Gather(_ telegraf.Accumulator) error {
|
||||||
|
|
||||||
func (wb *Webhooks) Listen(acc telegraf.Accumulator) {
|
func (wb *Webhooks) Listen(acc telegraf.Accumulator) {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
for _, webhook := range wb.Webhook {
|
|
||||||
if plugin, ok := webhooks_models.Webhooks[webhook.Name]; ok {
|
for _, webhook := range wb.AvailableWebhooks() {
|
||||||
sub := plugin(webhook.Path)
|
webhook.Register(r, acc)
|
||||||
sub.Register(r, acc)
|
|
||||||
} else {
|
|
||||||
log.Printf("Webhook %s is unknow\n", webhook.Name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
|
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error starting server: %v", err)
|
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 {
|
func (wb *Webhooks) Start(acc telegraf.Accumulator) error {
|
||||||
go wb.Listen(acc)
|
go wb.Listen(acc)
|
||||||
log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress)
|
log.Printf("Started the webhooks service on %s\n", wb.ServiceAddress)
|
||||||
|
|
|
@ -1,6 +0,0 @@
|
||||||
package webhooks_all
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "github.com/influxdata/telegraf/plugins/inputs/webhooks/github"
|
|
||||||
_ "github.com/influxdata/telegraf/plugins/inputs/webhooks/rollbar"
|
|
||||||
)
|
|
|
@ -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
|
|
||||||
}
|
|
Loading…
Reference in New Issue