mirror of
https://github.com/FlipsideCrypto/convox.git
synced 2026-02-06 10:56:56 +00:00
add heartbeat
This commit is contained in:
parent
7b7c010792
commit
7a6220121f
31
pkg/metrics/metrics.go
Normal file
31
pkg/metrics/metrics.go
Normal 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
21
provider/aws/heartbeat.go
Normal 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
36
provider/gcp/heartbeat.go
Normal 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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user