Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for custom http headers and TLS for metricbeat modules #3945

Merged
merged 2 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@urso as we start to using this in other places, we should probably move it out from the output package to ?

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