From ece8a77b60f1e0d9dd88e700fc1fa21d814c0215 Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Thu, 18 Jul 2019 19:13:07 +0200 Subject: [PATCH] Cherry-pick #12914 to 7.2: Release connections in metricbeat redis module (#12948) Release pooled connections in metricbeat redis module when they are not needed anymore so they can be reused. (cherry picked from commit b65f05b35c5b810234840f2344e68f9e87d20296) --- CHANGELOG.next.asciidoc | 2 ++ metricbeat/module/redis/info/info.go | 5 ++++- metricbeat/module/redis/key/key.go | 2 ++ metricbeat/module/redis/keyspace/keyspace.go | 5 ++++- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 0ec555b9ba78..7881992d3216 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -40,6 +40,8 @@ https://github.com/elastic/beats/compare/v7.2.0...7.2[Check the HEAD diff] *Metricbeat* +- Fix connections leak in redis module {pull}12914[12914] + *Packetbeat* *Winlogbeat* diff --git a/metricbeat/module/redis/info/info.go b/metricbeat/module/redis/info/info.go index c7f9762d1e87..3580d53a6c80 100644 --- a/metricbeat/module/redis/info/info.go +++ b/metricbeat/module/redis/info/info.go @@ -50,8 +50,11 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // Fetch fetches metrics from Redis by issuing the INFO command. func (m *MetricSet) Fetch(r mb.ReporterV2) error { + conn := m.Connection() + defer conn.Close() + // Fetch default INFO. - info, err := redis.FetchRedisInfo("default", m.Connection()) + info, err := redis.FetchRedisInfo("default", conn) if err != nil { return errors.Wrap(err, "failed to fetch redis info") } diff --git a/metricbeat/module/redis/key/key.go b/metricbeat/module/redis/key/key.go index 4cd8663adb82..085729678ca5 100644 --- a/metricbeat/module/redis/key/key.go +++ b/metricbeat/module/redis/key/key.go @@ -70,6 +70,8 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // Fetch fetches information from Redis keys func (m *MetricSet) Fetch(r mb.ReporterV2) error { conn := m.Connection() + defer conn.Close() + for _, p := range m.patterns { if err := redis.Select(conn, p.Keyspace); err != nil { msg := errors.Wrapf(err, "Failed to select keyspace %d", p.Keyspace) diff --git a/metricbeat/module/redis/keyspace/keyspace.go b/metricbeat/module/redis/keyspace/keyspace.go index 73172e05f97c..3f2f03818460 100644 --- a/metricbeat/module/redis/keyspace/keyspace.go +++ b/metricbeat/module/redis/keyspace/keyspace.go @@ -48,8 +48,11 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // Fetch fetches metrics from Redis by issuing the INFO command. func (m *MetricSet) Fetch(r mb.ReporterV2) error { + conn := m.Connection() + defer conn.Close() + // Fetch default INFO. - info, err := redis.FetchRedisInfo("keyspace", m.Connection()) + info, err := redis.FetchRedisInfo("keyspace", conn) if err != nil { return errors.Wrap(err, "Failed to fetch redis info for keyspaces") }