2015-08-12 20:15:34 +00:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-10-26 18:31:21 +00:00
|
|
|
"log"
|
2019-08-05 21:50:29 +00:00
|
|
|
"math"
|
2015-08-12 20:15:34 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-11-07 01:41:14 +00:00
|
|
|
"strings"
|
2015-08-12 20:15:34 +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"
|
2015-08-12 20:15:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Datadog struct {
|
|
|
|
Apikey string
|
2015-11-10 21:40:39 +00:00
|
|
|
Timeout internal.Duration
|
2015-08-12 20:15:34 +00:00
|
|
|
|
2018-10-05 20:51:16 +00:00
|
|
|
URL string `toml:"url"`
|
2015-08-12 20:15:34 +00:00
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Datadog API key
|
2020-05-06 18:25:21 +00:00
|
|
|
apikey = "my-secret-key"
|
2018-10-05 20:51:16 +00:00
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Connection timeout.
|
2015-10-15 21:53:29 +00:00
|
|
|
# timeout = "5s"
|
2020-05-06 18:25:21 +00:00
|
|
|
|
|
|
|
## Write URL override; useful for debugging.
|
|
|
|
# url = "https://app.datadoghq.com/api/v1/series"
|
2015-08-25 23:59:12 +00:00
|
|
|
`
|
|
|
|
|
2015-08-12 20:15:34 +00:00
|
|
|
type TimeSeries struct {
|
|
|
|
Series []*Metric `json:"series"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metric struct {
|
|
|
|
Metric string `json:"metric"`
|
2016-01-28 23:12:33 +00:00
|
|
|
Points [1]Point `json:"points"`
|
2015-10-23 13:07:48 +00:00
|
|
|
Host string `json:"host"`
|
2016-01-28 23:12:33 +00:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Point [2]float64
|
|
|
|
|
|
|
|
const datadog_api = "https://app.datadoghq.com/api/v1/series"
|
|
|
|
|
|
|
|
func (d *Datadog) Connect() error {
|
|
|
|
if d.Apikey == "" {
|
|
|
|
return fmt.Errorf("apikey is a required field for datadog output")
|
|
|
|
}
|
2017-09-08 22:35:20 +00:00
|
|
|
|
2015-08-12 20:15:34 +00:00
|
|
|
d.client = &http.Client{
|
2017-09-08 22:35:20 +00:00
|
|
|
Transport: &http.Transport{
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
},
|
2015-08-12 20:15:34 +00:00
|
|
|
Timeout: d.Timeout.Duration,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (d *Datadog) Write(metrics []telegraf.Metric) error {
|
2015-10-26 18:31:21 +00:00
|
|
|
ts := TimeSeries{}
|
2015-12-19 21:19:43 +00:00
|
|
|
tempSeries := []*Metric{}
|
|
|
|
metricCounter := 0
|
2015-12-19 21:08:31 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, m := range metrics {
|
|
|
|
if dogMs, err := buildMetrics(m); err == nil {
|
2018-10-05 20:48:18 +00:00
|
|
|
metricTags := buildTags(m.TagList())
|
|
|
|
host, _ := m.GetTag("host")
|
|
|
|
|
2019-08-05 21:50:29 +00:00
|
|
|
if len(dogMs) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for fieldName, dogM := range dogMs {
|
2016-01-28 18:28:33 +00:00
|
|
|
// name of the datadog measurement
|
|
|
|
var dname string
|
|
|
|
if fieldName == "value" {
|
|
|
|
// adding .value seems redundant here
|
2016-04-20 04:50:22 +00:00
|
|
|
dname = m.Name()
|
2016-01-28 18:28:33 +00:00
|
|
|
} else {
|
2016-04-20 04:50:22 +00:00
|
|
|
dname = m.Name() + "." + fieldName
|
2016-01-28 18:28:33 +00:00
|
|
|
}
|
2015-12-19 21:08:31 +00:00
|
|
|
metric := &Metric{
|
2016-01-28 18:28:33 +00:00
|
|
|
Metric: dname,
|
2018-10-05 20:48:18 +00:00
|
|
|
Tags: metricTags,
|
2016-04-20 04:50:22 +00:00
|
|
|
Host: host,
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
metric.Points[0] = dogM
|
2015-12-19 21:19:43 +00:00
|
|
|
tempSeries = append(tempSeries, metric)
|
|
|
|
metricCounter++
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2015-10-26 18:31:21 +00:00
|
|
|
} else {
|
2018-02-21 01:32:18 +00:00
|
|
|
log.Printf("I! unable to build Metric for %s due to error '%v', skipping\n", m.Name(), err)
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 21:08:31 +00:00
|
|
|
|
2019-08-05 21:50:29 +00:00
|
|
|
if len(tempSeries) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-07 01:41:14 +00:00
|
|
|
redactedApiKey := "****************"
|
2015-12-19 21:19:43 +00:00
|
|
|
ts.Series = make([]*Metric, metricCounter)
|
2015-10-26 18:31:21 +00:00
|
|
|
copy(ts.Series, tempSeries[0:])
|
2015-08-12 20:15:34 +00:00
|
|
|
tsBytes, err := json.Marshal(ts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal TimeSeries, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", d.authenticatedUrl(), bytes.NewBuffer(tsBytes))
|
|
|
|
if err != nil {
|
2017-11-07 01:41:14 +00:00
|
|
|
return fmt.Errorf("unable to create http.Request, %s\n", strings.Replace(err.Error(), d.Apikey, redactedApiKey, -1))
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := d.client.Do(req)
|
|
|
|
if err != nil {
|
2017-11-07 01:41:14 +00:00
|
|
|
return fmt.Errorf("error POSTing metrics, %s\n", strings.Replace(err.Error(), d.Apikey, redactedApiKey, -1))
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
2015-10-23 13:07:48 +00:00
|
|
|
defer resp.Body.Close()
|
2015-08-12 20:15:34 +00:00
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 209 {
|
|
|
|
return fmt.Errorf("received bad status code, %d\n", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
func (d *Datadog) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Datadog) Description() string {
|
|
|
|
return "Configuration for DataDog API to send metrics to."
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:15:34 +00:00
|
|
|
func (d *Datadog) authenticatedUrl() string {
|
|
|
|
q := url.Values{
|
|
|
|
"api_key": []string{d.Apikey},
|
|
|
|
}
|
2018-10-05 20:51:16 +00:00
|
|
|
return fmt.Sprintf("%s?%s", d.URL, q.Encode())
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func buildMetrics(m telegraf.Metric) (map[string]Point, error) {
|
|
|
|
ms := make(map[string]Point)
|
2018-10-05 20:48:18 +00:00
|
|
|
for _, field := range m.FieldList() {
|
|
|
|
if !verifyValue(field.Value) {
|
2016-03-22 15:07:01 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-19 21:08:31 +00:00
|
|
|
var p Point
|
2018-10-05 20:48:18 +00:00
|
|
|
if err := p.setValue(field.Value); err != nil {
|
|
|
|
return ms, fmt.Errorf("unable to extract value from Fields %v error %v", field.Key, err.Error())
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
p[0] = float64(m.Time().Unix())
|
2018-10-05 20:48:18 +00:00
|
|
|
ms[field.Key] = p
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
return ms, nil
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2018-10-05 20:48:18 +00:00
|
|
|
func buildTags(tagList []*telegraf.Tag) []string {
|
|
|
|
tags := make([]string, len(tagList))
|
2015-10-16 22:13:32 +00:00
|
|
|
index := 0
|
2018-10-05 20:48:18 +00:00
|
|
|
for _, tag := range tagList {
|
|
|
|
tags[index] = fmt.Sprintf("%s:%s", tag.Key, tag.Value)
|
2015-08-12 20:15:34 +00:00
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
2016-03-22 15:07:01 +00:00
|
|
|
func verifyValue(v interface{}) bool {
|
2019-08-05 21:50:29 +00:00
|
|
|
switch v := v.(type) {
|
2016-03-22 15:07:01 +00:00
|
|
|
case string:
|
|
|
|
return false
|
2019-08-05 21:50:29 +00:00
|
|
|
case float64:
|
|
|
|
// The payload will be encoded as JSON, which does not allow NaN or Inf.
|
|
|
|
return !math.IsNaN(v) && !math.IsInf(v, 0)
|
2016-03-22 15:07:01 +00:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:15:34 +00:00
|
|
|
func (p *Point) setValue(v interface{}) error {
|
|
|
|
switch d := v.(type) {
|
|
|
|
case int64:
|
2018-05-02 01:56:39 +00:00
|
|
|
p[1] = float64(d)
|
|
|
|
case uint64:
|
2015-08-12 20:15:34 +00:00
|
|
|
p[1] = float64(d)
|
|
|
|
case float64:
|
|
|
|
p[1] = float64(d)
|
2018-02-21 01:32:18 +00:00
|
|
|
case bool:
|
|
|
|
p[1] = float64(0)
|
|
|
|
if d {
|
|
|
|
p[1] = float64(1)
|
|
|
|
}
|
2015-08-12 20:15:34 +00:00
|
|
|
default:
|
2018-05-02 01:56:39 +00:00
|
|
|
return fmt.Errorf("undeterminable field type: %T", v)
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Datadog) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("datadog", func() telegraf.Output {
|
2018-10-05 20:51:16 +00:00
|
|
|
return &Datadog{
|
|
|
|
URL: datadog_api,
|
|
|
|
}
|
2015-08-12 20:15:34 +00:00
|
|
|
})
|
|
|
|
}
|