Skip to content

Commit

Permalink
fix: Linter fixes for plugins/outputs/[p-z]* (influxdata#10139)
Browse files Browse the repository at this point in the history
Co-authored-by: Pawel Zak <Pawel Zak>
  • Loading branch information
zak-pawel authored Nov 24, 2021
1 parent 64aee2c commit 64bc0ae
Show file tree
Hide file tree
Showing 23 changed files with 307 additions and 270 deletions.
23 changes: 17 additions & 6 deletions plugins/outputs/prometheus_client/prometheus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/internal"
tlsint "github.com/influxdata/telegraf/plugins/common/tls"
"github.com/influxdata/telegraf/plugins/outputs"
"github.com/influxdata/telegraf/plugins/outputs/prometheus_client/v1"
"github.com/influxdata/telegraf/plugins/outputs/prometheus_client/v2"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

var (
Expand Down Expand Up @@ -121,9 +123,15 @@ func (p *PrometheusClient) Init() error {
for collector := range defaultCollectors {
switch collector {
case "gocollector":
registry.Register(prometheus.NewGoCollector())
err := registry.Register(collectors.NewGoCollector())
if err != nil {
return err
}
case "process":
registry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
err := registry.Register(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
if err != nil {
return err
}
default:
return fmt.Errorf("unrecognized collector %s", collector)
}
Expand Down Expand Up @@ -160,7 +168,10 @@ func (p *PrometheusClient) Init() error {
rangeHandler := internal.IPRangeHandler(ipRange, onError)
promHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorHandling: promhttp.ContinueOnError})
landingPageHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Telegraf Output Plugin: Prometheus Client "))
_, err := w.Write([]byte("Telegraf Output Plugin: Prometheus Client "))
if err != nil {
p.Log.Errorf("Error occurred when writing HTTP reply: %v", err)
}
})

mux := http.NewServeMux()
Expand Down Expand Up @@ -229,7 +240,7 @@ func onError(rw http.ResponseWriter, code int) {
http.Error(rw, http.StatusText(code), code)
}

// Address returns the address the plugin is listening on. If not listening
// URL returns the address the plugin is listening on. If not listening
// an empty string is returned.
func (p *PrometheusClient) URL() string {
if p.url != nil {
Expand Down
38 changes: 22 additions & 16 deletions plugins/outputs/prometheus_client/prometheus_client_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
inputs "github.com/influxdata/telegraf/plugins/inputs/prometheus"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestMetricVersion1(t *testing.T) {
Logger := testutil.Logger{Name: "outputs.prometheus_client"}
logger := testutil.Logger{Name: "outputs.prometheus_client"}
tests := []struct {
name string
output *PrometheusClient
Expand All @@ -31,7 +32,7 @@ func TestMetricVersion1(t *testing.T) {
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -58,7 +59,7 @@ cpu_time_idle{host="example.org"} 42
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -85,7 +86,7 @@ cpu_time_idle{host="example.org"} 42
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -114,7 +115,7 @@ cpu_time_idle{host="example.org"} 42
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
StringAsLabel: true,
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -141,7 +142,7 @@ cpu_time_idle{host_name="example.org"} 42
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -169,7 +170,7 @@ cpu_time_idle{host="example.org"} 42
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -209,7 +210,7 @@ http_request_duration_seconds_count 144320
MetricVersion: 1,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -272,7 +273,7 @@ rpc_duration_seconds_count 2693
}

func TestRoundTripMetricVersion1(t *testing.T) {
Logger := testutil.Logger{Name: "outputs.prometheus_client"}
logger := testutil.Logger{Name: "outputs.prometheus_client"}
tests := []struct {
name string
data []byte
Expand Down Expand Up @@ -348,17 +349,18 @@ rpc_duration_seconds_count 2693
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()

url := fmt.Sprintf("http://%s", ts.Listener.Addr())
address := fmt.Sprintf("http://%s", ts.Listener.Addr())

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(tt.data)
_, err := w.Write(tt.data)
require.NoError(t, err)
})

input := &inputs.Prometheus{
URLs: []string{url},
URLs: []string{address},
URLTag: "",
MetricVersion: 1,
}
Expand All @@ -375,7 +377,7 @@ rpc_duration_seconds_count 2693
Listen: "127.0.0.1:0",
Path: defaultPath,
MetricVersion: 1,
Log: Logger,
Log: logger,
CollectorsExclude: []string{"gocollector", "process"},
}
err = output.Init()
Expand All @@ -391,6 +393,7 @@ rpc_duration_seconds_count 2693

resp, err := http.Get(output.URL())
require.NoError(t, err)
defer resp.Body.Close()

actual, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand All @@ -403,12 +406,12 @@ rpc_duration_seconds_count 2693
}

func TestLandingPage(t *testing.T) {
Logger := testutil.Logger{Name: "outputs.prometheus_client"}
logger := testutil.Logger{Name: "outputs.prometheus_client"}
output := PrometheusClient{
Listen: ":0",
CollectorsExclude: []string{"process"},
MetricVersion: 1,
Log: Logger,
Log: logger,
}
expected := "Telegraf Output Plugin: Prometheus Client"

Expand All @@ -419,8 +422,11 @@ func TestLandingPage(t *testing.T) {
require.NoError(t, err)

u, err := url.Parse(fmt.Sprintf("http://%s/", output.url.Host))
require.NoError(t, err)

resp, err := http.Get(u.String())
require.NoError(t, err)
defer resp.Body.Close()

actual, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down
29 changes: 16 additions & 13 deletions plugins/outputs/prometheus_client/prometheus_client_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/influxdata/telegraf"
inputs "github.com/influxdata/telegraf/plugins/inputs/prometheus"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)

func TestMetricVersion2(t *testing.T) {
Logger := testutil.Logger{Name: "outputs.prometheus_client"}
logger := testutil.Logger{Name: "outputs.prometheus_client"}
tests := []struct {
name string
output *PrometheusClient
Expand All @@ -30,7 +31,7 @@ func TestMetricVersion2(t *testing.T) {
MetricVersion: 2,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -57,7 +58,7 @@ cpu_time_idle{host="example.org"} 42
MetricVersion: 2,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -86,7 +87,7 @@ rpc_duration_seconds_count 2693
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
ExportTimestamp: true,
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -114,7 +115,7 @@ cpu_time_idle{host="example.org"} 42 0
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
StringAsLabel: true,
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -141,7 +142,7 @@ cpu_time_idle{host="example.org"} 42
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
StringAsLabel: false,
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -167,7 +168,7 @@ cpu_time_idle 42
MetricVersion: 2,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand All @@ -194,7 +195,7 @@ cpu_time_idle{host="example.org"} 42
MetricVersion: 2,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -276,7 +277,7 @@ cpu_usage_idle_count{cpu="cpu1"} 20
MetricVersion: 2,
CollectorsExclude: []string{"gocollector", "process"},
Path: "/metrics",
Log: Logger,
Log: logger,
},
metrics: []telegraf.Metric{
testutil.MustMetric(
Expand Down Expand Up @@ -332,7 +333,7 @@ cpu_usage_idle_count{cpu="cpu1"} 20
}

func TestRoundTripMetricVersion2(t *testing.T) {
Logger := testutil.Logger{Name: "outputs.prometheus_client"}
logger := testutil.Logger{Name: "outputs.prometheus_client"}
tests := []struct {
name string
data []byte
Expand Down Expand Up @@ -414,7 +415,8 @@ rpc_duration_seconds_count 2693
t.Run(tt.name, func(t *testing.T) {
ts.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(tt.data)
_, err := w.Write(tt.data)
require.NoError(t, err)
})

input := &inputs.Prometheus{
Expand All @@ -435,7 +437,7 @@ rpc_duration_seconds_count 2693
Listen: "127.0.0.1:0",
Path: defaultPath,
MetricVersion: 2,
Log: Logger,
Log: logger,
CollectorsExclude: []string{"gocollector", "process"},
}
err = output.Init()
Expand All @@ -451,6 +453,7 @@ rpc_duration_seconds_count 2693

resp, err := http.Get(output.URL())
require.NoError(t, err)
defer resp.Body.Close()

actual, err := io.ReadAll(resp.Body)
require.NoError(t, err)
Expand Down
9 changes: 5 additions & 4 deletions plugins/outputs/riemann/riemann.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/amir/raidman"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/outputs"
Expand Down Expand Up @@ -78,12 +79,12 @@ func (r *Riemann) Connect() error {
return nil
}

func (r *Riemann) Close() error {
func (r *Riemann) Close() (err error) {
if r.client != nil {
r.client.Close()
err = r.client.Close()
r.client = nil
}
return nil
return err
}

func (r *Riemann) SampleConfig() string {
Expand Down Expand Up @@ -113,7 +114,7 @@ func (r *Riemann) Write(metrics []telegraf.Metric) error {
}

if err := r.client.SendMulti(events); err != nil {
r.Close()
r.Close() //nolint:revive // There is another error which will be returned here
return fmt.Errorf("failed to send riemann message: %s", err)
}
return nil
Expand Down
Loading

0 comments on commit 64bc0ae

Please sign in to comment.