Skip to content

Instantly share code, notes, and snippets.

@p4tin
Last active August 7, 2018 08:35
Show Gist options
  • Save p4tin/ab1c7b610f3c4a4b87226f5cf1e0b58c to your computer and use it in GitHub Desktop.
Save p4tin/ab1c7b610f3c4a4b87226f5cf1e0b58c to your computer and use it in GitHub Desktop.
Influx Play
package main
import (
"strings"
"net/http"
"fmt"
"time"
"io/ioutil"
"net/url"
)
func sendEventToInflux(severity string, cartId string, sterlingOrderId string, retries int) {
body := strings.NewReader(fmt.Sprintf(`orderfulfillment,host=server01,severity=%s,cartId=%s,sterling_order_number=%s retries=%d %d`,
severity,
cartId,
sterlingOrderId,
retries,
time.Now().UTC().UnixNano()))
req, err := http.NewRequest("POST", "http://localhost:8086/write?db=mydb", body)
if err != nil {
fmt.Println(err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err.Error())
}
var msg []byte
x, y := resp.Body.Read(msg)
fmt.Println(string(msg), x, y)
defer resp.Body.Close()
}
func queryDataForCartID(cartId string) {
queryStr := fmt.Sprintf("db=mydb&q=SELECT * FROM orderfulfillment where cart_id='%s'", cartId)
t := &url.URL{Path: queryStr}
queryUrl := "http://localhost:8086/query?pretty=true&" + t.String()
resp, err := http.Get(queryUrl)
if err != nil {
fmt.Println(err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(string(body))
}
}
func main() {
//sendEventToInflux("INFO", "abc124", "FB1233456", 1)
queryDataForCartID("abc124")
}
brew install influxdb
docker run -p 8086:8086 -v influxdb:/var/lib/influxdb influxdb:alpine
curl -XPOST "http://localhost:8086/query" --data-urlencode "q=CREATE DATABASE mydb"
curl -XPOST "http://localhost:8086/write?db=mydb" -d 'cpu,host=server01,region=uswest load=42 1434055562000000000'
curl -XPOST "http://localhost:8086/write?db=mydb" -d 'cpu,host=server02,region=uswest load=78 1434055562000000000'
curl -XPOST "http://localhost:8086/write?db=mydb" -d 'cpu,host=server03,region=useast load=15.4 1434055562000000000'
curl -G "http://localhost:8086/query?pretty=true" --data-urlencode "db=mydb" --data-urlencode "q=SELECT mean(load) FROM cpu WHERE region='uswest'"
curl -G "http://localhost:8086/query?pretty=true" --data-urlencode "db=mydb" --data-urlencode "q=SELECT * FROM orderfulfillment WHERE cart_id='abc123'"
@pfortin-urbn
Copy link

docker run -p 3000:3000 --name=grafana -e "GF_SERVER_ROOT_URL=http://localhost:3000" -e "GF_SECURITY_ADMIN_PASSWORD=admin" grafana/grafana

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment