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

Move HTTP calls to Kibana from New() to Fetch() #15270

Merged
merged 3 commits into from
Dec 31, 2019
Merged
Changes from 2 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
97 changes: 51 additions & 46 deletions metricbeat/module/kibana/stats/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,46 +67,74 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

statsHTTP, err := helper.NewHTTP(base)
return &MetricSet{
MetricSet: ms,
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
err := m.init()
if err != nil {
return nil, err
if m.XPackEnabled {
m.Logger().Error(err)
return nil
}
return err
}

kibanaVersion, err := kibana.GetVersion(statsHTTP, statsPath)
now := time.Now()

err = m.fetchStats(r, now)
if err != nil {
return nil, err
if m.XPackEnabled {
m.Logger().Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ycombinator Do you really need to have a special handling of erros when xpack is enabled? My understanding is that the error will be returned by the Fetch() method and will be logged upstream?

Copy link
Contributor Author

@ycombinator ycombinator Dec 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this change was intentionally made in #12265.

In fact, I'm glad you brought this up — I need to add this same special handling around https://github.com/elastic/beats/pull/15270/files#diff-8710596f0decaae3ca552cedea1eb0a0R80 too!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good story about the different indices :)

return nil
}
return err
}

isStatsAPIAvailable := kibana.IsStatsAPIAvailable(kibanaVersion)
if m.XPackEnabled {
m.fetchSettings(r, now)
}

return nil
}

func (m *MetricSet) init() error {
statsHTTP, err := helper.NewHTTP(m.BaseMetricSet)
if err != nil {
return nil, err
return err
}

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

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

var settingsHTTP *helper.HTTP
if ms.XPackEnabled {
if m.XPackEnabled {
isSettingsAPIAvailable := kibana.IsSettingsAPIAvailable(kibanaVersion)
if err != nil {
return nil, err
}

if !isSettingsAPIAvailable {
const errorMsg = "The %v metricset with X-Pack enabled is only supported with Kibana >= %v. You are currently running Kibana %v"
return nil, fmt.Errorf(errorMsg, ms.FullyQualifiedName(), kibana.SettingsAPIAvailableVersion, kibanaVersion)
const errorMsg = "the %v metricset with X-Pack enabled is only supported with Kibana >= %v. You are currently running Kibana %v"
return fmt.Errorf(errorMsg, m.FullyQualifiedName(), kibana.SettingsAPIAvailableVersion, kibanaVersion)
}

settingsHTTP, err = helper.NewHTTP(base)
settingsHTTP, err = helper.NewHTTP(m.BaseMetricSet)
if err != nil {
return nil, err
return err
}

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

return &MetricSet{
ms,
statsHTTP,
settingsHTTP,
time.Time{},
kibana.IsUsageExcludable(kibanaVersion),
}, nil
}

// Fetch methods implements the data gathering and data conversion to the right format
// It returns the event which is then forward to the output. In case of an error, a
// descriptive error must be returned.
func (m *MetricSet) Fetch(r mb.ReporterV2) error {
now := time.Now()

err := m.fetchStats(r, now)
if err != nil {
if m.XPackEnabled {
m.Logger().Error(err)
return nil
}
return err
}

if m.XPackEnabled {
m.fetchSettings(r, now)
}
m.statsHTTP = statsHTTP
m.settingsHTTP = settingsHTTP
m.isUsageExcludable = kibana.IsUsageExcludable(kibanaVersion)
m.usageLastCollectedOn = time.Time{}

return nil
}
Expand Down