2022-05-25 22:48:59 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2015-08-13 04:15:34 +08:00
|
|
|
package datadog
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2022-05-25 22:48:59 +08:00
|
|
|
_ "embed"
|
2015-08-13 04:15:34 +08:00
|
|
|
"encoding/json"
|
2024-02-09 01:32:30 +08:00
|
|
|
"errors"
|
2015-08-13 04:15:34 +08:00
|
|
|
"fmt"
|
2022-11-22 04:54:24 +08:00
|
|
|
"io"
|
2019-08-06 05:50:29 +08:00
|
|
|
"math"
|
2015-08-13 04:15:34 +08:00
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
2017-11-07 09:41:14 +08:00
|
|
|
"strings"
|
2021-04-10 01:15:04 +08:00
|
|
|
"time"
|
2015-08-13 04:15:34 +08:00
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2021-04-10 01:15:04 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2022-01-08 00:38:19 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2021-06-02 05:18:31 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/common/proxy"
|
2016-01-21 02:57:35 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
2015-08-13 04:15:34 +08:00
|
|
|
)
|
|
|
|
|
|
2022-05-25 22:48:59 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
type Datadog struct {
|
2024-08-07 22:58:25 +08:00
|
|
|
Apikey string `toml:"apikey"`
|
|
|
|
|
Timeout config.Duration `toml:"timeout"`
|
|
|
|
|
URL string `toml:"url"`
|
|
|
|
|
Compression string `toml:"compression"`
|
|
|
|
|
RateInterval config.Duration `toml:"rate_interval"`
|
|
|
|
|
Log telegraf.Logger `toml:"-"`
|
2015-08-13 04:15:34 +08:00
|
|
|
|
|
|
|
|
client *http.Client
|
2021-06-02 05:18:31 +08:00
|
|
|
proxy.HTTPProxy
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type TimeSeries struct {
|
|
|
|
|
Series []*Metric `json:"series"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Metric struct {
|
2022-04-28 06:04:34 +08:00
|
|
|
Metric string `json:"metric"`
|
|
|
|
|
Points [1]Point `json:"points"`
|
|
|
|
|
Host string `json:"host"`
|
|
|
|
|
Type string `json:"type,omitempty"`
|
|
|
|
|
Tags []string `json:"tags,omitempty"`
|
|
|
|
|
Interval int64 `json:"interval"`
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Point [2]float64
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
const datadogAPI = "https://app.datadoghq.com/api/v1/series"
|
2015-08-13 04:15:34 +08:00
|
|
|
|
2022-05-25 22:48:59 +08:00
|
|
|
func (*Datadog) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
func (d *Datadog) Connect() error {
|
|
|
|
|
if d.Apikey == "" {
|
2024-02-09 01:32:30 +08:00
|
|
|
return errors.New("apikey is a required field for datadog output")
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
2017-09-09 06:35:20 +08:00
|
|
|
|
2021-06-02 05:18:31 +08:00
|
|
|
proxyFunc, err := d.Proxy()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
d.client = &http.Client{
|
2017-09-09 06:35:20 +08:00
|
|
|
Transport: &http.Transport{
|
2021-06-02 05:18:31 +08:00
|
|
|
Proxy: proxyFunc,
|
2017-09-09 06:35:20 +08:00
|
|
|
},
|
2021-04-10 01:15:04 +08:00
|
|
|
Timeout: time.Duration(d.Timeout),
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-07 22:58:25 +08:00
|
|
|
func (d *Datadog) convertToDatadogMetric(metrics []telegraf.Metric) []*Metric {
|
2024-10-18 19:06:42 +08:00
|
|
|
tempSeries := make([]*Metric, 0, len(metrics))
|
2016-01-28 07:15:14 +08:00
|
|
|
for _, m := range metrics {
|
|
|
|
|
if dogMs, err := buildMetrics(m); err == nil {
|
2018-10-06 04:48:18 +08:00
|
|
|
metricTags := buildTags(m.TagList())
|
|
|
|
|
host, _ := m.GetTag("host")
|
2024-08-07 22:58:25 +08:00
|
|
|
// Retrieve the metric_type tag created by inputs.statsd
|
|
|
|
|
statsDMetricType, _ := m.GetTag("metric_type")
|
2018-10-06 04:48:18 +08:00
|
|
|
|
2019-08-06 05:50:29 +08:00
|
|
|
if len(dogMs) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
for fieldName, dogM := range dogMs {
|
2016-01-29 02:28:33 +08:00
|
|
|
// name of the datadog measurement
|
|
|
|
|
var dname string
|
|
|
|
|
if fieldName == "value" {
|
|
|
|
|
// adding .value seems redundant here
|
2016-04-20 12:50:22 +08:00
|
|
|
dname = m.Name()
|
2016-01-29 02:28:33 +08:00
|
|
|
} else {
|
2016-04-20 12:50:22 +08:00
|
|
|
dname = m.Name() + "." + fieldName
|
2016-01-29 02:28:33 +08:00
|
|
|
}
|
2022-04-28 06:04:34 +08:00
|
|
|
var tname string
|
2024-08-07 22:58:25 +08:00
|
|
|
var interval int64
|
|
|
|
|
interval = 1
|
2022-04-28 06:04:34 +08:00
|
|
|
switch m.Type() {
|
2024-08-07 22:58:25 +08:00
|
|
|
case telegraf.Counter, telegraf.Untyped:
|
|
|
|
|
if d.RateInterval > 0 && isRateable(statsDMetricType, fieldName) {
|
|
|
|
|
// interval is expected to be in seconds
|
|
|
|
|
rateIntervalSeconds := time.Duration(d.RateInterval).Seconds()
|
|
|
|
|
interval = int64(rateIntervalSeconds)
|
|
|
|
|
dogM[1] = dogM[1] / rateIntervalSeconds
|
|
|
|
|
tname = "rate"
|
|
|
|
|
} else if m.Type() == telegraf.Counter {
|
|
|
|
|
tname = "count"
|
|
|
|
|
} else {
|
|
|
|
|
tname = ""
|
|
|
|
|
}
|
2022-04-28 06:04:34 +08:00
|
|
|
case telegraf.Gauge:
|
|
|
|
|
tname = "gauge"
|
|
|
|
|
default:
|
|
|
|
|
tname = ""
|
|
|
|
|
}
|
2015-12-20 05:08:31 +08:00
|
|
|
metric := &Metric{
|
2022-04-28 06:04:34 +08:00
|
|
|
Metric: dname,
|
|
|
|
|
Tags: metricTags,
|
|
|
|
|
Host: host,
|
|
|
|
|
Type: tname,
|
2024-08-07 22:58:25 +08:00
|
|
|
Interval: interval,
|
2015-12-20 05:08:31 +08:00
|
|
|
}
|
2016-01-28 07:15:14 +08:00
|
|
|
metric.Points[0] = dogM
|
2015-12-20 05:19:43 +08:00
|
|
|
tempSeries = append(tempSeries, metric)
|
2015-12-20 05:08:31 +08:00
|
|
|
}
|
2015-10-27 02:31:21 +08:00
|
|
|
} else {
|
2021-02-09 00:18:40 +08:00
|
|
|
d.Log.Infof("Unable to build Metric for %s due to error '%v', skipping", m.Name(), err)
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-07 22:58:25 +08:00
|
|
|
return tempSeries
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *Datadog) Write(metrics []telegraf.Metric) error {
|
|
|
|
|
ts := TimeSeries{}
|
|
|
|
|
tempSeries := d.convertToDatadogMetric(metrics)
|
2015-12-20 05:08:31 +08:00
|
|
|
|
2019-08-06 05:50:29 +08:00
|
|
|
if len(tempSeries) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
redactedAPIKey := "****************"
|
2024-08-07 22:58:25 +08:00
|
|
|
ts.Series = make([]*Metric, len(tempSeries))
|
2015-10-27 02:31:21 +08:00
|
|
|
copy(ts.Series, tempSeries[0:])
|
2015-08-13 04:15:34 +08:00
|
|
|
tsBytes, err := json.Marshal(ts)
|
|
|
|
|
if err != nil {
|
2023-02-22 20:38:16 +08:00
|
|
|
return fmt.Errorf("unable to marshal TimeSeries: %w", err)
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
2022-01-08 00:38:19 +08:00
|
|
|
|
|
|
|
|
var req *http.Request
|
|
|
|
|
c := strings.ToLower(d.Compression)
|
|
|
|
|
switch c {
|
|
|
|
|
case "zlib":
|
|
|
|
|
encoder, err := internal.NewContentEncoder(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
buf, err := encoder.Encode(tsBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
req, err = http.NewRequest("POST", d.authenticatedURL(), bytes.NewBuffer(buf))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
req.Header.Set("Content-Encoding", "deflate")
|
|
|
|
|
case "none":
|
|
|
|
|
fallthrough
|
|
|
|
|
default:
|
|
|
|
|
req, err = http.NewRequest("POST", d.authenticatedURL(), bytes.NewBuffer(tsBytes))
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
if err != nil {
|
2022-05-11 23:53:34 +08:00
|
|
|
return fmt.Errorf("unable to create http.Request, %s", strings.ReplaceAll(err.Error(), d.Apikey, redactedAPIKey))
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
|
|
resp, err := d.client.Do(req)
|
|
|
|
|
if err != nil {
|
2022-05-11 23:53:34 +08:00
|
|
|
return fmt.Errorf("error POSTing metrics, %s", strings.ReplaceAll(err.Error(), d.Apikey, redactedAPIKey))
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
2015-10-23 21:07:48 +08:00
|
|
|
defer resp.Body.Close()
|
2015-08-13 04:15:34 +08:00
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 209 {
|
2024-07-10 18:51:25 +08:00
|
|
|
//nolint:errcheck // err can be ignored since it is just for logging
|
2022-11-22 04:54:24 +08:00
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
return fmt.Errorf("received bad status code, %d: %s", resp.StatusCode, string(body))
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-02 05:04:35 +08:00
|
|
|
func (d *Datadog) authenticatedURL() string {
|
2015-08-13 04:15:34 +08:00
|
|
|
q := url.Values{
|
|
|
|
|
"api_key": []string{d.Apikey},
|
|
|
|
|
}
|
2018-10-06 04:51:16 +08:00
|
|
|
return fmt.Sprintf("%s?%s", d.URL, q.Encode())
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
|
2016-01-28 07:15:14 +08:00
|
|
|
func buildMetrics(m telegraf.Metric) (map[string]Point, error) {
|
|
|
|
|
ms := make(map[string]Point)
|
2018-10-06 04:48:18 +08:00
|
|
|
for _, field := range m.FieldList() {
|
|
|
|
|
if !verifyValue(field.Value) {
|
2016-03-22 23:07:01 +08:00
|
|
|
continue
|
|
|
|
|
}
|
2015-12-20 05:08:31 +08:00
|
|
|
var p Point
|
2018-10-06 04:48:18 +08:00
|
|
|
if err := p.setValue(field.Value); err != nil {
|
2023-02-22 20:38:16 +08:00
|
|
|
return ms, fmt.Errorf("unable to extract value from Field %v: %w", field.Key, err)
|
2015-12-20 05:08:31 +08:00
|
|
|
}
|
2016-01-28 07:15:14 +08:00
|
|
|
p[0] = float64(m.Time().Unix())
|
2018-10-06 04:48:18 +08:00
|
|
|
ms[field.Key] = p
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
2016-01-28 07:15:14 +08:00
|
|
|
return ms, nil
|
2015-10-17 06:13:32 +08:00
|
|
|
}
|
|
|
|
|
|
2018-10-06 04:48:18 +08:00
|
|
|
func buildTags(tagList []*telegraf.Tag) []string {
|
2022-12-20 18:42:09 +08:00
|
|
|
tags := make([]string, 0, len(tagList))
|
2018-10-06 04:48:18 +08:00
|
|
|
for _, tag := range tagList {
|
2022-12-20 18:42:09 +08:00
|
|
|
tags = append(tags, fmt.Sprintf("%s:%s", tag.Key, tag.Value))
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
return tags
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-22 23:07:01 +08:00
|
|
|
func verifyValue(v interface{}) bool {
|
2019-08-06 05:50:29 +08:00
|
|
|
switch v := v.(type) {
|
2016-03-22 23:07:01 +08:00
|
|
|
case string:
|
|
|
|
|
return false
|
2019-08-06 05:50:29 +08: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 23:07:01 +08:00
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-12 03:57:14 +08:00
|
|
|
func isRateable(statsDMetricType, fieldName string) bool {
|
2024-08-07 22:58:25 +08:00
|
|
|
switch statsDMetricType {
|
|
|
|
|
case
|
|
|
|
|
"counter":
|
|
|
|
|
return true
|
|
|
|
|
case
|
|
|
|
|
"timing",
|
|
|
|
|
"histogram":
|
|
|
|
|
return fieldName == "count"
|
|
|
|
|
default:
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-13 04:15:34 +08:00
|
|
|
func (p *Point) setValue(v interface{}) error {
|
|
|
|
|
switch d := v.(type) {
|
|
|
|
|
case int64:
|
2018-05-02 09:56:39 +08:00
|
|
|
p[1] = float64(d)
|
|
|
|
|
case uint64:
|
2015-08-13 04:15:34 +08:00
|
|
|
p[1] = float64(d)
|
|
|
|
|
case float64:
|
2021-11-19 00:26:24 +08:00
|
|
|
p[1] = d
|
2018-02-21 09:32:18 +08:00
|
|
|
case bool:
|
|
|
|
|
p[1] = float64(0)
|
|
|
|
|
if d {
|
|
|
|
|
p[1] = float64(1)
|
|
|
|
|
}
|
2015-08-13 04:15:34 +08:00
|
|
|
default:
|
2018-05-02 09:56:39 +08:00
|
|
|
return fmt.Errorf("undeterminable field type: %T", v)
|
2015-08-13 04:15:34 +08:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-16 03:58:39 +08:00
|
|
|
func (*Datadog) Close() error {
|
2015-08-13 04:15:34 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2016-01-28 05:21:36 +08:00
|
|
|
outputs.Add("datadog", func() telegraf.Output {
|
2018-10-06 04:51:16 +08:00
|
|
|
return &Datadog{
|
2022-01-08 00:38:19 +08:00
|
|
|
URL: datadogAPI,
|
|
|
|
|
Compression: "none",
|
2018-10-06 04:51:16 +08:00
|
|
|
}
|
2015-08-13 04:15:34 +08:00
|
|
|
})
|
|
|
|
|
}
|