Skip to content

Commit

Permalink
Adding support for custom http headers and TLS for metricbeat modules
Browse files Browse the repository at this point in the history
  • Loading branch information
vjsamuel committed Apr 7, 2017
1 parent 30d9baa commit f485795
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]
- Add experimental metricset `perfmon` to Windows module. {pull}3758[3758]
- Add memcached module with stats metricset. {pull}3693[3693]
- Add the `process.cmdline.cache.enabled` config option to the System Process Metricset. {pull}3891[3891]
- Adding support for custom http headers and TLS for metricbeat modules {pull}3945[3945]

*Packetbeat*
- Add `fields` and `fields_under_root` to packetbeat protocols configurations. {pull}3518[3518]
Expand Down
41 changes: 38 additions & 3 deletions metricbeat/helper/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"io"
"io/ioutil"
"net/http"
"time"

"github.com/elastic/beats/libbeat/outputs"
"github.com/elastic/beats/libbeat/outputs/transport"
"github.com/elastic/beats/metricbeat/mb"
)

Expand All @@ -22,10 +25,42 @@ type HTTP struct {

// NewHTTP creates new http helper
func NewHTTP(base mb.BaseMetricSet) *HTTP {
config := struct {
TLS *outputs.TLSConfig `config:"ssl"`
Timeout time.Duration `config:"timeout"`
Headers map[string]string `config:"headers"`
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil
}

if config.Headers == nil {
config.Headers = map[string]string{}
}

tlsConfig, err := outputs.LoadTLSConfig(config.TLS)
if err != nil {
return nil
}

var dialer, tlsDialer transport.Dialer

dialer = transport.NetDialer(config.Timeout)
tlsDialer, err = transport.TLSDialer(dialer, tlsConfig, config.Timeout)
if err != nil {
return nil
}

return &HTTP{
base: base,
client: &http.Client{Timeout: base.Module().Config().Timeout},
headers: map[string]string{},
base: base,
client: &http.Client{
Transport: &http.Transport{
Dial: dialer.Dial,
DialTLS: tlsDialer.Dial,
},
Timeout: config.Timeout,
},
headers: config.Headers,
method: "GET",
body: nil,
}
Expand Down

0 comments on commit f485795

Please sign in to comment.