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"` Period *uint `json:"period,omitempty"` DisplayName *string `json:"display_name,omitempty"` Attributes *MetricAttributes `json:"attributes,omitempty"` } 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"` DisplayUnitsShort string `json:"display_units_short"` DisplayStacked bool `json:"display_stacked"` DisplayTransform string `json:"display_transform"` } type ListMetricsOptions struct { *PaginationMeta Name string `url:"name,omitempty"` } // Advance to the specified page in result set, while retaining // the filtering options. func (l *ListMetricsOptions) AdvancePage(next *PaginationMeta) ListMetricsOptions { return ListMetricsOptions{ PaginationMeta: next, Name: l.Name, } } type ListMetricsResponse struct { ThisPage *PaginationResponseMeta NextPage *PaginationMeta } // List metrics using the provided options. // // Librato API docs: https://www.librato.com/docs/api/#retrieve-metrics 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 // // Librato API docs: https://www.librato.com/docs/api/#retrieve-metric-by-name 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 } type MeasurementSubmission struct { MeasureTime *uint `json:"measure_time,omitempty"` Source *string `json:"source,omitempty"` Gauges []*GaugeMeasurement `json:"gauges,omitempty"` Counters []*Measurement `json:"counters,omitempty"` } type Measurement struct { Name string `json:"name"` Value *float64 `json:"value,omitempty"` MeasureTime *uint `json:"measure_time,omitempty"` Source *string `json:"source,omitempty"` } 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"` } // Submit metrics // // Librato API docs: https://www.librato.com/docs/api/#submit-metrics func (m *MetricsService) Submit(measurements *MeasurementSubmission) (*http.Response, error) { req, err := m.client.NewRequest("POST", "/metrics", measurements) if err != nil { return nil, err } return m.client.Do(req, nil) } // Edit a metric. // // Librato API docs: https://www.librato.com/docs/api/#update-metric-by-name func (m *MetricsService) Edit(metric *Metric) (*http.Response, error) { 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. // // Librato API docs: https://www.librato.com/docs/api/#delete-metric-by-name 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) }