github.com/aquanetwork/aquachain@v1.7.8/common/metrics/librato/client.go (about) 1 package librato 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "fmt" 7 "io/ioutil" 8 "net/http" 9 ) 10 11 const Operations = "operations" 12 const OperationsShort = "ops" 13 14 type LibratoClient struct { 15 Email, Token string 16 } 17 18 // property strings 19 const ( 20 // display attributes 21 Color = "color" 22 DisplayMax = "display_max" 23 DisplayMin = "display_min" 24 DisplayUnitsLong = "display_units_long" 25 DisplayUnitsShort = "display_units_short" 26 DisplayStacked = "display_stacked" 27 DisplayTransform = "display_transform" 28 // special gauge display attributes 29 SummarizeFunction = "summarize_function" 30 Aggregate = "aggregate" 31 32 // metric keys 33 Name = "name" 34 Period = "period" 35 Description = "description" 36 DisplayName = "display_name" 37 Attributes = "attributes" 38 39 // measurement keys 40 MeasureTime = "measure_time" 41 Source = "source" 42 Value = "value" 43 44 // special gauge keys 45 Count = "count" 46 Sum = "sum" 47 Max = "max" 48 Min = "min" 49 SumSquares = "sum_squares" 50 51 // batch keys 52 Counters = "counters" 53 Gauges = "gauges" 54 55 MetricsPostUrl = "https://metrics-api.librato.com/v1/metrics" 56 ) 57 58 type Measurement map[string]interface{} 59 type Metric map[string]interface{} 60 61 type Batch struct { 62 Gauges []Measurement `json:"gauges,omitempty"` 63 Counters []Measurement `json:"counters,omitempty"` 64 MeasureTime int64 `json:"measure_time"` 65 Source string `json:"source"` 66 } 67 68 func (self *LibratoClient) PostMetrics(batch Batch) (err error) { 69 var ( 70 js []byte 71 req *http.Request 72 resp *http.Response 73 ) 74 75 if len(batch.Counters) == 0 && len(batch.Gauges) == 0 { 76 return nil 77 } 78 79 if js, err = json.Marshal(batch); err != nil { 80 return 81 } 82 83 if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil { 84 return 85 } 86 87 req.Header.Set("Content-Type", "application/json") 88 req.SetBasicAuth(self.Email, self.Token) 89 90 if resp, err = http.DefaultClient.Do(req); err != nil { 91 return 92 } 93 94 if resp.StatusCode != http.StatusOK { 95 var body []byte 96 if body, err = ioutil.ReadAll(resp.Body); err != nil { 97 body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err)) 98 } 99 err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body)) 100 } 101 return 102 }