You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.0 KiB
55 lines
1.0 KiB
7 months ago
|
package client
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type DataItem struct {
|
||
|
Metric string `json:"metric"`
|
||
|
Tags map[string]string `json:"tags"`
|
||
|
Timestamp int64 `json:"timestamp"`
|
||
|
Value any `json:"value"`
|
||
|
}
|
||
|
|
||
|
type ReportResponse struct {
|
||
|
RequestId string `json:"request_id,omitempty"`
|
||
|
Message string `json:"message,omitempty"`
|
||
|
}
|
||
|
|
||
|
func (c *Client) Report(dataItem []DataItem) (*ReportResponse, error) {
|
||
|
reportJsonBytes, err := json.Marshal(dataItem)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// send report data
|
||
|
resp, err := http.Post(c.config.CenterServer, "application/json", bytes.NewBuffer(reportJsonBytes))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// get response
|
||
|
defer func(Body io.ReadCloser) {
|
||
|
_ = Body.Close()
|
||
|
}(resp.Body)
|
||
|
|
||
|
respBytes, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
fmt.Println(string(respBytes))
|
||
|
|
||
|
response := ReportResponse{}
|
||
|
err = json.Unmarshal(respBytes, &response)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return &response, nil
|
||
|
}
|