Skip to content

Commit 9eedf16

Browse files
committed
[Stack Monitoring] Trim Trailing / After Host URI
This removes any trailing slash to simplify configuration of both Stack Monitoring and AutoOps hostnames. If the user accidentally adds the `/`, then historically some requests would succeed and some would fail and, depending on the error handling, the error may not be made clear. This bypasses the whole issue.
1 parent 990735b commit 9eedf16

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# REQUIRED
2+
# Kind can be one of:
3+
# - breaking-change: a change to previously-documented behavior
4+
# - deprecation: functionality that is being removed in a later release
5+
# - bug-fix: fixes a problem in a previous version
6+
# - enhancement: extends functionality but does not break or fix existing behavior
7+
# - feature: new functionality
8+
# - known-issue: problems that we are aware of in a given version
9+
# - security: impacts on the security of a product or a user’s deployment.
10+
# - upgrade: important information for someone upgrading from a prior version
11+
# - other: does not fit into any of the other categories
12+
kind: bug-fix
13+
14+
# REQUIRED for all kinds
15+
# Change summary; a 80ish characters long description of the change.
16+
summary: Stack Monitoring now trims trailing slashes from host URLs for simplicity
17+
18+
# REQUIRED for breaking-change, deprecation, known-issue
19+
# Long description; in case the summary is not enough to describe the change
20+
# this field accommodate a description without length limits.
21+
# description:
22+
23+
# REQUIRED for breaking-change, deprecation, known-issue
24+
# impact:
25+
26+
# REQUIRED for breaking-change, deprecation, known-issue
27+
# action:
28+
29+
# REQUIRED for all kinds
30+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
31+
component: metricbeat
32+
33+
# AUTOMATED
34+
# OPTIONAL to manually add other PR URLs
35+
# PR URL: A link the PR that added the changeset.
36+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
37+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
38+
# Please provide it if you are adding a fragment for a different PR.
39+
# pr: https://github.com/owner/repo/1234
40+
41+
# AUTOMATED
42+
# OPTIONAL to manually add other issue URLs
43+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
44+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
45+
issue: https://github.com/elastic/beats/issues/48426

metricbeat/module/elasticsearch/metricset.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ type MetricSetAPI interface {
8686
// management plugin
8787
type MetricSet struct {
8888
mb.BaseMetricSet
89+
hostURI string
8990
servicePath string
9091
*helper.HTTP
9192
Scope Scope
9293
XPackEnabled bool
9394
}
9495

95-
// NewMetricSet creates an metric set that can be used to build other metric
96-
// sets that query RabbitMQ management plugin
96+
// NewMetricSet creates a metric set that can be used to build other metric
97+
// sets that query Elasticsearch APIs
9798
func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error) {
9899
http, err := helper.NewHTTP(base)
99100
if err != nil {
@@ -131,8 +132,20 @@ func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error)
131132
http.SetHeader("Authorization", "ApiKey "+apiKey)
132133
}
133134

135+
hostURI := base.HostData().SanitizedURI
136+
137+
// if you supply your URI like "http://localhost:9200/" we need to trim the trailing slash to avoid issues with
138+
// downstream paths
139+
if strings.HasSuffix(hostURI, "/") {
140+
badURI := hostURI
141+
hostURI = strings.TrimRight(hostURI, "/")
142+
143+
base.Logger().Warnf("host URI should not have a trailing slash, updated from %s to %s", badURI, hostURI)
144+
}
145+
134146
ms := &MetricSet{
135147
base,
148+
hostURI,
136149
servicePath,
137150
http,
138151
config.Scope,
@@ -146,7 +159,7 @@ func NewMetricSet(base mb.BaseMetricSet, servicePath string) (*MetricSet, error)
146159

147160
// GetServiceURI returns the URI of the Elasticsearch service being monitored by this metricset
148161
func (m *MetricSet) GetServiceURI() string {
149-
return m.HostData().SanitizedURI + m.servicePath
162+
return m.hostURI + m.servicePath
150163
}
151164

152165
// SetServiceURI updates the URI of the Elasticsearch service being monitored by this metricset

metricbeat/module/elasticsearch/node/node_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ package node
2121

2222
import (
2323
"encoding/base64"
24-
"io/ioutil"
2524
"net/http"
2625
"net/http/httptest"
26+
"os"
2727
"path/filepath"
28+
"strings"
2829
"testing"
2930

3031
"github.com/stretchr/testify/require"
@@ -42,7 +43,7 @@ func TestFetch(t *testing.T) {
4243

4344
for _, f := range files {
4445
t.Run(f, func(t *testing.T) {
45-
response, err := ioutil.ReadFile(f)
46+
response, err := os.ReadFile(f)
4647
require.NoError(t, err)
4748

4849
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -128,10 +129,17 @@ func TestFetch(t *testing.T) {
128129
}))
129130
defer server.Close()
130131

132+
serverURL := server.URL
133+
134+
// for an arbitrary request, try configuring with an extra trailing slash
135+
if strings.HasPrefix(tt.name, "from config") {
136+
serverURL = server.URL + "/"
137+
}
138+
131139
config := map[string]any{
132140
"module": "elasticsearch",
133141
"metricsets": []string{"node"},
134-
"hosts": []string{server.URL},
142+
"hosts": []string{serverURL},
135143
}
136144

137145
apiKey := base64.StdEncoding.EncodeToString([]byte(tt.apiKey))

0 commit comments

Comments
 (0)