modularize
This commit is contained in:
8
cachet/cachet.go
Normal file
8
cachet/cachet.go
Normal file
@@ -0,0 +1,8 @@
|
||||
package cachet
|
||||
|
||||
import "os"
|
||||
|
||||
// apiUrl -> https://demo.cachethq.io/api
|
||||
// apiToken -> qwertyuiop
|
||||
var apiUrl = os.Getenv("CACHET_API")
|
||||
var apiToken = os.Getenv("CACHET_TOKEN")
|
||||
42
cachet/metrics.go
Normal file
42
cachet/metrics.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"bytes"
|
||||
"strconv"
|
||||
"net/http"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func SendMetric(metricId int, delay int64) {
|
||||
jsonBytes, err := json.Marshal(&map[string]interface{}{
|
||||
"value": delay,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", apiUrl + "/metrics/" + strconv.Itoa(metricId) + "/points", bytes.NewBuffer(jsonBytes))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", apiToken)
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println(strconv.Itoa(resp.StatusCode) + " " + string(body))
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
fmt.Println("Could not log data point!")
|
||||
}
|
||||
}
|
||||
49
cachet/monitor.go
Normal file
49
cachet/monitor.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package cachet
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const timeout = time.Duration(time.Second)
|
||||
|
||||
type Monitor struct {
|
||||
Url string `json:"url"`
|
||||
MetricId int `json:"metric_id"`
|
||||
}
|
||||
|
||||
func (monitor *Monitor) Run() {
|
||||
reqStart := getMs()
|
||||
err := monitor.doRequest()
|
||||
lag := getMs() - reqStart
|
||||
|
||||
failed := false
|
||||
if err != nil {
|
||||
failed = true
|
||||
}
|
||||
|
||||
if failed == true {
|
||||
fmt.Println("Req failed")
|
||||
}
|
||||
|
||||
SendMetric(1, lag)
|
||||
}
|
||||
|
||||
func (monitor *Monitor) doRequest() error {
|
||||
client := &http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
resp, err := client.Get(monitor.Url) // http://127.0.0.1:1337
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getMs() int64 {
|
||||
return time.Now().UnixNano() / int64(time.Millisecond)
|
||||
}
|
||||
67
main.go
67
main.go
@@ -1,69 +1,22 @@
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
// "time"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"io/ioutil"
|
||||
"encoding/json"
|
||||
"time"
|
||||
"./cachet"
|
||||
)
|
||||
|
||||
const timeout = time.Duration(time.Second)
|
||||
|
||||
func main() {
|
||||
monitors := []cachet.Monitor{
|
||||
cachet.Monitor{
|
||||
Url: "https://nodegear.io/ping",
|
||||
MetricId: 1,
|
||||
},
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Second)
|
||||
for _ = range ticker.C {
|
||||
reqStart := time.Now().UnixNano() / int64(time.Millisecond)
|
||||
doRequest()
|
||||
reqEnd := time.Now().UnixNano() / int64(time.Millisecond)
|
||||
go sendMetric(reqEnd - reqStart)
|
||||
for _, monitor := range monitors {
|
||||
go monitor.Run()
|
||||
}
|
||||
}
|
||||
|
||||
func doRequest() error {
|
||||
client := http.Client{
|
||||
Timeout: timeout,
|
||||
}
|
||||
resp, err := client.Get("https://nodegear.io/ping") // http://127.0.0.1:1337
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func sendMetric(delay int64) {
|
||||
js := &map[string]interface{}{
|
||||
"value": delay,
|
||||
}
|
||||
|
||||
jsonBytes, err := json.Marshal(&js)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", "https://demo.cachethq.io/api/metrics/1/points", bytes.NewBuffer(jsonBytes))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Cachet-Token", "5wQt9MnJXmhnQsDI8Hmv")
|
||||
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println(string(body))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user