main.go 932 字节
Newer Older
B
Bo Ding 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"time"
)

type Tags struct {
	Location string `json:"location"`
	Groupid  int32  `json:"groupid"`
}

type Metric struct {
	Metric    string `json:"metric"`
	Timestamp int64  `json:"timestamp"`
	Value     int32  `json:"value"`
	Tags      Tags   `json:"tags"`
}

func main() {
	client := http.Client{}
	for i := 0; i < 10; i++ {
		metric := Metric{"voltage", time.Now().UnixMilli(), 1, Tags{"A", 1}}
		json, err := json.Marshal(metric)
		if err != nil {
			panic(err)
		}
		req, err := http.NewRequest("POST", "http://localhost:6041/opentsdb/v1/put/json/rest_go", bytes.NewBuffer(json))
		if err != nil {
			panic(err)
		}
		req.Header.Set("Content-Type", "application/json")
		req.Header.Set("Authorization", "Basic cm9vdDp0YW9zZGF0YQ==")
		resp, err := client.Do(req)
		if err != nil {
			panic(err)
		}
		fmt.Printf("%v\n", resp)
		time.Sleep(time.Second)

	}
}