From a081d9627de1f3bb16daf130103fc891996e8dbb Mon Sep 17 00:00:00 2001 From: Jaime Soriano Pastor Date: Fri, 19 Jul 2019 08:44:31 +0200 Subject: [PATCH] Cherry-pick #12950 to 7.3: Reuse connections in redis info metricset (#12967) Reuse connections in redis info metricset, and log close errors at the debug level. (cherry picked from commit 4d207e7a12c0d5a93916c298b594aeceaf5d0bf8) --- CHANGELOG.next.asciidoc | 2 +- metricbeat/module/redis/info/info.go | 8 ++++++-- metricbeat/module/redis/key/key.go | 6 +++++- metricbeat/module/redis/keyspace/keyspace.go | 6 +++++- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index e37d658866d..cc27b1c09ff 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -145,7 +145,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Reuse connections in PostgreSQL metricsets. {issue}12504[12504] {pull}12603[12603] - PdhExpandWildCardPathW will not expand counter paths in 32 bit windows systems, workaround will use a different function.{issue}12590[12590]{pull}12622[12622] - In the elasticsearch/node_stats metricset, if xpack is enabled, make parsing of ES node load average optional as ES on Windows doesn't report load average. {pull}12866[12866] -- Fix connections leak in redis module {pull}12914[12914] +- Fix connections leak in redis module {pull}12914[12914] {pull}12950[12950] - Fix wrong uptime reporting by system/uptime metricset under Windows. {pull}12915[12915] *Packetbeat* diff --git a/metricbeat/module/redis/info/info.go b/metricbeat/module/redis/info/info.go index 9ff475a4899..0570710b201 100644 --- a/metricbeat/module/redis/info/info.go +++ b/metricbeat/module/redis/info/info.go @@ -53,7 +53,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() + defer func() { + if err := conn.Close(); err != nil { + m.Logger().Debug(errors.Wrapf(err, "failed to release connection")) + } + }() // Fetch default INFO. info, err := redis.FetchRedisInfo("default", conn) @@ -76,7 +80,7 @@ func (m *MetricSet) Fetch(r mb.ReporterV2) error { } } - slowLogLength, err := redis.FetchSlowLogLength(m.Connection()) + slowLogLength, err := redis.FetchSlowLogLength(conn) if err != nil { return errors.Wrap(err, "failed to fetch slow log length") diff --git a/metricbeat/module/redis/key/key.go b/metricbeat/module/redis/key/key.go index 1ddf9186efe..bb116def471 100644 --- a/metricbeat/module/redis/key/key.go +++ b/metricbeat/module/redis/key/key.go @@ -72,7 +72,11 @@ 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() + defer func() { + if err := conn.Close(); err != nil { + m.Logger().Debug(errors.Wrapf(err, "failed to release connection")) + } + }() for _, p := range m.patterns { if err := redis.Select(conn, p.Keyspace); err != nil { diff --git a/metricbeat/module/redis/keyspace/keyspace.go b/metricbeat/module/redis/keyspace/keyspace.go index 6aa1ff5820a..fba7166341f 100644 --- a/metricbeat/module/redis/keyspace/keyspace.go +++ b/metricbeat/module/redis/keyspace/keyspace.go @@ -51,7 +51,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() + defer func() { + if err := conn.Close(); err != nil { + m.Logger().Debug(errors.Wrapf(err, "failed to release connection")) + } + }() // Fetch default INFO. info, err := redis.FetchRedisInfo("keyspace", conn)