From 306d7a3e0152b74f3370aaaf1a18c9e1671c0e2f Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Fri, 24 Jan 2020 15:37:21 +0100 Subject: [PATCH 1/2] Memcached: add support for Unix socket --- CHANGELOG.next.asciidoc | 1 + metricbeat/module/memcached/stats/stats.go | 34 +++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 8bd298f3098..0547557dbdc 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -97,6 +97,7 @@ TLS or Beats that accept connections over TLS and validate client certificates. - Add a `system/network_summary` metricset {pull}15196[15196] - Add mesh metricset for Istio Metricbeat module{pull}15535[15535] - Make the `system/cpu` metricset collect normalized CPU metrics by default. {issue}15618[15618] {pull}15729[15729] +- Add support for Unix socket in Memcached metricbeat module. {issue}13685[13685] {pull}15822[15822] *Packetbeat* diff --git a/metricbeat/module/memcached/stats/stats.go b/metricbeat/module/memcached/stats/stats.go index be66dfc3e68..bbc0e79341d 100644 --- a/metricbeat/module/memcached/stats/stats.go +++ b/metricbeat/module/memcached/stats/stats.go @@ -19,6 +19,7 @@ package stats import ( "bufio" + "fmt" "net" "strings" @@ -35,11 +36,23 @@ func init() { type MetricSet struct { mb.BaseMetricSet + + network string + socketPath string } func New(base mb.BaseMetricSet) (mb.MetricSet, error) { + config := struct { + Network string `config:"network"` + SocketPath string `config:"socket_path"` + }{} + if err := base.Module().UnpackConfig(&config); err != nil { + return nil, err + } return &MetricSet{ BaseMetricSet: base, + network: config.Network, + socketPath: config.SocketPath, }, nil } @@ -47,7 +60,12 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) { // format. It publishes the event which is then forwarded to the output. In case // of an error set the Error field of mb.Event or simply call report.Error(). func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { - conn, err := net.DialTimeout("tcp", m.Host(), m.Module().Config().Timeout) + network, address, err := m.getNetworkAndAddress() + if err != nil { + return errors.Wrap(err, "error in fetch") + } + + conn, err := net.DialTimeout(network, address, m.Module().Config().Timeout) if err != nil { return errors.Wrap(err, "error in fetch") } @@ -81,3 +99,17 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { return nil } + +func (m *MetricSet) getNetworkAndAddress() (network string, address string, err error) { + switch m.network { + case "", "tcp": + network = "tcp" + address = m.Host() + case "unix": + network = "unix" + address = m.socketPath + default: + err = fmt.Errorf("unsupported network: %s", m.network) + } + return +} From 41959d5f1cade9b8141defba1590bdd57d7cd729 Mon Sep 17 00:00:00 2001 From: Marcin Tojek Date: Mon, 27 Jan 2020 07:41:14 +0100 Subject: [PATCH 2/2] Use single host field for URI --- metricbeat/module/memcached/stats/stats.go | 34 ++++++++-------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/metricbeat/module/memcached/stats/stats.go b/metricbeat/module/memcached/stats/stats.go index bbc0e79341d..52c938a6342 100644 --- a/metricbeat/module/memcached/stats/stats.go +++ b/metricbeat/module/memcached/stats/stats.go @@ -19,40 +19,32 @@ package stats import ( "bufio" - "fmt" "net" + "net/url" "strings" "github.com/pkg/errors" "github.com/elastic/beats/metricbeat/mb" + "github.com/elastic/beats/metricbeat/mb/parse" ) +var hostParser = parse.URLHostParserBuilder{DefaultScheme: "tcp"}.Build() + func init() { mb.Registry.MustAddMetricSet("memcached", "stats", New, + mb.WithHostParser(hostParser), mb.DefaultMetricSet(), ) } type MetricSet struct { mb.BaseMetricSet - - network string - socketPath string } func New(base mb.BaseMetricSet) (mb.MetricSet, error) { - config := struct { - Network string `config:"network"` - SocketPath string `config:"socket_path"` - }{} - if err := base.Module().UnpackConfig(&config); err != nil { - return nil, err - } return &MetricSet{ BaseMetricSet: base, - network: config.Network, - socketPath: config.SocketPath, }, nil } @@ -101,15 +93,13 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error { } func (m *MetricSet) getNetworkAndAddress() (network string, address string, err error) { - switch m.network { - case "", "tcp": - network = "tcp" - address = m.Host() - case "unix": - network = "unix" - address = m.socketPath - default: - err = fmt.Errorf("unsupported network: %s", m.network) + hostData := m.HostData() + u, err := url.Parse(hostData.URI) + if err != nil { + err = errors.Wrap(err, "invalid URL") + return } + network = u.Scheme + address = u.Host return }