gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/librato/client.go (about) 1 // Copyright 2018 The aquachain Authors 2 // This file is part of the aquachain library. 3 // 4 // The aquachain library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The aquachain library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>. 16 17 package librato 18 19 import ( 20 "bytes" 21 "encoding/json" 22 "fmt" 23 "io/ioutil" 24 "net/http" 25 ) 26 27 const Operations = "operations" 28 const OperationsShort = "ops" 29 30 type LibratoClient struct { 31 Email, Token string 32 } 33 34 // property strings 35 const ( 36 // display attributes 37 Color = "color" 38 DisplayMax = "display_max" 39 DisplayMin = "display_min" 40 DisplayUnitsLong = "display_units_long" 41 DisplayUnitsShort = "display_units_short" 42 DisplayStacked = "display_stacked" 43 DisplayTransform = "display_transform" 44 // special gauge display attributes 45 SummarizeFunction = "summarize_function" 46 Aggregate = "aggregate" 47 48 // metric keys 49 Name = "name" 50 Period = "period" 51 Description = "description" 52 DisplayName = "display_name" 53 Attributes = "attributes" 54 55 // measurement keys 56 MeasureTime = "measure_time" 57 Source = "source" 58 Value = "value" 59 60 // special gauge keys 61 Count = "count" 62 Sum = "sum" 63 Max = "max" 64 Min = "min" 65 SumSquares = "sum_squares" 66 67 // batch keys 68 Counters = "counters" 69 Gauges = "gauges" 70 71 MetricsPostUrl = "https://metrics-api.librato.com/v1/metrics" 72 ) 73 74 type Measurement map[string]interface{} 75 type Metric map[string]interface{} 76 77 type Batch struct { 78 Gauges []Measurement `json:"gauges,omitempty"` 79 Counters []Measurement `json:"counters,omitempty"` 80 MeasureTime int64 `json:"measure_time"` 81 Source string `json:"source"` 82 } 83 84 func (self *LibratoClient) PostMetrics(batch Batch) (err error) { 85 var ( 86 js []byte 87 req *http.Request 88 resp *http.Response 89 ) 90 91 if len(batch.Counters) == 0 && len(batch.Gauges) == 0 { 92 return nil 93 } 94 95 if js, err = json.Marshal(batch); err != nil { 96 return 97 } 98 99 if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil { 100 return 101 } 102 103 req.Header.Set("Content-Type", "application/json") 104 req.SetBasicAuth(self.Email, self.Token) 105 106 if resp, err = http.DefaultClient.Do(req); err != nil { 107 return 108 } 109 110 if resp.StatusCode != http.StatusOK { 111 var body []byte 112 if body, err = ioutil.ReadAll(resp.Body); err != nil { 113 body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err)) 114 } 115 err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body)) 116 } 117 return 118 }