github.com/codingfuture/orig-energi3@v0.8.4/metrics/librato/client.go (about)

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