-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathapi_base.go
48 lines (39 loc) · 920 Bytes
/
api_base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package collector
import (
"encoding/json"
"github.com/prometheus/common/log"
"net/http"
)
// HTTPHandler type
type HTTPHandler struct {
Endpoint string
}
// Get method for HTTPHandler
func (h *HTTPHandler) Get() (http.Response, error) {
response, err := http.Get(h.Endpoint)
if err != nil {
return http.Response{}, err
}
return *response, nil
}
// HTTPHandlerInterface interface
type HTTPHandlerInterface interface {
Get() (http.Response, error)
}
func getMetrics(h HTTPHandlerInterface, target interface{}) error {
response, err := h.Get()
if err != nil {
log.Errorf("Cannot retrieve metrics: %s", err)
return nil
}
defer func() {
err = response.Body.Close()
if err != nil {
log.Errorf("Cannot close response body: %v", err)
}
}()
if err := json.NewDecoder(response.Body).Decode(target); err != nil {
log.Errorf("Cannot parse Logstash response json: %s", err)
}
return nil
}