From 1cadc9b3b3a5d18f4a838c0152a75d9e4aec8083 Mon Sep 17 00:00:00 2001 From: Matej Kramny Date: Sun, 15 Mar 2015 20:58:14 +0100 Subject: [PATCH] modularize --- cachet/cachet.go | 8 ++++++ cachet/metrics.go | 42 +++++++++++++++++++++++++++++ cachet/monitor.go | 49 +++++++++++++++++++++++++++++++++ main.go | 69 ++++++++--------------------------------------- 4 files changed, 110 insertions(+), 58 deletions(-) create mode 100644 cachet/cachet.go create mode 100644 cachet/metrics.go create mode 100644 cachet/monitor.go diff --git a/cachet/cachet.go b/cachet/cachet.go new file mode 100644 index 0000000..7c2b25d --- /dev/null +++ b/cachet/cachet.go @@ -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") \ No newline at end of file diff --git a/cachet/metrics.go b/cachet/metrics.go new file mode 100644 index 0000000..791be75 --- /dev/null +++ b/cachet/metrics.go @@ -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!") + } +} diff --git a/cachet/monitor.go b/cachet/monitor.go new file mode 100644 index 0000000..81d24a3 --- /dev/null +++ b/cachet/monitor.go @@ -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) +} diff --git a/main.go b/main.go index 5fbfc68..1534a72 100644 --- a/main.go +++ b/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)) -}