Skip to content

Commit

Permalink
Expose monitoring.cluster_uuid in State API (#13254)
Browse files Browse the repository at this point in the history
* Expose monitoring.cluster_uuid in State API

* Consume monitoring.cluster_uuid from State API in beat/stats metricset

* Consume monitoring.cluster_uuid from State API in beat/state metricset
  • Loading branch information
ycombinator authored Sep 10, 2019
1 parent e0c705c commit 44e77e3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
8 changes: 8 additions & 0 deletions libbeat/cmd/instance/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,14 @@ func (b *Beat) launch(settings Settings, bt beat.Creator) error {
return err
}
defer reporter.Stop()

// Expose monitoring.cluster_uuid in state API
if reporterSettings.ClusterUUID != "" {
stateRegistry := monitoring.GetNamespace("state").GetRegistry()
monitoringRegistry := stateRegistry.NewRegistry("monitoring")
clusterUUIDRegVar := monitoring.NewString(monitoringRegistry, "cluster_uuid")
clusterUUIDRegVar.Set(reporterSettings.ClusterUUID)
}
}

if b.Config.MetricLogging == nil || b.Config.MetricLogging.Enabled() {
Expand Down
3 changes: 3 additions & 0 deletions metricbeat/module/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ type Info struct {

// State construct contains the relevant data from the Beat's /state endpoint
type State struct {
Monitoring struct {
ClusterUUID string `json:"cluster_uuid"`
} `json:"monitoring"`
Output struct {
Name string `json:"name"`
} `json:"output"`
Expand Down
42 changes: 34 additions & 8 deletions metricbeat/module/beat/state/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ func eventMappingXPack(r mb.ReporterV2, m *MetricSet, info b.Info, content []byt
"timestamp": now,
}

var clusterUUID string
if isOutputES(state) {
clusterUUID = getClusterUUID(state)
if clusterUUID == "" {
// Output is ES but cluster UUID could not be determined. No point sending monitoring
// data with empty cluster UUID since it will not be associated with the correct ES
// production cluster. Log error instead.
return errors.Wrap(b.ErrClusterUUID, "could not determine cluster UUID")
clusterUUID := getMonitoringClusterUUID(state)
if clusterUUID == "" {
if isOutputES(state) {
clusterUUID = getClusterUUID(state)
if clusterUUID == "" {
// Output is ES but cluster UUID could not be determined. No point sending monitoring
// data with empty cluster UUID since it will not be associated with the correct ES
// production cluster. Log error instead.
return errors.Wrap(b.ErrClusterUUID, "could not determine cluster UUID")
}
}
}

Expand Down Expand Up @@ -141,3 +143,27 @@ func isOutputES(state map[string]interface{}) bool {

return name == "elasticsearch"
}

func getMonitoringClusterUUID(state map[string]interface{}) string {
m, exists := state["monitoring"]
if !exists {
return ""
}

monitoring, ok := m.(map[string]interface{})
if !ok {
return ""
}

c, exists := monitoring["cluster_uuid"]
if !exists {
return ""
}

clusterUUID, ok := c.(string)
if !ok {
return ""
}

return clusterUUID
}
7 changes: 6 additions & 1 deletion metricbeat/module/beat/stats/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,16 @@ func (m *MetricSet) getClusterUUID() (string, error) {
return "", errors.Wrap(err, "could not get state information")
}

clusterUUID := state.Monitoring.ClusterUUID
if clusterUUID != "" {
return clusterUUID, nil
}

if state.Output.Name != "elasticsearch" {
return "", nil
}

clusterUUID := state.Outputs.Elasticsearch.ClusterUUID
clusterUUID = state.Outputs.Elasticsearch.ClusterUUID
if clusterUUID == "" {
// Output is ES but cluster UUID could not be determined. No point sending monitoring
// data with empty cluster UUID since it will not be associated with the correct ES
Expand Down

0 comments on commit 44e77e3

Please sign in to comment.