Skip to content

Commit

Permalink
Using json.Unmarshaler instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jul 7, 2020
1 parent 987c583 commit 87b73dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
30 changes: 21 additions & 9 deletions metricbeat/module/elasticsearch/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,26 @@ func GetXPack(http *helper.HTTP, resetURI string) (XPack, error) {
return xpack, err
}

type boolStr bool

func (b *boolStr) UnmarshalJSON(raw []byte) error {
var bs string
err := json.Unmarshal(raw, &bs)
if err != nil {
return err
}

bv, err := strconv.ParseBool(bs)
if err != nil {
return err
}

*b = boolStr(bv)
return nil
}

type IndexSettings struct {
Hidden bool
Hidden boolStr
}

// GetIndicesSettings returns a map of index names to their settings.
Expand All @@ -376,9 +394,7 @@ func GetIndicesSettings(http *helper.HTTP, resetURI string) (map[string]IndexSet

var resp map[string]struct {
Settings struct {
Index struct {
Hidden string `json:"hidden"`
} `json:"index"`
Index IndexSettings `json:"index"`
} `json:"settings"`
}

Expand All @@ -389,11 +405,7 @@ func GetIndicesSettings(http *helper.HTTP, resetURI string) (map[string]IndexSet

ret := make(map[string]IndexSettings, len(resp))
for index, settings := range resp {
isHidden, err := strconv.ParseBool(settings.Settings.Index.Hidden)
if err != nil {
return nil, errors.Wrap(err, "could not parse hidden setting as boolean")
}
ret[index] = IndexSettings{Hidden: isHidden}
ret[index] = settings.Settings.Index
}

return ret, nil
Expand Down
2 changes: 1 addition & 1 deletion metricbeat/module/elasticsearch/index/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func eventsMappingXPack(r mb.ReporterV2, m *MetricSet, info elasticsearch.Info,

settings, exists := indicesSettings[name]
if exists {
idx.Hidden = settings.Hidden
idx.Hidden = bool(settings.Hidden)
}

err = addClusterStateFields(&idx, clusterState)
Expand Down

0 comments on commit 87b73dd

Please sign in to comment.