Skip to content

Commit

Permalink
Cherry-pick #15822 to 7.x: [Metricbeat] Memcached: add support for Un…
Browse files Browse the repository at this point in the history
…ix socket (#15858)

* [Metricbeat] Memcached: add support for Unix socket (#15822)

* Memcached: add support for Unix socket

* Use single host field for URI

(cherry picked from commit 4a072f7)

* Fix: changelog
  • Loading branch information
mtojek authored Jan 27, 2020
1 parent 9772633 commit cd1808e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add IBM MQ light-weight Metricbeat module {pull}15301[15301]
- Enable script processor. {pull}14711[14711]
- Add mesh metricset for Istio Metricbeat module{pull}15535[15535]
- Add support for Unix socket in Memcached metricbeat module. {issue}13685[13685] {pull}15822[15822]


*Packetbeat*

Expand Down
24 changes: 23 additions & 1 deletion metricbeat/module/memcached/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ package stats
import (
"bufio"
"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(),
)
}
Expand All @@ -47,7 +52,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")
}
Expand Down Expand Up @@ -81,3 +91,15 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {

return nil
}

func (m *MetricSet) getNetworkAndAddress() (network string, address string, err error) {
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
}

0 comments on commit cd1808e

Please sign in to comment.