Create a JolokiaClient. allowing to inject a stub implementation

This commit is contained in:
saiello 2015-10-29 17:00:26 +01:00 committed by Cameron Sparr
parent eabc0875de
commit 55c598f9ff
1 changed files with 27 additions and 5 deletions

View File

@ -26,7 +26,20 @@ type Metric struct {
Drop []string Drop []string
} }
type JolokiaClient interface {
MakeRequest(req *http.Request) (*http.Response, error)
}
type JolokiaClientImpl struct {
client *http.Client
}
func (c JolokiaClientImpl) MakeRequest(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}
type Jolokia struct { type Jolokia struct {
jClient JolokiaClient
Context string Context string
Servers []Server Servers []Server
Metrics []Metric Metrics []Metric
@ -67,9 +80,18 @@ func (j *Jolokia) Description() string {
return "Read JMX metrics through Jolokia" return "Read JMX metrics through Jolokia"
} }
func getAttr(requestUrl *url.URL) (map[string]interface{}, error) { func (j *Jolokia) getAttr(requestUrl *url.URL) (map[string]interface{}, error) {
//make request // Create + send request
resp, err := http.Get(requestUrl.String()) req, err := http.NewRequest("GET", requestUrl.String(), nil)
if err != nil {
return nil, err
}
resp, err := j.jClient.MakeRequest(req)
if err != nil {
return nil, err
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -166,7 +188,7 @@ func (j *Jolokia) Gather(acc plugins.Accumulator) error {
return err return err
} }
out, _ := getAttr(requestUrl) out, _ := j.getAttr(requestUrl)
if values, ok := out["value"]; ok { if values, ok := out["value"]; ok {
switch values.(type) { switch values.(type) {
@ -186,6 +208,6 @@ func (j *Jolokia) Gather(acc plugins.Accumulator) error {
func init() { func init() {
plugins.Add("jolokia", func() plugins.Plugin { plugins.Add("jolokia", func() plugins.Plugin {
return &Jolokia{} return &Jolokia{jClient: &JolokiaClientImpl{client: &http.Client{}}}
}) })
} }