2022-05-25 22:48:59 +08:00
|
|
|
//go:generate ../../../tools/readme_config_includer/generator
|
2018-05-15 08:15:40 +08:00
|
|
|
package http
|
|
|
|
|
|
|
|
|
|
import (
|
2021-09-15 05:04:34 +08:00
|
|
|
"bufio"
|
2018-05-15 08:15:40 +08:00
|
|
|
"bytes"
|
2018-09-07 01:54:05 +08:00
|
|
|
"context"
|
2021-12-11 04:06:33 +08:00
|
|
|
"crypto/sha256"
|
2022-05-25 22:48:59 +08:00
|
|
|
_ "embed"
|
2018-05-15 08:15:40 +08:00
|
|
|
"fmt"
|
2018-10-06 06:06:41 +08:00
|
|
|
"io"
|
2018-05-15 08:15:40 +08:00
|
|
|
"net/http"
|
|
|
|
|
"strings"
|
2021-12-11 04:06:33 +08:00
|
|
|
"time"
|
2018-05-15 08:11:44 +08:00
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
awsV2 "github.com/aws/aws-sdk-go-v2/aws"
|
2022-04-08 05:55:03 +08:00
|
|
|
v4 "github.com/aws/aws-sdk-go-v2/aws/signer/v4"
|
2022-05-25 22:48:59 +08:00
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
2023-01-26 04:02:29 +08:00
|
|
|
"github.com/influxdata/telegraf/config"
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2022-08-17 01:04:30 +08:00
|
|
|
internalaws "github.com/influxdata/telegraf/plugins/common/aws"
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig "github.com/influxdata/telegraf/plugins/common/http"
|
2018-05-15 08:11:44 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/outputs"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
2022-05-25 07:33:02 +08:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
"google.golang.org/api/idtoken"
|
2018-05-15 08:15:40 +08:00
|
|
|
)
|
|
|
|
|
|
2022-05-25 22:48:59 +08:00
|
|
|
//go:embed sample.conf
|
|
|
|
|
var sampleConfig string
|
|
|
|
|
|
2019-06-15 02:29:58 +08:00
|
|
|
const (
|
2021-09-15 05:04:34 +08:00
|
|
|
maxErrMsgLen = 1024
|
|
|
|
|
defaultURL = "http://127.0.0.1:8080/telegraf"
|
2019-06-15 02:29:58 +08:00
|
|
|
)
|
|
|
|
|
|
2018-05-15 08:15:40 +08:00
|
|
|
const (
|
2021-10-29 22:05:28 +08:00
|
|
|
defaultContentType = "text/plain; charset=utf-8"
|
|
|
|
|
defaultMethod = http.MethodPost
|
|
|
|
|
defaultUseBatchFormat = true
|
2018-05-15 08:15:40 +08:00
|
|
|
)
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
type HTTP struct {
|
2021-12-21 01:16:23 +08:00
|
|
|
URL string `toml:"url"`
|
|
|
|
|
Method string `toml:"method"`
|
2023-01-26 04:02:29 +08:00
|
|
|
Username config.Secret `toml:"username"`
|
|
|
|
|
Password config.Secret `toml:"password"`
|
2021-12-21 01:16:23 +08:00
|
|
|
Headers map[string]string `toml:"headers"`
|
|
|
|
|
ContentEncoding string `toml:"content_encoding"`
|
|
|
|
|
UseBatchFormat bool `toml:"use_batch_format"`
|
|
|
|
|
AwsService string `toml:"aws_service"`
|
|
|
|
|
NonRetryableStatusCodes []int `toml:"non_retryable_statuscodes"`
|
2021-04-23 21:37:27 +08:00
|
|
|
httpconfig.HTTPClientConfig
|
2021-07-14 05:58:49 +08:00
|
|
|
Log telegraf.Logger `toml:"-"`
|
2018-05-15 08:15:40 +08:00
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
client *http.Client
|
2018-05-15 08:15:40 +08:00
|
|
|
serializer serializers.Serializer
|
2021-12-11 04:06:33 +08:00
|
|
|
|
|
|
|
|
awsCfg *awsV2.Config
|
|
|
|
|
internalaws.CredentialConfig
|
2022-05-25 07:33:02 +08:00
|
|
|
|
|
|
|
|
// Google API Auth
|
|
|
|
|
CredentialsFile string `toml:"google_application_credentials"`
|
|
|
|
|
oauth2Token *oauth2.Token
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 22:48:59 +08:00
|
|
|
func (*HTTP) SampleConfig() string {
|
|
|
|
|
return sampleConfig
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) SetSerializer(serializer serializers.Serializer) {
|
2018-05-15 08:15:40 +08:00
|
|
|
h.serializer = serializer
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Connect() error {
|
2021-12-11 04:06:33 +08:00
|
|
|
if h.AwsService != "" {
|
|
|
|
|
cfg, err := h.CredentialConfig.Credentials()
|
|
|
|
|
if err == nil {
|
|
|
|
|
h.awsCfg = &cfg
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
if h.Method == "" {
|
|
|
|
|
h.Method = http.MethodPost
|
|
|
|
|
}
|
|
|
|
|
h.Method = strings.ToUpper(h.Method)
|
|
|
|
|
if h.Method != http.MethodPost && h.Method != http.MethodPut {
|
|
|
|
|
return fmt.Errorf("invalid method [%s] %s", h.URL, h.Method)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-07 01:54:05 +08:00
|
|
|
ctx := context.Background()
|
2021-07-14 05:58:49 +08:00
|
|
|
client, err := h.HTTPClientConfig.CreateClient(ctx, h.Log)
|
2018-05-15 08:11:44 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2018-09-07 01:54:05 +08:00
|
|
|
h.client = client
|
2018-05-15 08:15:40 +08:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Close() error {
|
2018-05-15 08:15:40 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
func (h *HTTP) Write(metrics []telegraf.Metric) error {
|
2021-10-29 22:05:28 +08:00
|
|
|
var reqBody []byte
|
|
|
|
|
|
|
|
|
|
if h.UseBatchFormat {
|
|
|
|
|
var err error
|
|
|
|
|
reqBody, err = h.serializer.SerializeBatch(metrics)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
return h.writeMetric(reqBody)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-29 22:05:28 +08:00
|
|
|
for _, metric := range metrics {
|
|
|
|
|
var err error
|
|
|
|
|
reqBody, err = h.serializer.Serialize(metric)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
if err := h.writeMetric(reqBody); err != nil {
|
2021-10-29 22:05:28 +08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
|
2021-11-25 03:40:25 +08:00
|
|
|
func (h *HTTP) writeMetric(reqBody []byte) error {
|
2018-10-06 06:06:41 +08:00
|
|
|
var reqBodyBuffer io.Reader = bytes.NewBuffer(reqBody)
|
|
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
if h.ContentEncoding == "gzip" {
|
2023-02-02 02:20:11 +08:00
|
|
|
rc := internal.CompressWithGzip(reqBodyBuffer)
|
2019-11-14 04:56:01 +08:00
|
|
|
defer rc.Close()
|
|
|
|
|
reqBodyBuffer = rc
|
2018-10-06 06:06:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
var payloadHash *string
|
|
|
|
|
if h.awsCfg != nil {
|
|
|
|
|
// We need a local copy of the full buffer, the signature scheme requires a sha256 of the request body.
|
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
|
_, err = io.Copy(buf, reqBodyBuffer)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sum := sha256.Sum256(buf.Bytes())
|
|
|
|
|
reqBodyBuffer = buf
|
|
|
|
|
|
|
|
|
|
// sha256 is hex encoded
|
|
|
|
|
hash := fmt.Sprintf("%x", sum)
|
|
|
|
|
payloadHash = &hash
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-06 06:06:41 +08:00
|
|
|
req, err := http.NewRequest(h.Method, h.URL, reqBodyBuffer)
|
2018-07-18 05:54:10 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
|
2021-12-11 04:06:33 +08:00
|
|
|
if h.awsCfg != nil {
|
|
|
|
|
signer := v4.NewSigner()
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
|
|
credentials, err := h.awsCfg.Credentials.Retrieve(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = signer.SignHTTP(ctx, credentials, req, *payloadHash, h.AwsService, h.Region, time.Now().UTC())
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 04:02:29 +08:00
|
|
|
if !h.Username.Empty() || !h.Password.Empty() {
|
|
|
|
|
username, err := h.Username.Get()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("getting username failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer config.ReleaseSecret(username)
|
|
|
|
|
password, err := h.Password.Get()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("getting password failed: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer config.ReleaseSecret(password)
|
|
|
|
|
|
|
|
|
|
req.SetBasicAuth(string(username), string(password))
|
2018-08-30 03:28:29 +08:00
|
|
|
}
|
|
|
|
|
|
2022-05-25 07:33:02 +08:00
|
|
|
// google api auth
|
|
|
|
|
if h.CredentialsFile != "" {
|
|
|
|
|
token, err := h.getAccessToken(context.Background(), h.URL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
token.SetAuthHeader(req)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-22 01:02:18 +08:00
|
|
|
req.Header.Set("User-Agent", internal.ProductToken())
|
2018-05-15 08:11:44 +08:00
|
|
|
req.Header.Set("Content-Type", defaultContentType)
|
2018-10-06 06:06:41 +08:00
|
|
|
if h.ContentEncoding == "gzip" {
|
|
|
|
|
req.Header.Set("Content-Encoding", "gzip")
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
for k, v := range h.Headers {
|
2019-05-07 03:13:51 +08:00
|
|
|
if strings.ToLower(k) == "host" {
|
|
|
|
|
req.Host = v
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
req.Header.Set(k, v)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp, err := h.client.Do(req)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
2018-05-15 08:11:44 +08:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
2021-12-21 01:16:23 +08:00
|
|
|
for _, nonRetryableStatusCode := range h.NonRetryableStatusCodes {
|
|
|
|
|
if resp.StatusCode == nonRetryableStatusCode {
|
|
|
|
|
h.Log.Errorf("Received non-retryable status %v. Metrics are lost.", resp.StatusCode)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 05:04:34 +08:00
|
|
|
errorLine := ""
|
|
|
|
|
scanner := bufio.NewScanner(io.LimitReader(resp.Body, maxErrMsgLen))
|
|
|
|
|
if scanner.Scan() {
|
|
|
|
|
errorLine = scanner.Text()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fmt.Errorf("when writing to [%s] received status code: %d. body: %s", h.URL, resp.StatusCode, errorLine)
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
2021-09-15 05:04:34 +08:00
|
|
|
|
2021-09-29 05:16:32 +08:00
|
|
|
_, err = io.ReadAll(resp.Body)
|
2021-03-04 03:56:31 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("when writing to [%s] received error: %v", h.URL, err)
|
|
|
|
|
}
|
2018-05-15 08:15:40 +08:00
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
outputs.Add("http", func() telegraf.Output {
|
2018-05-15 08:11:44 +08:00
|
|
|
return &HTTP{
|
2021-10-29 22:05:28 +08:00
|
|
|
Method: defaultMethod,
|
|
|
|
|
URL: defaultURL,
|
|
|
|
|
UseBatchFormat: defaultUseBatchFormat,
|
2018-05-15 08:15:40 +08:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-05-25 07:33:02 +08:00
|
|
|
|
|
|
|
|
func (h *HTTP) getAccessToken(ctx context.Context, audience string) (*oauth2.Token, error) {
|
|
|
|
|
if h.oauth2Token.Valid() {
|
|
|
|
|
return h.oauth2Token, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ts, err := idtoken.NewTokenSource(ctx, audience, idtoken.WithCredentialsFile(h.CredentialsFile))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error creating oauth2 token source: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
token, err := ts.Token()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("error fetching oauth2 token: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.oauth2Token = token
|
|
|
|
|
|
|
|
|
|
return token, nil
|
|
|
|
|
}
|