mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-24 23:46:17 +00:00
The name of a method’s receiver should be a reflection of its identity; often a one or two letter abbreviation of its type suffices (such as “c” or “cl” for “Client”). Don’t use generic names such as “me”, “this” or “self”, identifiers typical of object-oriented languages that place more emphasis on methods as opposed to functions. The name need not be as descriptive as that of a method argument, as its role is obvious and serves no documentary purpose. It can be very short as it will appear on almost every line of every method of the type; familiarity admits brevity. Be consistent, too: if you call the receiver “c” in one method, don’t call it “cl” in another.
102 lines
2.2 KiB
Go
102 lines
2.2 KiB
Go
package librato
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const Operations = "operations"
|
|
const OperationsShort = "ops"
|
|
|
|
type LibratoClient struct {
|
|
Email, Token string
|
|
}
|
|
|
|
// property strings
|
|
const (
|
|
// display attributes
|
|
Color = "color"
|
|
DisplayMax = "display_max"
|
|
DisplayMin = "display_min"
|
|
DisplayUnitsLong = "display_units_long"
|
|
DisplayUnitsShort = "display_units_short"
|
|
DisplayStacked = "display_stacked"
|
|
DisplayTransform = "display_transform"
|
|
// special gauge display attributes
|
|
SummarizeFunction = "summarize_function"
|
|
Aggregate = "aggregate"
|
|
|
|
// metric keys
|
|
Name = "name"
|
|
Period = "period"
|
|
Description = "description"
|
|
DisplayName = "display_name"
|
|
Attributes = "attributes"
|
|
|
|
// measurement keys
|
|
MeasureTime = "measure_time"
|
|
Source = "source"
|
|
Value = "value"
|
|
|
|
// special gauge keys
|
|
Count = "count"
|
|
Sum = "sum"
|
|
Max = "max"
|
|
Min = "min"
|
|
SumSquares = "sum_squares"
|
|
|
|
// batch keys
|
|
Counters = "counters"
|
|
Gauges = "gauges"
|
|
|
|
MetricsPostUrl = "https://metrics-api.librato.com/v1/metrics"
|
|
)
|
|
|
|
type Measurement map[string]interface{}
|
|
type Metric map[string]interface{}
|
|
|
|
type Batch struct {
|
|
Gauges []Measurement `json:"gauges,omitempty"`
|
|
Counters []Measurement `json:"counters,omitempty"`
|
|
MeasureTime int64 `json:"measure_time"`
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
func (lc *LibratoClient) PostMetrics(batch Batch) (err error) {
|
|
var (
|
|
js []byte
|
|
req *http.Request
|
|
resp *http.Response
|
|
)
|
|
|
|
if len(batch.Counters) == 0 && len(batch.Gauges) == 0 {
|
|
return nil
|
|
}
|
|
|
|
if js, err = json.Marshal(batch); err != nil {
|
|
return
|
|
}
|
|
|
|
if req, err = http.NewRequest("POST", MetricsPostUrl, bytes.NewBuffer(js)); err != nil {
|
|
return
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.SetBasicAuth(lc.Email, lc.Token)
|
|
|
|
if resp, err = http.DefaultClient.Do(req); err != nil {
|
|
return
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
var body []byte
|
|
if body, err = io.ReadAll(resp.Body); err != nil {
|
|
body = []byte(fmt.Sprintf("(could not fetch response body for error: %s)", err))
|
|
}
|
|
err = fmt.Errorf("unable to post to Librato: %d %s %s", resp.StatusCode, resp.Status, string(body))
|
|
}
|
|
return
|
|
}
|