Skip to content

Commit

Permalink
[metricbeat] support basepath in beat module
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartnelson3 committed Sep 28, 2021
1 parent 7e4affb commit 0d46fb9
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 9 deletions.
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)
}
}

0 comments on commit 0d46fb9

Please sign in to comment.