github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/metrics/librato/client.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 12:09:42</date>
    10  //</624342649473601536>
    11  
    12  package librato
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"fmt"
    18  	"io/ioutil"
    19  	"net/http"
    20  )
    21  
    22  const Operations = "operations"
    23  const OperationsShort = "ops"
    24  
    25  type LibratoClient struct {
    26  	Email, Token string
    27  }
    28  
    29  //属性字符串
    30  const (
    31  //显示属性
    32  	Color             = "color"
    33  	DisplayMax        = "display_max"
    34  	DisplayMin        = "display_min"
    35  	DisplayUnitsLong  = "display_units_long"
    36  	DisplayUnitsShort = "display_units_short"
    37  	DisplayStacked    = "display_stacked"
    38  	DisplayTransform  = "display_transform"
    39  //特殊仪表显示属性
    40  	SummarizeFunction = "summarize_function"
    41  	Aggregate         = "aggregate"
    42  
    43  //公制键
    44  	Name        = "name"
    45  	Period      = "period"
    46  	Description = "description"
    47  	DisplayName = "display_name"
    48  	Attributes  = "attributes"
    49  
    50  //测量键
    51  	MeasureTime = "measure_time"
    52  	Source      = "source"
    53  	Value       = "value"
    54  
    55  //专用仪表键
    56  	Count      = "count"
    57  	Sum        = "sum"
    58  	Max        = "max"
    59  	Min        = "min"
    60  	SumSquares = "sum_squares"
    61  
    62  //批密钥
    63  	Counters = "counters"
    64  	Gauges   = "gauges"
    65  
    66  MetricsPostUrl = "https://度量API.libato.com/v1/metrics“
    67  )
    68  
    69  type Measurement map[string]interface{}
    70  type Metric map[string]interface{}
    71  
    72  type Batch struct {
    73  	Gauges      []Measurement `json:"gauges,omitempty"`
    74  	Counters    []Measurement `json:"counters,omitempty"`
    75  	MeasureTime int64         `json:"measure_time"`
    76  	Source      string        `json:"source"`
    77  }
    78  
    79  func (c *LibratoClient) PostMetrics(batch Batch) (err error) {
    80  	var (
    81  		js   []byte
    82  		req  *http.Request
    83  		resp *http.Response
    84  	)
    85  
    86  	if len(batch.Counters) == 0 && len(batch.Gauges) == 0 {
    87  		return nil
    88  	}
    89  
    90  	if js, err = json.Marshal(batch); err != nil {
    91  		return
    92  	}
    93  
    94  	if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
    95  		return
    96  	}
    97  
    98  	req.Header.Set("Content-Type", "application/json")
    99  	req.SetBasicAuth(c.Email, c.Token)
   100  
   101  	if resp, err = http.DefaultClient.Do(req); err != nil {
   102  		return
   103  	}
   104  
   105  	if resp.StatusCode != http.StatusOK {
   106  		var body []byte
   107  		if body, err = ioutil.ReadAll(resp.Body); err != nil {
   108  			body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
   109  		}
   110  		err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
   111  	}
   112  	return
   113  }
   114