Update rollbar webhook plugin.

This commit is contained in:
François de Metz 2016-05-25 19:38:44 +02:00 committed by Cyril Duez
parent 2221344028
commit b09f7c89c2
1 changed files with 6 additions and 20 deletions

View File

@ -3,7 +3,6 @@ package rollbar_webhooks
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -20,7 +19,7 @@ func init() {
} }
type RollbarWebhooks struct { type RollbarWebhooks struct {
ServiceAddress string Path string
// Lock for the struct // Lock for the struct
sync.Mutex sync.Mutex
// Events buffer to store events between Gather calls // Events buffer to store events between Gather calls
@ -33,8 +32,8 @@ func NewRollbarWebhooks() *RollbarWebhooks {
func (rb *RollbarWebhooks) SampleConfig() string { func (rb *RollbarWebhooks) SampleConfig() string {
return ` return `
## Address and port to host Webhook listener on ## Path of the global server
service_address = ":1619" path = "/rollbar"
` `
} }
@ -52,25 +51,12 @@ func (rb *RollbarWebhooks) Gather(acc telegraf.Accumulator) error {
return nil return nil
} }
func (rb *RollbarWebhooks) Listen() { func (rb *RollbarWebhooks) Register(r *mux.Router, _ telegraf.Accumulator) error {
r := mux.NewRouter() r.HandleFunc(rb.Path, rb.eventHandler).Methods("POST")
r.HandleFunc("/", rb.eventHandler).Methods("POST") log.Printf("Register the rollbar_webhooks router on %s\n", rb.Path)
err := http.ListenAndServe(fmt.Sprintf("%s", rb.ServiceAddress), r)
if err != nil {
log.Printf("Error starting server: %v", err)
}
}
func (rb *RollbarWebhooks) Start(_ telegraf.Accumulator) error {
go rb.Listen()
log.Printf("Started the rollbar_webhooks service on %s\n", rb.ServiceAddress)
return nil return nil
} }
func (rb *RollbarWebhooks) Stop() {
log.Println("Stopping the rbWebhooks service")
}
func (rb *RollbarWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) { func (rb *RollbarWebhooks) eventHandler(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close() defer r.Body.Close()
data, err := ioutil.ReadAll(r.Body) data, err := ioutil.ReadAll(r.Body)