Skip to content

Commit d545f69

Browse files
authored
Move HTTP calls to Kibana from New() to Fetch() (#15270)
* Move HTTP calls to Fetch() from New() This makes the `kibana/stats` metricset resilient to Kibana's unavailability. * Add special handling for errors when xpack.enabled is set to true * Don't reinit usageLastCollectedOn
1 parent 4def200 commit d545f69

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

metricbeat/module/kibana/stats/stats.go

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -67,46 +67,74 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
6767
return nil, err
6868
}
6969

70-
statsHTTP, err := helper.NewHTTP(base)
70+
return &MetricSet{
71+
MetricSet: ms,
72+
}, nil
73+
}
74+
75+
// Fetch methods implements the data gathering and data conversion to the right format
76+
// It returns the event which is then forward to the output. In case of an error, a
77+
// descriptive error must be returned.
78+
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
79+
err := m.init()
7180
if err != nil {
72-
return nil, err
81+
if m.XPackEnabled {
82+
m.Logger().Error(err)
83+
return nil
84+
}
85+
return err
7386
}
7487

75-
kibanaVersion, err := kibana.GetVersion(statsHTTP, statsPath)
88+
now := time.Now()
89+
90+
err = m.fetchStats(r, now)
7691
if err != nil {
77-
return nil, err
92+
if m.XPackEnabled {
93+
m.Logger().Error(err)
94+
return nil
95+
}
96+
return err
7897
}
7998

80-
isStatsAPIAvailable := kibana.IsStatsAPIAvailable(kibanaVersion)
99+
if m.XPackEnabled {
100+
m.fetchSettings(r, now)
101+
}
102+
103+
return nil
104+
}
105+
106+
func (m *MetricSet) init() error {
107+
statsHTTP, err := helper.NewHTTP(m.BaseMetricSet)
81108
if err != nil {
82-
return nil, err
109+
return err
83110
}
84111

85-
if !isStatsAPIAvailable {
86-
const errorMsg = "The %v metricset is only supported with Kibana >= %v. You are currently running Kibana %v"
87-
return nil, fmt.Errorf(errorMsg, base.FullyQualifiedName(), kibana.StatsAPIAvailableVersion, kibanaVersion)
112+
kibanaVersion, err := kibana.GetVersion(statsHTTP, statsPath)
113+
if err != nil {
114+
return err
88115
}
89116

90-
if ms.XPackEnabled {
117+
isStatsAPIAvailable := kibana.IsStatsAPIAvailable(kibanaVersion)
118+
if !isStatsAPIAvailable {
119+
const errorMsg = "the %v metricset is only supported with Kibana >= %v. You are currently running Kibana %v"
120+
return fmt.Errorf(errorMsg, m.FullyQualifiedName(), kibana.StatsAPIAvailableVersion, kibanaVersion)
121+
}
122+
if m.XPackEnabled {
91123
// Use legacy API response so we can passthru usage as-is
92124
statsHTTP.SetURI(statsHTTP.GetURI() + "&legacy=true")
93125
}
94126

95127
var settingsHTTP *helper.HTTP
96-
if ms.XPackEnabled {
128+
if m.XPackEnabled {
97129
isSettingsAPIAvailable := kibana.IsSettingsAPIAvailable(kibanaVersion)
98-
if err != nil {
99-
return nil, err
100-
}
101-
102130
if !isSettingsAPIAvailable {
103-
const errorMsg = "The %v metricset with X-Pack enabled is only supported with Kibana >= %v. You are currently running Kibana %v"
104-
return nil, fmt.Errorf(errorMsg, ms.FullyQualifiedName(), kibana.SettingsAPIAvailableVersion, kibanaVersion)
131+
const errorMsg = "the %v metricset with X-Pack enabled is only supported with Kibana >= %v. You are currently running Kibana %v"
132+
return fmt.Errorf(errorMsg, m.FullyQualifiedName(), kibana.SettingsAPIAvailableVersion, kibanaVersion)
105133
}
106134

107-
settingsHTTP, err = helper.NewHTTP(base)
135+
settingsHTTP, err = helper.NewHTTP(m.BaseMetricSet)
108136
if err != nil {
109-
return nil, err
137+
return err
110138
}
111139

112140
// HACK! We need to do this because there might be a basepath involved, so we
@@ -115,33 +143,9 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
115143
settingsHTTP.SetURI(settingsURI)
116144
}
117145

118-
return &MetricSet{
119-
ms,
120-
statsHTTP,
121-
settingsHTTP,
122-
time.Time{},
123-
kibana.IsUsageExcludable(kibanaVersion),
124-
}, nil
125-
}
126-
127-
// Fetch methods implements the data gathering and data conversion to the right format
128-
// It returns the event which is then forward to the output. In case of an error, a
129-
// descriptive error must be returned.
130-
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
131-
now := time.Now()
132-
133-
err := m.fetchStats(r, now)
134-
if err != nil {
135-
if m.XPackEnabled {
136-
m.Logger().Error(err)
137-
return nil
138-
}
139-
return err
140-
}
141-
142-
if m.XPackEnabled {
143-
m.fetchSettings(r, now)
144-
}
146+
m.statsHTTP = statsHTTP
147+
m.settingsHTTP = settingsHTTP
148+
m.isUsageExcludable = kibana.IsUsageExcludable(kibanaVersion)
145149

146150
return nil
147151
}

0 commit comments

Comments
 (0)