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] support basepath in beat module #28162

Merged
merged 2 commits into from
Sep 29, 2021
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 @@ -405,6 +405,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix remote_write flaky test. {pull}21173[21173]
- Remove io.time from windows {pull}22237[22237]
- Change vsphere.datastore.capacity.used.pct value to betweeen 0 and 1. {pull}23148[23148]
- `beat` module respects `basepath` config option. {pull}28162[28162]

*Packetbeat*

Expand Down
34 changes: 25 additions & 9 deletions metricbeat/module/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"encoding/json"
"fmt"
"net/url"
"path"
"strings"

"github.com/elastic/beats/v7/libbeat/logp"
"github.com/elastic/beats/v7/metricbeat/helper"
Expand All @@ -35,9 +37,11 @@ func init() {
}
}

var metricSets = []string{"state", "stats"}

// NewModule creates a new module
func NewModule(base mb.BaseModule) (mb.Module, error) {
return elastic.NewModule(&base, []string{"state", "stats"}, logp.NewLogger(ModuleName))
return elastic.NewModule(&base, metricSets, logp.NewLogger(ModuleName))
}

// ModuleName is the name of this module.
Expand Down Expand Up @@ -76,7 +80,7 @@ type State struct {

// GetInfo returns the data for the Beat's / endpoint.
func GetInfo(m *MetricSet) (*Info, error) {
content, err := fetchPath(m.HTTP, "/", "")
content, err := fetchPath(m.HTTP, "/")
if err != nil {
return nil, err
}
Expand All @@ -92,7 +96,7 @@ func GetInfo(m *MetricSet) (*Info, error) {

// GetState returns the data for the Beat's /state endpoint.
func GetState(m *MetricSet) (*State, error) {
content, err := fetchPath(m.HTTP, "/state", "")
content, err := fetchPath(m.HTTP, "/state")
if err != nil {
return nil, err
}
Expand All @@ -106,16 +110,28 @@ func GetState(m *MetricSet) (*State, error) {
return info, nil
}

func fetchPath(httpHelper *helper.HTTP, path string, query string) ([]byte, error) {
func fetchPath(httpHelper *helper.HTTP, path string) ([]byte, error) {
currentURI := httpHelper.GetURI()
defer httpHelper.SetURI(currentURI)

// Parses the uri to replace the path
u, _ := url.Parse(currentURI)
u.Path = path
u.RawQuery = query
u, err := url.Parse(currentURI)
if err != nil {
return nil, err
}

// Http helper includes the HostData with username and password
httpHelper.SetURI(u.String())
// HTTP helper includes the HostData with username and password
httpHelper.SetURI(fetchURI(u, path))
return httpHelper.FetchContent()
}

func fetchURI(u *url.URL, uriPath string) string {
for _, s := range metricSets {
if strings.HasSuffix(u.Path, s) {
u.Path = u.Path[:len(u.Path)-len(s)]
break
}
}
u.Path = path.Join(u.Path, uriPath)
return u.String()
}
73 changes: 73 additions & 0 deletions metricbeat/module/beat/beat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 beat

import (
"net/url"
"testing"

"github.com/stretchr/testify/require"
"gotest.tools/assert"
)

func TestFetchURI(t *testing.T) {
tcs := []struct {
orig, path, want string
}{
{
orig: "https://localhost:5000/some/proxy/path",
path: "/state",
want: "https://localhost:5000/some/proxy/path/state",
}, {
orig: "https://localhost:5000/some/proxy/path/state",
path: "/state",
want: "https://localhost:5000/some/proxy/path/state",
}, {
orig: "https://localhost:5000/some/proxy/path/state",
path: "/",
want: "https://localhost:5000/some/proxy/path",
}, {
orig: "http://localhost:5000",
path: "/state",
want: "http://localhost:5000/state",
}, {
orig: "http://localhost:5000/state",
path: "/state",
want: "http://localhost:5000/state",
}, {
orig: "http://localhost:5000/stats",
path: "/state",
want: "http://localhost:5000/state",
}, {
orig: "http://localhost:5000/stats",
path: "/",
want: "http://localhost:5000/",
}, {
orig: "http://localhost:5000/state",
path: "/",
want: "http://localhost:5000/",
},
}

for _, tc := range tcs {
u, err := url.Parse(tc.orig)
require.NoError(t, err)
got := fetchURI(u, tc.path)
assert.Equal(t, tc.want, got)
}
}