Skip to content

Commit

Permalink
Adding version check to Logstash metricsets when xpack.enabled: true …
Browse files Browse the repository at this point in the history
…is set
  • Loading branch information
ycombinator committed Jun 27, 2019
1 parent 5cd611a commit 6a9324a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
31 changes: 31 additions & 0 deletions metricbeat/module/logstash/logstash.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/helper/elastic"
"github.com/elastic/beats/metricbeat/mb"
)

Expand Down Expand Up @@ -74,6 +75,10 @@ func validateXPackMetricsets(base mb.BaseModule) error {
// ModuleName is the name of this module.
const ModuleName = "logstash"

// PipelineGraphAPIsAvailableVersion is the version of Logstash since when its APIs
// can return pipeline graphs
var PipelineGraphAPIsAvailableVersion = common.MustNewVersion("7.3.0")

// MetricSet can be used to build other metricsets within the Logstash module.
type MetricSet struct {
mb.BaseMetricSet
Expand Down Expand Up @@ -135,6 +140,32 @@ func GetPipelines(http *helper.HTTP, resetURI string) ([]PipelineState, error) {
return pipelines, nil
}

// GetVersion returns the version of the Logstash node
func GetVersion(http *helper.HTTP, currentPath string) (*common.Version, error) {
const rootPath = "/"
content, err := fetchPath(http, currentPath, rootPath, "")
if err != nil {
return nil, err
}

var response struct {
Version *common.Version `json:"version"`
}

err = json.Unmarshal(content, &response)
if err != nil {
return nil, err
}

return response.Version, nil
}

// ArePipelineGraphAPIsAvailable returns whether Logstash APIs that returns pipeline graphs
// are available in the given version of Logstash
func ArePipelineGraphAPIsAvailable(currentLogstashVersion *common.Version) bool {
return elastic.IsFeatureAvailable(currentLogstashVersion, PipelineGraphAPIsAvailableVersion)
}

func fetchPath(http *helper.HTTP, resetURI, path string, query string) ([]byte, error) {
defer http.SetURI(resetURI)

Expand Down
19 changes: 19 additions & 0 deletions metricbeat/module/logstash/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package node

import (
"fmt"

"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
Expand Down Expand Up @@ -63,6 +65,23 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, err
}

if ms.XPack {
logstashVersion, err := logstash.GetVersion(http, nodePath)
if err != nil {
return nil, err
}

arePipelineGraphAPIsAvailable := logstash.ArePipelineGraphAPIsAvailable(logstashVersion)
if err != nil {
return nil, err
}

if !arePipelineGraphAPIsAvailable {
const errorMsg = "The %v metricset with X-Pack enabled is only supported with Logstash >= %v. You are currently running Logstash %v"
return nil, fmt.Errorf(errorMsg, ms.FullyQualifiedName(), logstash.PipelineGraphAPIsAvailableVersion, logstashVersion)
}
}

return &MetricSet{
ms,
http,
Expand Down
17 changes: 17 additions & 0 deletions metricbeat/module/logstash/node_stats/node_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package node_stats

import (
"fmt"

"github.com/elastic/beats/metricbeat/helper"

"github.com/elastic/beats/metricbeat/mb"
Expand Down Expand Up @@ -66,6 +68,21 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
}

if ms.XPack {
logstashVersion, err := logstash.GetVersion(http, nodeStatsPath)
if err != nil {
return nil, err
}

arePipelineGraphAPIsAvailable := logstash.ArePipelineGraphAPIsAvailable(logstashVersion)
if err != nil {
return nil, err
}

if !arePipelineGraphAPIsAvailable {
const errorMsg = "The %v metricset with X-Pack enabled is only supported with Logstash >= %v. You are currently running Logstash %v"
return nil, fmt.Errorf(errorMsg, ms.FullyQualifiedName(), logstash.PipelineGraphAPIsAvailableVersion, logstashVersion)
}

http.SetURI(http.GetURI() + "?vertices=true")
}

Expand Down

0 comments on commit 6a9324a

Please sign in to comment.