Skip to content

Commit

Permalink
Create a JolokiaClient. allowing to inject a stub implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
saiello authored and sparrc committed Nov 6, 2015
1 parent eabc087 commit 55c598f
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions plugins/jolokia/jolokia.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,20 @@ type Metric struct {
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 {
jClient JolokiaClient
Context string
Servers []Server
Metrics []Metric
Expand Down Expand Up @@ -67,9 +80,18 @@ func (j *Jolokia) Description() string {
return "Read JMX metrics through Jolokia"
}

func getAttr(requestUrl *url.URL) (map[string]interface{}, error) {
//make request
resp, err := http.Get(requestUrl.String())
func (j *Jolokia) getAttr(requestUrl *url.URL) (map[string]interface{}, error) {
// Create + send request
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 {
return nil, err
}
Expand Down Expand Up @@ -166,7 +188,7 @@ func (j *Jolokia) Gather(acc plugins.Accumulator) error {
return err
}

out, _ := getAttr(requestUrl)
out, _ := j.getAttr(requestUrl)

if values, ok := out["value"]; ok {
switch values.(type) {
Expand All @@ -186,6 +208,6 @@ func (j *Jolokia) Gather(acc plugins.Accumulator) error {

func init() {
plugins.Add("jolokia", func() plugins.Plugin {
return &Jolokia{}
return &Jolokia{jClient: &JolokiaClientImpl{client: &http.Client{}}}
})
}

0 comments on commit 55c598f

Please sign in to comment.