Skip to content

Commit

Permalink
Fix for not reusable http client leading to connection leaks in Jolo…
Browse files Browse the repository at this point in the history
…kia module (#11014) (#11087)

Jolokia module was creating a new HTTP helper for each request, what
was leading to leaks under some scenarios. Make it reuse connections.

(cherry picked from commit 288a76c)

Co-Authored-By: Carmelo Mirko Musumeci <[email protected]>
  • Loading branch information
jsoriano and Carmelo Mirko Musumeci authored Mar 14, 2019
1 parent ebd2750 commit eaf4ec6
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ https://github.com/elastic/beats/compare/v7.0.0-beta1...master[Check the HEAD di

- 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]
- Fix parsing error using GET in Jolokia module. {pull}11075[11075] {issue}11071[11071]
- Add documentation about jolokia autodiscover fields. {issue}10925[10925] {pull}10979[10979]
- Add missing aws.ec2.instance.state.name into fields.yml. {issue}11219[11219] {pull}11221[11221]
Expand Down
22 changes: 8 additions & 14 deletions metricbeat/module/jolokia/jmx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions metricbeat/module/jolokia/jmx/jmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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
}
Expand Down

0 comments on commit eaf4ec6

Please sign in to comment.