-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HTTP helper for Metricsets (#3413)
This should simplify the implementation of MetricSets based on HTTP.
- Loading branch information
1 parent
0a8ca7d
commit 77cec5a
Showing
13 changed files
with
139 additions
and
197 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package helper | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/elastic/beats/metricbeat/mb" | ||
) | ||
|
||
type HTTP struct { | ||
base mb.BaseMetricSet | ||
client *http.Client // HTTP client that is reused across requests. | ||
} | ||
|
||
// NewHTTP creates new http helper | ||
func NewHTTP(base mb.BaseMetricSet) *HTTP { | ||
return &HTTP{ | ||
base: base, | ||
client: &http.Client{Timeout: base.Module().Config().Timeout}, | ||
} | ||
} | ||
|
||
// FetchResponse fetches a response for the http metricset. | ||
// It's important that resp.Body has to be closed if this method is used. Before using this method | ||
// check if one of the other Fetch* methods could be used as they ensure that the Body is properly closed. | ||
func (h *HTTP) FetchResponse() (*http.Response, error) { | ||
req, err := http.NewRequest("GET", h.base.HostData().SanitizedURI, nil) | ||
if h.base.HostData().User != "" || h.base.HostData().Password != "" { | ||
req.SetBasicAuth(h.base.HostData().User, h.base.HostData().Password) | ||
} | ||
resp, err := h.client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("error making http request: %v", err) | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
// FetchContent makes an HTTP request to the configured url and returns the body content. | ||
func (h *HTTP) FetchContent() ([]byte, error) { | ||
resp, err := h.FetchResponse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != 200 { | ||
return nil, fmt.Errorf("HTTP error %d in %s: %s", resp.StatusCode, h.base.Name(), resp.Status) | ||
} | ||
|
||
return ioutil.ReadAll(resp.Body) | ||
} | ||
|
||
// FetchScanner returns a Scanner for the content. | ||
func (h *HTTP) FetchScanner() (*bufio.Scanner, error) { | ||
content, err := h.FetchContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return bufio.NewScanner(bytes.NewReader(content)), nil | ||
} | ||
|
||
// FetchJSON makes an HTTP request to the configured url and returns the JSON content. | ||
// This only works if the JSON output needed is in map[string]interface format. | ||
func (h *HTTP) FetchJSON() (map[string]interface{}, error) { | ||
|
||
body, err := h.FetchContent() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var data map[string]interface{} | ||
|
||
err = json.Unmarshal(body, &data) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.