Skip to content

Commit a417403

Browse files
committed
Don't generate autodiscover config when no port matches host hints (elastic#12086)
On metricbeat, when the host autodiscover hint is used, and it includes the port, one of the exposed ports has to match with the one in the hint. If not, no configuration should be generated. If it is generated, it will have empty hosts, what would lead to unexpected errors as the seen in elastic#8264. (cherry picked from commit cc73643)
1 parent 03b3db2 commit a417403

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ https://github.com/elastic/beats/compare/v7.0.0...7.1[Check the HEAD diff]
4343
*Metricbeat*
4444

4545
- Update documentation with cloudwatch:ListMetrics permission. {pull}11987[11987]
46+
- Avoid generating hints-based configuration with empty hosts when no exposed port is suitable for the hosts hint. {issue}8264[8264] {pull}12086[12086]
4647

4748
*Packetbeat*
4849

metricbeat/autodiscover/builder/hints/metrics.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,11 @@ func (m *metricHints) CreateConfig(event bus.Event) []*common.Config {
101101
return config
102102
}
103103

104-
hsts := m.getHostsWithPort(hints, port)
104+
hosts, ok := m.getHostsWithPort(hints, port)
105+
if !ok {
106+
return config
107+
}
108+
105109
ns := m.getNamespace(hints)
106110
msets := m.getMetricSets(hints, mod)
107111
tout := m.getTimeout(hints)
@@ -112,7 +116,7 @@ func (m *metricHints) CreateConfig(event bus.Event) []*common.Config {
112116
moduleConfig := common.MapStr{
113117
"module": mod,
114118
"metricsets": msets,
115-
"hosts": hsts,
119+
"hosts": hosts,
116120
"timeout": tout,
117121
"period": ival,
118122
"enabled": true,
@@ -161,7 +165,7 @@ func (m *metricHints) getMetricSets(hints common.MapStr, module string) []string
161165
return msets
162166
}
163167

164-
func (m *metricHints) getHostsWithPort(hints common.MapStr, port int) []string {
168+
func (m *metricHints) getHostsWithPort(hints common.MapStr, port int) ([]string, bool) {
165169
var result []string
166170
thosts := builder.GetHintAsList(hints, m.Key, hosts)
167171

@@ -175,7 +179,12 @@ func (m *metricHints) getHostsWithPort(hints common.MapStr, port int) []string {
175179
}
176180
}
177181

178-
return result
182+
if len(thosts) > 0 && len(result) == 0 {
183+
logp.Debug("hints.builder", "no hosts selected for port %d with hints: %+v", port, thosts)
184+
return nil, false
185+
}
186+
187+
return result, true
179188
}
180189

181190
func (m *metricHints) getNamespace(hints common.MapStr) string {

metricbeat/autodiscover/builder/hints/metrics_test.go

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,65 @@ func TestGenerateHints(t *testing.T) {
6767
len: 0,
6868
result: common.MapStr{},
6969
},
70+
{
71+
message: "Hints without matching port should return nothing",
72+
event: bus.Event{
73+
"host": "1.2.3.4",
74+
"port": 9090,
75+
"hints": common.MapStr{
76+
"metrics": common.MapStr{
77+
"module": "mockmoduledefaults",
78+
"hosts": "${data.host}:8888",
79+
},
80+
},
81+
},
82+
len: 0,
83+
result: common.MapStr{},
84+
},
85+
{
86+
message: "Hints with multiple hosts return only the matching one",
87+
event: bus.Event{
88+
"host": "1.2.3.4",
89+
"port": 9090,
90+
"hints": common.MapStr{
91+
"metrics": common.MapStr{
92+
"module": "mockmoduledefaults",
93+
"hosts": "${data.host}:8888,${data.host}:9090",
94+
},
95+
},
96+
},
97+
len: 1,
98+
result: common.MapStr{
99+
"module": "mockmoduledefaults",
100+
"metricsets": []string{"default"},
101+
"timeout": "3s",
102+
"period": "1m",
103+
"enabled": true,
104+
"hosts": []interface{}{"1.2.3.4:9090"},
105+
},
106+
},
107+
{
108+
message: "Hints with multiple hosts return only the one with the template",
109+
event: bus.Event{
110+
"host": "1.2.3.4",
111+
"port": 9090,
112+
"hints": common.MapStr{
113+
"metrics": common.MapStr{
114+
"module": "mockmoduledefaults",
115+
"hosts": "${data.host}:8888,${data.host}:${data.port}",
116+
},
117+
},
118+
},
119+
len: 1,
120+
result: common.MapStr{
121+
"module": "mockmoduledefaults",
122+
"metricsets": []string{"default"},
123+
"timeout": "3s",
124+
"period": "1m",
125+
"enabled": true,
126+
"hosts": []interface{}{"1.2.3.4:9090"},
127+
},
128+
},
70129
{
71130
message: "Only module hint should return all metricsets",
72131
event: bus.Event{
@@ -87,7 +146,7 @@ func TestGenerateHints(t *testing.T) {
87146
},
88147
},
89148
{
90-
message: "metricsets hint works",
149+
message: "Metricsets hint works",
91150
event: bus.Event{
92151
"host": "1.2.3.4",
93152
"hints": common.MapStr{

0 commit comments

Comments
 (0)