2015-10-26 18:31:21 +00:00
|
|
|
package librato
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2016-02-26 20:06:56 +00:00
|
|
|
"io/ioutil"
|
2015-10-26 18:31:21 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2016-08-09 07:29:15 +00:00
|
|
|
"regexp"
|
2015-10-26 18:31:21 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2016-02-26 20:06:56 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers/graphite"
|
2015-10-26 18:31:21 +00:00
|
|
|
)
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// Librato structure for configuration and client
|
2015-10-26 18:31:21 +00:00
|
|
|
type Librato struct {
|
2017-01-09 14:48:32 +00:00
|
|
|
APIUser string `toml:"api_user"`
|
|
|
|
APIToken string `toml:"api_token"`
|
2016-08-09 07:29:15 +00:00
|
|
|
Debug bool
|
|
|
|
SourceTag string // Deprecated, keeping for backward-compatibility
|
|
|
|
Timeout internal.Duration
|
|
|
|
Template string
|
|
|
|
|
|
|
|
APIUrl string
|
2015-10-26 18:31:21 +00:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// https://www.librato.com/docs/kb/faq/best_practices/naming_convention_metrics_sources.html#naming-limitations-for-sources-and-metrics
|
|
|
|
var reUnacceptedChar = regexp.MustCompile("[^.a-zA-Z0-9_-]")
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Librator API Docs
|
|
|
|
## http://dev.librato.com/v1/metrics-authentication
|
|
|
|
## Librato API user
|
2015-10-26 18:31:21 +00:00
|
|
|
api_user = "telegraf@influxdb.com" # required.
|
2016-02-18 21:26:51 +00:00
|
|
|
## Librato API token
|
2015-10-26 18:31:21 +00:00
|
|
|
api_token = "my-secret-token" # required.
|
2016-04-08 22:04:45 +00:00
|
|
|
## Debug
|
2016-02-26 20:06:56 +00:00
|
|
|
# debug = false
|
2016-02-18 21:26:51 +00:00
|
|
|
## Connection timeout.
|
2015-10-26 18:31:21 +00:00
|
|
|
# timeout = "5s"
|
2016-08-09 07:29:15 +00:00
|
|
|
## Output source Template (same as graphite buckets)
|
2016-04-08 22:04:45 +00:00
|
|
|
## see https://github.com/influxdata/telegraf/blob/master/docs/DATA_FORMATS_OUTPUT.md#graphite
|
2016-08-09 07:29:15 +00:00
|
|
|
## This template is used in librato's source (not metric's name)
|
|
|
|
template = "host"
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
`
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// LMetrics is the default struct for Librato's API fromat
|
2016-01-27 23:15:14 +00:00
|
|
|
type LMetrics struct {
|
2015-10-26 18:31:21 +00:00
|
|
|
Gauges []*Gauge `json:"gauges"`
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// Gauge is the gauge format for Librato's API fromat
|
2015-10-26 18:31:21 +00:00
|
|
|
type Gauge struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value float64 `json:"value"`
|
|
|
|
Source string `json:"source"`
|
|
|
|
MeasureTime int64 `json:"measure_time"`
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
const libratoAPI = "https://metrics-api.librato.com/v1/metrics"
|
2015-10-26 18:31:21 +00:00
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// NewLibrato is the main constructor for librato output plugins
|
|
|
|
func NewLibrato(apiURL string) *Librato {
|
2015-10-26 18:31:21 +00:00
|
|
|
return &Librato{
|
2016-08-09 07:29:15 +00:00
|
|
|
APIUrl: apiURL,
|
|
|
|
Template: "host",
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// Connect is the default output plugin connection function who make sure it
|
|
|
|
// can connect to the endpoint
|
2015-10-26 18:31:21 +00:00
|
|
|
func (l *Librato) Connect() error {
|
2016-08-09 07:29:15 +00:00
|
|
|
if l.APIUser == "" || l.APIToken == "" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"api_user and api_token are required fields for librato output")
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
l.client = &http.Client{
|
2017-09-08 22:35:20 +00:00
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
},
|
2015-10-26 18:31:21 +00:00
|
|
|
Timeout: l.Timeout.Duration,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (l *Librato) Write(metrics []telegraf.Metric) error {
|
2016-08-09 07:29:15 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
if len(metrics) == 0 {
|
2015-10-26 18:31:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-08-09 07:29:15 +00:00
|
|
|
if l.Template == "" {
|
|
|
|
l.Template = "host"
|
|
|
|
}
|
|
|
|
if l.SourceTag != "" {
|
|
|
|
l.Template = l.SourceTag
|
|
|
|
}
|
|
|
|
|
2015-12-19 21:19:43 +00:00
|
|
|
tempGauges := []*Gauge{}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, m := range metrics {
|
|
|
|
if gauges, err := l.buildGauges(m); err == nil {
|
2015-12-19 21:19:43 +00:00
|
|
|
for _, gauge := range gauges {
|
|
|
|
tempGauges = append(tempGauges, gauge)
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Got a gauge: %v\n", gauge)
|
2015-12-19 21:19:43 +00:00
|
|
|
}
|
2015-10-26 18:31:21 +00:00
|
|
|
} else {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! unable to build Gauge for %s, skipping\n", m.Name())
|
|
|
|
log.Printf("D! Couldn't build gauge: %v\n", err)
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 21:19:43 +00:00
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
metricCounter := len(tempGauges)
|
|
|
|
// make sur we send a batch of maximum 300
|
|
|
|
sizeBatch := 300
|
|
|
|
for start := 0; start < metricCounter; start += sizeBatch {
|
|
|
|
lmetrics := LMetrics{}
|
|
|
|
end := start + sizeBatch
|
|
|
|
if end > metricCounter {
|
|
|
|
end = metricCounter
|
|
|
|
sizeBatch = end - start
|
|
|
|
}
|
|
|
|
lmetrics.Gauges = make([]*Gauge, sizeBatch)
|
|
|
|
copy(lmetrics.Gauges, tempGauges[start:end])
|
|
|
|
metricsBytes, err := json.Marshal(lmetrics)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal Metrics, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Librato request: %v\n", string(metricsBytes))
|
2015-10-26 18:31:21 +00:00
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
req, err := http.NewRequest(
|
|
|
|
"POST",
|
|
|
|
l.APIUrl,
|
|
|
|
bytes.NewBuffer(metricsBytes))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"unable to create http.Request, %s\n",
|
|
|
|
err.Error())
|
2016-02-26 20:06:56 +00:00
|
|
|
}
|
2016-08-09 07:29:15 +00:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.SetBasicAuth(l.APIUser, l.APIToken)
|
|
|
|
|
|
|
|
resp, err := l.client.Do(req)
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Error POSTing metrics: %v\n", err.Error())
|
2016-08-09 07:29:15 +00:00
|
|
|
return fmt.Errorf("error POSTing metrics, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 || l.Debug {
|
2016-02-26 20:06:56 +00:00
|
|
|
htmlData, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Couldn't get response! (%v)\n", err)
|
2016-08-09 07:29:15 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"received bad status code, %d\n %s",
|
|
|
|
resp.StatusCode,
|
|
|
|
string(htmlData))
|
|
|
|
}
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("D! Librato response: %v\n", string(htmlData))
|
2016-02-26 20:06:56 +00:00
|
|
|
}
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
2016-02-26 20:06:56 +00:00
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// SampleConfig is function who return the default configuration for this
|
|
|
|
// output
|
2015-10-26 18:31:21 +00:00
|
|
|
func (l *Librato) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
// Description is function who return the Description of this output
|
2015-10-26 18:31:21 +00:00
|
|
|
func (l *Librato) Description() string {
|
|
|
|
return "Configuration for Librato API to send metrics to."
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (l *Librato) buildGauges(m telegraf.Metric) ([]*Gauge, error) {
|
2016-08-09 07:29:15 +00:00
|
|
|
|
2015-12-19 21:19:43 +00:00
|
|
|
gauges := []*Gauge{}
|
2016-08-09 07:29:15 +00:00
|
|
|
if m.Time().Unix() == 0 {
|
2018-03-28 00:30:51 +00:00
|
|
|
return gauges, fmt.Errorf("time was zero %s", m.Name())
|
2016-08-09 07:29:15 +00:00
|
|
|
}
|
|
|
|
metricSource := graphite.InsertField(
|
|
|
|
graphite.SerializeBucketName("", m.Tags(), l.Template, ""),
|
|
|
|
"value")
|
|
|
|
if metricSource == "" {
|
|
|
|
return gauges,
|
|
|
|
fmt.Errorf("undeterminable Source type from Field, %s\n",
|
|
|
|
l.Template)
|
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
for fieldName, value := range m.Fields() {
|
2016-08-09 07:29:15 +00:00
|
|
|
|
|
|
|
metricName := m.Name()
|
|
|
|
if fieldName != "value" {
|
|
|
|
metricName = fmt.Sprintf("%s.%s", m.Name(), fieldName)
|
|
|
|
}
|
|
|
|
|
2015-12-19 21:19:43 +00:00
|
|
|
gauge := &Gauge{
|
2016-08-09 07:29:15 +00:00
|
|
|
Source: reUnacceptedChar.ReplaceAllString(metricSource, "-"),
|
|
|
|
Name: reUnacceptedChar.ReplaceAllString(metricName, "-"),
|
2016-01-27 23:15:14 +00:00
|
|
|
MeasureTime: m.Time().Unix(),
|
2015-12-19 21:19:43 +00:00
|
|
|
}
|
2016-08-09 07:29:15 +00:00
|
|
|
if !verifyValue(value) {
|
2016-03-22 15:07:01 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-19 21:19:43 +00:00
|
|
|
if err := gauge.setValue(value); err != nil {
|
2016-08-09 07:29:15 +00:00
|
|
|
return gauges, fmt.Errorf(
|
|
|
|
"unable to extract value from Fields, %s\n",
|
2015-12-19 21:19:43 +00:00
|
|
|
err.Error())
|
|
|
|
}
|
2016-02-26 20:06:56 +00:00
|
|
|
gauges = append(gauges, gauge)
|
|
|
|
}
|
2016-09-30 21:37:56 +00:00
|
|
|
|
|
|
|
log.Printf("D! Built gauges: %v\n", gauges)
|
2015-12-19 21:19:43 +00:00
|
|
|
return gauges, nil
|
2015-10-26 18:31:21 +00:00
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
func verifyValue(v interface{}) bool {
|
2016-03-22 15:07:01 +00:00
|
|
|
switch v.(type) {
|
|
|
|
case string:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-10-26 18:31:21 +00:00
|
|
|
func (g *Gauge) setValue(v interface{}) error {
|
|
|
|
switch d := v.(type) {
|
|
|
|
case int64:
|
|
|
|
g.Value = float64(int64(d))
|
2018-05-15 22:54:20 +00:00
|
|
|
case uint64:
|
2015-10-26 18:31:21 +00:00
|
|
|
g.Value = float64(d)
|
|
|
|
case float64:
|
|
|
|
g.Value = float64(d)
|
2018-05-15 22:54:20 +00:00
|
|
|
case bool:
|
|
|
|
if d {
|
|
|
|
g.Value = float64(1.0)
|
|
|
|
} else {
|
|
|
|
g.Value = float64(0.0)
|
|
|
|
}
|
2015-10-26 18:31:21 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("undeterminable type %+v", d)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-09 07:29:15 +00:00
|
|
|
//Close is used to close the connection to librato Output
|
2015-10-26 18:31:21 +00:00
|
|
|
func (l *Librato) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("librato", func() telegraf.Output {
|
2016-08-09 07:29:15 +00:00
|
|
|
return NewLibrato(libratoAPI)
|
2015-10-26 18:31:21 +00:00
|
|
|
})
|
|
|
|
}
|