github.com/neatlab/neatio@v1.7.3-0.20220425043230-d903e92fcc75/utilities/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  const (
    19  	Color             = "color"
    20  	DisplayMax        = "display_max"
    21  	DisplayMin        = "display_min"
    22  	DisplayUnitsLong  = "display_units_long"
    23  	DisplayUnitsShort = "display_units_short"
    24  	DisplayStacked    = "display_stacked"
    25  	DisplayTransform  = "display_transform"
    26  	SummarizeFunction = "summarize_function"
    27  	Aggregate         = "aggregate"
    28  
    29  	Name        = "name"
    30  	Period      = "period"
    31  	Description = "description"
    32  	DisplayName = "display_name"
    33  	Attributes  = "attributes"
    34  
    35  	MeasureTime = "measure_time"
    36  	Source      = "source"
    37  	Value       = "value"
    38  
    39  	Count      = "count"
    40  	Sum        = "sum"
    41  	Max        = "max"
    42  	Min        = "min"
    43  	SumSquares = "sum_squares"
    44  
    45  	Counters = "counters"
    46  	Gauges   = "gauges"
    47  
    48  	MetricsPostUrl = "https://metrics-api.librato.com/v1/metrics"
    49  )
    50  
    51  type Measurement map[string]interface{}
    52  type Metric map[string]interface{}
    53  
    54  type Batch struct {
    55  	Gauges      []Measurement `json:"gauges,omitempty"`
    56  	Counters    []Measurement `json:"counters,omitempty"`
    57  	MeasureTime int64         `json:"measure_time"`
    58  	Source      string        `json:"source"`
    59  }
    60  
    61  func (self *LibratoClient) PostMetrics(batch Batch) (err error) {
    62  	var (
    63  		js   []byte
    64  		req  *http.Request
    65  		resp *http.Response
    66  	)
    67  
    68  	if len(batch.Counters) == 0 && len(batch.Gauges) == 0 {
    69  		return nil
    70  	}
    71  
    72  	if js, err = json.Marshal(batch); err != nil {
    73  		return
    74  	}
    75  
    76  	if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
    77  		return
    78  	}
    79  
    80  	req.Header.Set("Content-Type", "application/json")
    81  	req.SetBasicAuth(self.Email, self.Token)
    82  
    83  	if resp, err = http.DefaultClient.Do(req); err != nil {
    84  		return
    85  	}
    86  
    87  	if resp.StatusCode != http.StatusOK {
    88  		var body []byte
    89  		if body, err = ioutil.ReadAll(resp.Body); err != nil {
    90  			body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
    91  		}
    92  		err = fmt.Errorf("Unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
    93  	}
    94  	return
    95  }