Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
34 changes: 33 additions & 1 deletion metricbeat/module/memcached/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package stats

import (
"bufio"
"fmt"
"net"
"strings"

Expand All @@ -35,19 +36,36 @@ 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"`
jsoriano marked this conversation as resolved.
Show resolved Hide resolved
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
return &MetricSet{
BaseMetricSet: base,
network: config.Network,
socketPath: config.SocketPath,
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right
// 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 +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
}