github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/librato/client.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 package librato 10 11 import ( 12 "bytes" 13 "encoding/json" 14 "fmt" 15 "io/ioutil" 16 "net/http" 17 ) 18 19 const Operations = "operations" 20 const OperationsShort = "ops" 21 22 type LibratoClient struct { 23 Email, Token string 24 } 25 26 //属性字符串 27 const ( 28 //显示属性 29 Color = "color" 30 DisplayMax = "display_max" 31 DisplayMin = "display_min" 32 DisplayUnitsLong = "display_units_long" 33 DisplayUnitsShort = "display_units_short" 34 DisplayStacked = "display_stacked" 35 DisplayTransform = "display_transform" 36 //特殊仪表显示属性 37 SummarizeFunction = "summarize_function" 38 Aggregate = "aggregate" 39 40 //公制键 41 Name = "name" 42 Period = "period" 43 Description = "description" 44 DisplayName = "display_name" 45 Attributes = "attributes" 46 47 //测量键 48 MeasureTime = "measure_time" 49 Source = "source" 50 Value = "value" 51 52 //专用仪表键 53 Count = "count" 54 Sum = "sum" 55 Max = "max" 56 Min = "min" 57 SumSquares = "sum_squares" 58 59 //批密钥 60 Counters = "counters" 61 Gauges = "gauges" 62 63 MetricsPostUrl = "https://度量API.libato.com/v1/metrics“ 64 ) 65 66 type Measurement map[string]interface{} 67 type Metric map[string]interface{} 68 69 type Batch struct { 70 Gauges []Measurement `json:"gauges,omitempty"` 71 Counters []Measurement `json:"counters,omitempty"` 72 MeasureTime int64 `json:"measure_time"` 73 Source string `json:"source"` 74 } 75 76 func (c *LibratoClient) PostMetrics(batch Batch) (err error) { 77 var ( 78 js []byte 79 req *http.Request 80 resp *http.Response 81 ) 82 83 if len(batch.Counters) == 0 && len(batch.Gauges) == 0 { 84 return nil 85 } 86 87 if js, err = json.Marshal(batch); err != nil { 88 return 89 } 90 91 if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil { 92 return 93 } 94 95 req.Header.Set("Content-Type", "application/json") 96 req.SetBasicAuth(c.Email, c.Token) 97 98 if resp, err = http.DefaultClient.Do(req); err != nil { 99 return 100 } 101 102 if resp.StatusCode != http.StatusOK { 103 var body []byte 104 if body, err = ioutil.ReadAll(resp.Body); err != nil { 105 body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err)) 106 } 107 err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body)) 108 } 109 return 110 }