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

Cherry-pick #17512 to 7.x: Fix Unix socket path in memcached #17560

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -186,6 +186,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Combine cloudwatch aggregated metrics into single event. {pull}17345[17345]
- Fix cloudwatch metricset missing tags collection. {issue}17419[17419] {pull}17424[17424]
- check if cpuOptions field is nil in DescribeInstances output in ec2 metricset. {pull}17418[17418]
- Fix Unix socket path in memcached. {pull}17512[17512]

*Packetbeat*

Expand Down
12 changes: 8 additions & 4 deletions metricbeat/module/memcached/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ 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 {
network, address, err := m.getNetworkAndAddress()
network, address, err := getNetworkAndAddress(m.HostData())
if err != nil {
return errors.Wrap(err, "error in fetch")
}
Expand Down Expand Up @@ -92,14 +92,18 @@ func (m *MetricSet) Fetch(reporter mb.ReporterV2) error {
return nil
}

func (m *MetricSet) getNetworkAndAddress() (network string, address string, err error) {
hostData := m.HostData()
func getNetworkAndAddress(hostData mb.HostData) (network string, address string, err error) {
u, err := url.Parse(hostData.URI)
if err != nil {
err = errors.Wrap(err, "invalid URL")
return
}

network = u.Scheme
address = u.Host
if network == "unix" {
address = u.Path
} else {
address = u.Host
}
return
}
48 changes: 48 additions & 0 deletions metricbeat/module/memcached/stats/stats_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package stats

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/elastic/beats/v7/metricbeat/mb"
)

func TestGetNetworkAddress_URL(t *testing.T) {
hostData := mb.HostData{
Host: "127.0.0.1:11211",
URI: "tcp://127.0.0.1:11211",
}
network, address, err := getNetworkAndAddress(hostData)
require.NoError(t, err)
require.Equal(t, "tcp", network)
require.Equal(t, "127.0.0.1:11211", address)
}

func TestGetNetworkAddress_Unix(t *testing.T) {
hostData := mb.HostData{
Host: "/tmp/d.sock",
URI: "unix:///tmp/d.sock",
}
network, address, err := getNetworkAndAddress(hostData)
require.NoError(t, err)
require.Equal(t, "unix", network)
require.Equal(t, "/tmp/d.sock", address)
}