terraform/vendor/github.com/henrikhodne/go-librato/librato/metrics.go

176 lines
5.0 KiB
Go
Raw Normal View History

2016-08-12 18:57:48 +02:00
package librato
import (
"fmt"
"net/http"
)
// MetricsService handles communication with the Librato API methods related to
// metrics.
type MetricsService struct {
client *Client
}
// Metric represents a Librato Metric.
type Metric struct {
Name *string `json:"name"`
2017-05-17 17:33:44 +02:00
Description *string `json:"description,omitempty"`
Type *string `json:"type"`
2016-08-12 18:57:48 +02:00
Period *uint `json:"period,omitempty"`
DisplayName *string `json:"display_name,omitempty"`
2017-05-17 17:33:44 +02:00
Composite *string `json:"composite,omitempty"`
2016-08-12 18:57:48 +02:00
Attributes *MetricAttributes `json:"attributes,omitempty"`
}
2017-05-17 17:33:44 +02:00
// MetricAttributes are named attributes as key:value pairs.
2016-08-12 18:57:48 +02:00
type MetricAttributes struct {
Color *string `json:"color"`
// These are interface{} because sometimes the Librato API
// returns strings, and sometimes it returns integers
DisplayMax interface{} `json:"display_max"`
DisplayMin interface{} `json:"display_min"`
2017-05-17 17:33:44 +02:00
DisplayUnitsLong string `json:"display_units_long"`
2016-08-12 18:57:48 +02:00
DisplayUnitsShort string `json:"display_units_short"`
DisplayStacked bool `json:"display_stacked"`
2017-05-17 17:33:44 +02:00
CreatedByUA string `json:"created_by_ua,omitempty"`
GapDetection bool `json:"gap_detection,omitempty"`
Aggregate bool `json:"aggregate,omitempty"`
2016-08-12 18:57:48 +02:00
}
2017-05-17 17:33:44 +02:00
// ListMetricsOptions are used to coordinate paging of metrics.
2016-08-12 18:57:48 +02:00
type ListMetricsOptions struct {
*PaginationMeta
Name string `url:"name,omitempty"`
}
2017-05-17 17:33:44 +02:00
// AdvancePage advances to the specified page in result set, while retaining
2016-08-12 18:57:48 +02:00
// the filtering options.
func (l *ListMetricsOptions) AdvancePage(next *PaginationMeta) ListMetricsOptions {
return ListMetricsOptions{
PaginationMeta: next,
Name: l.Name,
}
}
2017-05-17 17:33:44 +02:00
// ListMetricsResponse represents the response of a List call against the metrics service.
2016-08-12 18:57:48 +02:00
type ListMetricsResponse struct {
ThisPage *PaginationResponseMeta
NextPage *PaginationMeta
}
// List metrics using the provided options.
//
2017-05-17 17:33:44 +02:00
// Librato API docs: https://www.librato.com/docs/api/#list-a-subset-of-metrics
2016-08-12 18:57:48 +02:00
func (m *MetricsService) List(opts *ListMetricsOptions) ([]Metric, *ListMetricsResponse, error) {
u, err := urlWithOptions("metrics", opts)
if err != nil {
return nil, nil, err
}
req, err := m.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
var metricsResponse struct {
Query PaginationResponseMeta
Metrics []Metric
}
_, err = m.client.Do(req, &metricsResponse)
if err != nil {
return nil, nil, err
}
return metricsResponse.Metrics,
&ListMetricsResponse{
ThisPage: &metricsResponse.Query,
NextPage: metricsResponse.Query.nextPage(opts.PaginationMeta),
},
nil
}
// Get a metric by name
//
2017-05-17 17:33:44 +02:00
// Librato API docs: https://www.librato.com/docs/api/#retrieve-a-metric-by-name
2016-08-12 18:57:48 +02:00
func (m *MetricsService) Get(name string) (*Metric, *http.Response, error) {
u := fmt.Sprintf("metrics/%s", name)
req, err := m.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}
metric := new(Metric)
resp, err := m.client.Do(req, metric)
if err != nil {
return nil, resp, err
}
return metric, resp, err
}
2017-05-17 17:33:44 +02:00
// MeasurementSubmission represents the payload to submit/create a metric.
2016-08-12 18:57:48 +02:00
type MeasurementSubmission struct {
MeasureTime *uint `json:"measure_time,omitempty"`
Source *string `json:"source,omitempty"`
Gauges []*GaugeMeasurement `json:"gauges,omitempty"`
Counters []*Measurement `json:"counters,omitempty"`
}
2017-05-17 17:33:44 +02:00
// Measurement represents a Librato Measurement.
2016-08-12 18:57:48 +02:00
type Measurement struct {
Name string `json:"name"`
Value *float64 `json:"value,omitempty"`
MeasureTime *uint `json:"measure_time,omitempty"`
Source *string `json:"source,omitempty"`
}
2017-05-17 17:33:44 +02:00
// GaugeMeasurement represents a Librato measurement gauge.
2016-08-12 18:57:48 +02:00
type GaugeMeasurement struct {
*Measurement
Count *uint `json:"count,omitempty"`
Sum *float64 `json:"sum,omitempty"`
Max *float64 `json:"max,omitempty"`
Min *float64 `json:"min,omitempty"`
SumSquares *float64 `json:"sum_squares,omitempty"`
}
// Create metrics
2016-08-12 18:57:48 +02:00
//
2017-05-17 17:33:44 +02:00
// Librato API docs: https://www.librato.com/docs/api/#create-a-measurement
func (m *MetricsService) Create(measurements *MeasurementSubmission) (*http.Response, error) {
2016-08-12 18:57:48 +02:00
req, err := m.client.NewRequest("POST", "/metrics", measurements)
if err != nil {
return nil, err
}
return m.client.Do(req, nil)
}
// Update a metric.
2016-08-12 18:57:48 +02:00
//
2017-05-17 17:33:44 +02:00
// Librato API docs: https://www.librato.com/docs/api/#update-a-metric-by-name
func (m *MetricsService) Update(metric *Metric) (*http.Response, error) {
2016-08-12 18:57:48 +02:00
u := fmt.Sprintf("metrics/%s", *metric.Name)
req, err := m.client.NewRequest("PUT", u, metric)
if err != nil {
return nil, err
}
return m.client.Do(req, nil)
}
// Delete a metric.
//
2017-05-17 17:33:44 +02:00
// Librato API docs: https://www.librato.com/docs/api/#delete-a-metric-by-name
2016-08-12 18:57:48 +02:00
func (m *MetricsService) Delete(name string) (*http.Response, error) {
u := fmt.Sprintf("metrics/%s", name)
req, err := m.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return m.client.Do(req, nil)
}