2015-08-12 20:15:34 +00:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-10-26 18:31:21 +00:00
|
|
|
"log"
|
2015-08-12 20:15:34 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"sort"
|
2015-10-23 13:07:48 +00:00
|
|
|
"strings"
|
2016-01-29 07:22:07 +00:00
|
|
|
"time"
|
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
|
|
|
|
|
|
|
apiUrl string
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:59:12 +00:00
|
|
|
var sampleConfig = `
|
2015-10-15 21:53:29 +00:00
|
|
|
# Datadog API key
|
|
|
|
apikey = "my-secret-key" # required.
|
2015-08-25 23:59:12 +00:00
|
|
|
|
2015-10-15 21:53:29 +00:00
|
|
|
# Connection timeout.
|
|
|
|
# timeout = "5s"
|
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 {
|
2016-01-29 07:22:07 +00:00
|
|
|
Metric string `json:"metric"`
|
|
|
|
Points [1]Point `json:"points"`
|
|
|
|
Host string `json:"host"`
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
Interval float64 `json:"interval,omitempty"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
DeviceName string `json:"device_name"`
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Point [2]float64
|
|
|
|
|
|
|
|
const datadog_api = "https://app.datadoghq.com/api/v1/series"
|
|
|
|
|
|
|
|
func NewDatadog(apiUrl string) *Datadog {
|
|
|
|
return &Datadog{
|
|
|
|
apiUrl: apiUrl,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Datadog) Connect() error {
|
|
|
|
if d.Apikey == "" {
|
|
|
|
return fmt.Errorf("apikey is a required field for datadog output")
|
|
|
|
}
|
|
|
|
d.client = &http.Client{
|
|
|
|
Timeout: d.Timeout.Duration,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (d *Datadog) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
2015-08-12 20:15:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-10-26 18:31:21 +00:00
|
|
|
ts := TimeSeries{}
|
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 {
|
2016-01-29 07:22:07 +00:00
|
|
|
ts.Series = append(ts.Series, dogMs...)
|
2015-10-26 18:31:21 +00:00
|
|
|
} else {
|
2016-01-27 23:15:14 +00:00
|
|
|
log.Printf("unable to build Metric for %s, skipping\n", m.Name())
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 21:08:31 +00:00
|
|
|
|
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 {
|
|
|
|
return fmt.Errorf("unable to create http.Request, %s\n", err.Error())
|
|
|
|
}
|
2016-01-29 07:22:07 +00:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
// If we don't pretend to be dogstatsd, it ignores our carefully
|
|
|
|
// calculated type.
|
|
|
|
req.Header.Set("DD-Dogstatsd-Version", "5.6.3")
|
|
|
|
req.Header.Set("User-Agent", "python-requests/2.6.0 CPython/2.7.10")
|
2015-08-12 20:15:34 +00:00
|
|
|
|
|
|
|
resp, err := d.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error POSTing metrics, %s\n", err.Error())
|
|
|
|
}
|
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},
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s?%s", d.apiUrl, q.Encode())
|
|
|
|
}
|
|
|
|
|
2016-01-29 07:22:07 +00:00
|
|
|
// Convert a telegraf metric to datadog metrics;
|
|
|
|
// we need a separate metric for each field.
|
|
|
|
// (also has magic for statsd field names)
|
|
|
|
func buildMetrics(m telegraf.Metric) ([]*Metric, error) {
|
|
|
|
var datadogMetrics []*Metric
|
|
|
|
tags := m.Tags()
|
|
|
|
metricType := tags["metric_type"]
|
|
|
|
baseDatadogMetric := Metric{
|
|
|
|
Metric: m.Name(),
|
|
|
|
Tags: buildTags(tags),
|
|
|
|
Host: tags["host"],
|
|
|
|
Interval: float64(m.Interval()) / float64(time.Second),
|
|
|
|
}
|
|
|
|
|
|
|
|
for field, value := range m.Fields() {
|
|
|
|
metric := baseDatadogMetric
|
|
|
|
if field != "value" {
|
|
|
|
metric.Metric += "_" + field
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2016-01-29 07:22:07 +00:00
|
|
|
metric.Metric = strings.Replace(metric.Metric, "_", ".", -1)
|
|
|
|
p := &metric.Points[0]
|
2016-01-27 23:15:14 +00:00
|
|
|
p[0] = float64(m.Time().Unix())
|
2016-01-29 07:22:07 +00:00
|
|
|
if err := p.setValue(value); err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to extract value from Fields, %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if metricType == "counter" ||
|
|
|
|
((metricType == "histogram" || metricType == "timing") && field == "count") {
|
|
|
|
metric.Type = "rate"
|
|
|
|
p[1] /= baseDatadogMetric.Interval
|
|
|
|
} else {
|
|
|
|
metric.Type = "gauge"
|
|
|
|
}
|
|
|
|
|
|
|
|
datadogMetrics = append(datadogMetrics, &metric)
|
2015-08-12 20:15:34 +00:00
|
|
|
}
|
2016-01-29 07:22:07 +00:00
|
|
|
|
|
|
|
return datadogMetrics, nil
|
2015-10-16 22:13:32 +00:00
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func buildTags(mTags map[string]string) []string {
|
|
|
|
tags := make([]string, len(mTags))
|
2015-10-16 22:13:32 +00:00
|
|
|
index := 0
|
2016-01-27 23:15:14 +00:00
|
|
|
for k, v := range mTags {
|
2015-08-12 20:15:34 +00:00
|
|
|
tags[index] = fmt.Sprintf("%s:%s", k, v)
|
|
|
|
index += 1
|
|
|
|
}
|
|
|
|
sort.Strings(tags)
|
|
|
|
return tags
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Point) setValue(v interface{}) error {
|
|
|
|
switch d := v.(type) {
|
|
|
|
case int:
|
|
|
|
p[1] = float64(int(d))
|
|
|
|
case int32:
|
|
|
|
p[1] = float64(int32(d))
|
|
|
|
case int64:
|
|
|
|
p[1] = float64(int64(d))
|
|
|
|
case float32:
|
|
|
|
p[1] = float64(d)
|
|
|
|
case float64:
|
|
|
|
p[1] = float64(d)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("undeterminable type")
|
|
|
|
}
|
|
|
|
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 {
|
2015-08-12 20:15:34 +00:00
|
|
|
return NewDatadog(datadog_api)
|
|
|
|
})
|
|
|
|
}
|