add heartbeat

This commit is contained in:
David Dollar 2019-09-25 10:52:23 -04:00
parent 7b7c010792
commit 7a6220121f
No known key found for this signature in database
GPG Key ID: AFAF263FB45B2124
4 changed files with 89 additions and 1 deletions

31
pkg/metrics/metrics.go Normal file
View File

@ -0,0 +1,31 @@
package metrics
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
type Metrics struct {
url string
}
func New(url string) *Metrics {
return &Metrics{url: url}
}
func (m *Metrics) Post(name string, attrs map[string]interface{}) error {
data, err := json.Marshal(attrs)
if err != nil {
return err
}
res, err := http.Post(fmt.Sprintf("%s/%s", m.url, name), "application/json", bytes.NewReader(data))
if err != nil {
return err
}
defer res.Body.Close()
return nil
}

21
provider/aws/heartbeat.go Normal file
View File

@ -0,0 +1,21 @@
package aws
import (
"strings"
"github.com/convox/convox/pkg/common"
)
func (p *Provider) Heartbeat() (map[string]interface{}, error) {
data, err := common.Get("http://169.254.169.254/latest/meta-data/instance-type")
if err != nil {
return nil, err
}
hs := map[string]interface{}{
"instance_type": strings.TrimSpace(string(data)),
"region": p.Region,
}
return hs, nil
}

36
provider/gcp/heartbeat.go Normal file
View File

@ -0,0 +1,36 @@
package gcp
import (
"io/ioutil"
"net/http"
"strings"
)
func (p *Provider) Heartbeat() (map[string]interface{}, error) {
req, err := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/instance/machine-type", nil)
if err != nil {
return nil, err
}
req.Header.Add("Metadata-Flavor", "Google")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
tparts := strings.Split(strings.TrimSpace(string(data)), "/")
hs := map[string]interface{}{
"instance_type": tparts[len(tparts)-1],
"region": p.Region,
}
return hs, nil
}

View File

@ -212,7 +212,7 @@ func (p *Provider) heartbeat() error {
ms[k] = v
}
if err := p.metrics.Post("heartbeat", hs); err != nil {
if err := p.metrics.Post("heartbeat", ms); err != nil {
return err
}