Add HTTP proxy setting to New Relic output plugin (#8749)

This commit is contained in:
Mike Summers 2021-02-01 11:54:42 -06:00 committed by GitHub
parent 13520ba6e5
commit c43de16bce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 7 deletions

View File

@ -17,6 +17,10 @@ Telegraf minimum version: Telegraf 1.15.0
## Timeout for writes to the New Relic API. ## Timeout for writes to the New Relic API.
# timeout = "15s" # timeout = "15s"
## HTTP Proxy override. If unset use values from the standard
## proxy environment variables to determine proxy, if any.
# http_proxy = "http://corporate.proxy:3128"
``` ```
[Metrics API]: https://docs.newrelic.com/docs/data-ingest-apis/get-data-new-relic/metric-api/introduction-metric-api [Metrics API]: https://docs.newrelic.com/docs/data-ingest-apis/get-data-new-relic/metric-api/introduction-metric-api

View File

@ -5,6 +5,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"net/url"
"time" "time"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
@ -19,12 +20,13 @@ type NewRelic struct {
InsightsKey string `toml:"insights_key"` InsightsKey string `toml:"insights_key"`
MetricPrefix string `toml:"metric_prefix"` MetricPrefix string `toml:"metric_prefix"`
Timeout internal.Duration `toml:"timeout"` Timeout internal.Duration `toml:"timeout"`
HttpProxy string `toml:"http_proxy"`
harvestor *telemetry.Harvester harvestor *telemetry.Harvester
dc *cumulative.DeltaCalculator dc *cumulative.DeltaCalculator
savedErrors map[int]interface{} savedErrors map[int]interface{}
errorCount int errorCount int
Client http.Client `toml:"-"` client http.Client `toml:"-"`
} }
// Description returns a one-sentence description on the Output // Description returns a one-sentence description on the Output
@ -43,6 +45,10 @@ func (nr *NewRelic) SampleConfig() string {
## Timeout for writes to the New Relic API. ## Timeout for writes to the New Relic API.
# timeout = "15s" # timeout = "15s"
## HTTP Proxy override. If unset use values from the standard
## proxy environment variables to determine proxy, if any.
# http_proxy = "http://corporate.proxy:3128"
` `
} }
@ -51,14 +57,18 @@ func (nr *NewRelic) Connect() error {
if nr.InsightsKey == "" { if nr.InsightsKey == "" {
return fmt.Errorf("InsightKey is a required for newrelic") return fmt.Errorf("InsightKey is a required for newrelic")
} }
var err error err := nr.initClient()
if err != nil {
return err
}
nr.harvestor, err = telemetry.NewHarvester(telemetry.ConfigAPIKey(nr.InsightsKey), nr.harvestor, err = telemetry.NewHarvester(telemetry.ConfigAPIKey(nr.InsightsKey),
telemetry.ConfigHarvestPeriod(0), telemetry.ConfigHarvestPeriod(0),
func(cfg *telemetry.Config) { func(cfg *telemetry.Config) {
cfg.Product = "NewRelic-Telegraf-Plugin" cfg.Product = "NewRelic-Telegraf-Plugin"
cfg.ProductVersion = "1.0" cfg.ProductVersion = "1.0"
cfg.HarvestTimeout = nr.Timeout.Duration cfg.HarvestTimeout = nr.Timeout.Duration
cfg.Client = &nr.Client cfg.Client = &nr.client
cfg.ErrorLogger = func(e map[string]interface{}) { cfg.ErrorLogger = func(e map[string]interface{}) {
var errorString string var errorString string
for k, v := range e { for k, v := range e {
@ -79,7 +89,7 @@ func (nr *NewRelic) Connect() error {
// Close any connections to the Output // Close any connections to the Output
func (nr *NewRelic) Close() error { func (nr *NewRelic) Close() error {
nr.errorCount = 0 nr.errorCount = 0
nr.Client.CloseIdleConnections() nr.client.CloseIdleConnections()
return nil return nil
} }
@ -108,7 +118,7 @@ func (nr *NewRelic) Write(metrics []telegraf.Metric) error {
case uint64: case uint64:
mvalue = float64(n) mvalue = float64(n)
case float64: case float64:
mvalue = float64(n) mvalue = n
case bool: case bool:
mvalue = float64(0) mvalue = float64(0)
if n { if n {
@ -119,7 +129,7 @@ func (nr *NewRelic) Write(metrics []telegraf.Metric) error {
// we just skip // we just skip
continue continue
default: default:
return fmt.Errorf("Undefined field type: %T", field.Value) return fmt.Errorf("undefined field type: %T", field.Value)
} }
switch metric.Type() { switch metric.Type() {
@ -152,7 +162,27 @@ func init() {
outputs.Add("newrelic", func() telegraf.Output { outputs.Add("newrelic", func() telegraf.Output {
return &NewRelic{ return &NewRelic{
Timeout: internal.Duration{Duration: time.Second * 15}, Timeout: internal.Duration{Duration: time.Second * 15},
Client: http.Client{},
} }
}) })
} }
func (nr *NewRelic) initClient() error {
if nr.HttpProxy == "" {
nr.client = http.Client{}
return nil
}
proxyURL, err := url.Parse(nr.HttpProxy)
if err != nil {
return err
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
nr.client = http.Client{
Transport: transport,
}
return nil
}

View File

@ -168,6 +168,14 @@ func TestNewRelic_Connect(t *testing.T) {
}, },
wantErr: false, wantErr: false,
}, },
{
name: "Test: HTTP Proxy",
newrelic: &NewRelic{
InsightsKey: "12121212",
HttpProxy: "https://my.proxy",
},
wantErr: false,
},
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {