Skip to content

Commit bf5a302

Browse files
Soulouotherpirate
authored andcommitted
Add support for TLS configuration in NSQ input (influxdata#3903)
1 parent fd9b8db commit bf5a302

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

plugins/inputs/nsq/nsq.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,27 @@ import (
3333
"time"
3434

3535
"github.com/influxdata/telegraf"
36+
"github.com/influxdata/telegraf/internal/tls"
3637
"github.com/influxdata/telegraf/plugins/inputs"
3738
)
3839

3940
// Might add Lookupd endpoints for cluster discovery
4041
type NSQ struct {
4142
Endpoints []string
43+
tls.ClientConfig
44+
httpClient *http.Client
4245
}
4346

4447
var sampleConfig = `
4548
## An array of NSQD HTTP API endpoints
46-
endpoints = ["http://localhost:4151"]
49+
endpoints = ["http://localhost:4151"]
50+
51+
## Or using HTTPS endpoint
52+
endpoints = ["https://localhost:4152"]
53+
tls_cert = "/path/to/client-cert.pem"
54+
tls_key = "/path/to/client-key.pem"
55+
tls_ca = "/path/to/ca.pem"
56+
insecure_skip_verify = false
4757
`
4858

4959
const (
@@ -52,10 +62,14 @@ const (
5262

5363
func init() {
5464
inputs.Add("nsq", func() telegraf.Input {
55-
return &NSQ{}
65+
return New()
5666
})
5767
}
5868

69+
func New() *NSQ {
70+
return &NSQ{}
71+
}
72+
5973
func (n *NSQ) SampleConfig() string {
6074
return sampleConfig
6175
}
@@ -65,6 +79,15 @@ func (n *NSQ) Description() string {
6579
}
6680

6781
func (n *NSQ) Gather(acc telegraf.Accumulator) error {
82+
var err error
83+
84+
if n.httpClient == nil {
85+
n.httpClient, err = n.getHttpClient()
86+
if err != nil {
87+
return err
88+
}
89+
}
90+
6891
var wg sync.WaitGroup
6992
for _, e := range n.Endpoints {
7093
wg.Add(1)
@@ -78,21 +101,27 @@ func (n *NSQ) Gather(acc telegraf.Accumulator) error {
78101
return nil
79102
}
80103

81-
var tr = &http.Transport{
82-
ResponseHeaderTimeout: time.Duration(3 * time.Second),
83-
}
84-
85-
var client = &http.Client{
86-
Transport: tr,
87-
Timeout: time.Duration(4 * time.Second),
104+
func (n *NSQ) getHttpClient() (*http.Client, error) {
105+
tlsConfig, err := n.ClientConfig.TLSConfig()
106+
if err != nil {
107+
return nil, err
108+
}
109+
tr := &http.Transport{
110+
TLSClientConfig: tlsConfig,
111+
}
112+
httpClient := &http.Client{
113+
Transport: tr,
114+
Timeout: time.Duration(4 * time.Second),
115+
}
116+
return httpClient, nil
88117
}
89118

90119
func (n *NSQ) gatherEndpoint(e string, acc telegraf.Accumulator) error {
91120
u, err := buildURL(e)
92121
if err != nil {
93122
return err
94123
}
95-
r, err := client.Get(u.String())
124+
r, err := n.httpClient.Get(u.String())
96125
if err != nil {
97126
return fmt.Errorf("Error while polling %s: %s", u.String(), err)
98127
}

plugins/inputs/nsq/nsq_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ func TestNSQStatsV1(t *testing.T) {
1919
}))
2020
defer ts.Close()
2121

22-
n := &NSQ{
23-
Endpoints: []string{ts.URL},
24-
}
22+
n := New()
23+
n.Endpoints = []string{ts.URL}
2524

2625
var acc testutil.Accumulator
2726
err := acc.GatherError(n.Gather)
@@ -276,9 +275,8 @@ func TestNSQStatsPreV1(t *testing.T) {
276275
}))
277276
defer ts.Close()
278277

279-
n := &NSQ{
280-
Endpoints: []string{ts.URL},
281-
}
278+
n := New()
279+
n.Endpoints = []string{ts.URL}
282280

283281
var acc testutil.Accumulator
284282
err := acc.GatherError(n.Gather)

0 commit comments

Comments
 (0)