From 55c598f9ff4d4090c2ffb4a1eec2430a975b9158 Mon Sep 17 00:00:00 2001 From: saiello Date: Thu, 29 Oct 2015 17:00:26 +0100 Subject: [PATCH] Create a JolokiaClient. allowing to inject a stub implementation --- plugins/jolokia/jolokia.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/plugins/jolokia/jolokia.go b/plugins/jolokia/jolokia.go index e9230208d..a9ec11635 100644 --- a/plugins/jolokia/jolokia.go +++ b/plugins/jolokia/jolokia.go @@ -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 @@ -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 } @@ -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) { @@ -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{}}} }) }