diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index a9ee19e8aad..3e615dd21af 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -181,6 +181,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Fix issue with `elasticsearch/node_stats` metricset (x-pack) not indexing `source_node` field. {pull}10639[10639] - Migrate docker autodiscover to ECS. {issue}10757[10757] {pull}10862[10862] - Fix issue in kubernetes module preventing usage percentages to be properly calculated. {pull}10946[10946] +- Fix for not reusable http client leading to connection leaks in Jolokia module {pull}11014[11014] *Packetbeat* diff --git a/metricbeat/module/jolokia/jmx/config.go b/metricbeat/module/jolokia/jmx/config.go index ad6be581e94..60b9a60ff63 100644 --- a/metricbeat/module/jolokia/jmx/config.go +++ b/metricbeat/module/jolokia/jmx/config.go @@ -29,7 +29,6 @@ import ( "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/logp" - "github.com/elastic/beats/metricbeat/helper" ) type JMXMapping struct { @@ -355,20 +354,17 @@ func (pc *JolokiaHTTPGetFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { } for _, r := range httpReqs { + m.http.SetMethod(r.HTTPMethod) + m.http.SetURI(m.BaseMetricSet.HostData().SanitizedURI + r.URI) - http, err := helper.NewHTTP(m.BaseMetricSet) - - http.SetMethod(r.HTTPMethod) - http.SetURI(m.BaseMetricSet.HostData().SanitizedURI + r.URI) - - resBody, err := http.FetchContent() + resBody, err := m.http.FetchContent() if err != nil { return nil, err } if logp.IsDebug(metricsetName) { m.log.Debugw("Jolokia response body", - "host", m.HostData().Host, "uri", http.GetURI(), "body", string(resBody), "type", "response") + "host", m.HostData().Host, "uri", m.http.GetURI(), "body", string(resBody), "type", "response") } // Map response to Metricbeat events @@ -491,19 +487,17 @@ func (pc *JolokiaHTTPPostFetcher) Fetch(m *MetricSet) ([]common.MapStr, error) { } } - http, err := helper.NewHTTP(m.BaseMetricSet) - - http.SetMethod(httpReqs[0].HTTPMethod) - http.SetBody(httpReqs[0].Body) + m.http.SetMethod(httpReqs[0].HTTPMethod) + m.http.SetBody(httpReqs[0].Body) - resBody, err := http.FetchContent() + resBody, err := m.http.FetchContent() if err != nil { return nil, err } if logp.IsDebug(metricsetName) { m.log.Debugw("Jolokia response body", - "host", m.HostData().Host, "uri", http.GetURI(), "body", string(resBody), "type", "response") + "host", m.HostData().Host, "uri", m.http.GetURI(), "body", string(resBody), "type", "response") } // Map response to Metricbeat events diff --git a/metricbeat/module/jolokia/jmx/jmx.go b/metricbeat/module/jolokia/jmx/jmx.go index 7ad38bfe1df..ba0ac72cb68 100644 --- a/metricbeat/module/jolokia/jmx/jmx.go +++ b/metricbeat/module/jolokia/jmx/jmx.go @@ -20,6 +20,8 @@ package jmx import ( "github.com/joeshaw/multierror" + "github.com/elastic/beats/metricbeat/helper" + "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/logp" "github.com/elastic/beats/metricbeat/mb" @@ -55,8 +57,9 @@ type MetricSet struct { mb.BaseMetricSet mapping []JMXMapping namespace string - http JolokiaHTTPRequestFetcher + jolokia JolokiaHTTPRequestFetcher log *logp.Logger + http *helper.HTTP } // New create a new instance of the MetricSet @@ -71,16 +74,22 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { return nil, err } - jolokiaHTTPBuild := NewJolokiaHTTPRequestFetcher(config.HTTPMethod) + jolokiaFetcher := NewJolokiaHTTPRequestFetcher(config.HTTPMethod) log := logp.NewLogger(metricsetName).With("host", base.HostData().Host) + http, err := helper.NewHTTP(base) + if err != nil { + return nil, err + } + return &MetricSet{ BaseMetricSet: base, mapping: config.Mappings, namespace: config.Namespace, - http: jolokiaHTTPBuild, + jolokia: jolokiaFetcher, log: log, + http: http, }, nil } @@ -89,7 +98,7 @@ func (m *MetricSet) Fetch() ([]common.MapStr, error) { var allEvents []common.MapStr - allEvents, err := m.http.Fetch(m) + allEvents, err := m.jolokia.Fetch(m) if err != nil { return nil, err }