You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-06-15 00:15:15 +02:00
74 lines
1.4 KiB
Go
74 lines
1.4 KiB
Go
package collector
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io/ioutil"
|
|
"net"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Expvar provides the ability to receive metrics
|
|
// from internal services using expvar.
|
|
type Expvar struct {
|
|
host string
|
|
tr *http.Transport
|
|
client http.Client
|
|
}
|
|
|
|
// New creates a Expvar for collection metrics.
|
|
func New(host string) (*Expvar, error) {
|
|
tr := http.Transport{
|
|
Proxy: http.ProxyFromEnvironment,
|
|
DialContext: (&net.Dialer{
|
|
Timeout: 30 * time.Second,
|
|
KeepAlive: 30 * time.Second,
|
|
DualStack: true,
|
|
}).DialContext,
|
|
MaxIdleConns: 2,
|
|
IdleConnTimeout: 90 * time.Second,
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
}
|
|
|
|
exp := Expvar{
|
|
host: host,
|
|
tr: &tr,
|
|
client: http.Client{
|
|
Transport: &tr,
|
|
Timeout: 1 * time.Second,
|
|
},
|
|
}
|
|
|
|
return &exp, nil
|
|
}
|
|
|
|
func (exp *Expvar) Collect() (map[string]interface{}, error) {
|
|
req, err := http.NewRequest("GET", exp.host, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := exp.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
msg, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return nil, errors.New(string(msg))
|
|
}
|
|
|
|
data := make(map[string]interface{})
|
|
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return data, nil
|
|
}
|