Skip to content

Commit

Permalink
fix: changed warn to error in ems for key/labels (#1981)
Browse files Browse the repository at this point in the history
* fix: changed warn to error in ems for key/labels

* fix: change revert for hyphen

* fix: sorted bookendkeys

* fix: handle review comment
  • Loading branch information
Hardikl committed Apr 25, 2023
1 parent a46be25 commit e73cb2e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
6 changes: 3 additions & 3 deletions cmd/collectors/ems/ems.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (e *Ems) InitCache() error {

// check if name is present in template
if line.GetChildContentS("name") == "" {
e.Logger.Warn().Msg("Missing event name")
e.Logger.Error().Msg("Missing event name")
continue
}

Expand Down Expand Up @@ -509,7 +509,7 @@ func (e *Ems) HandleResults(result []gjson.Result, prop map[string][]*emsProp) (
messageName := instanceData.Get("message.name")
// verify if message name exists in ONTAP response
if !messageName.Exists() {
e.Logger.Warn().Msg("skip instance, missing message name")
e.Logger.Error().Msg("skip instance, missing message name")
continue
}
msgName := messageName.String()
Expand Down Expand Up @@ -686,7 +686,7 @@ func (e *Ems) getInstanceKeys(p *emsProp, instanceData gjson.Result) string {
if value.Exists() {
instanceKey += Hyphen + value.String()
} else {
e.Logger.Warn().Str("key", k).Msg("skip instance, missing key")
e.Logger.Error().Str("key", k).Msg("skip instance, missing key")
break
}
}
Expand Down
14 changes: 9 additions & 5 deletions cmd/collectors/ems/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ func (e *Ems) ParseDefaults(prop *emsProp) {
func (e *Ems) ParseExports(counter *node.Node, prop *emsProp) {
var (
display, name, key string
bookendKeys []string
)
bookendKeys := make(map[string]string)

for _, c := range counter.GetAllChildContentS() {
if c != "" {
Expand All @@ -91,7 +91,7 @@ func (e *Ems) ParseExports(counter *node.Node, prop *emsProp) {

if key == "key" {
// only for bookend EMS
bookendKeys = append(bookendKeys, name)
bookendKeys[display] = name
e.Logger.Debug().
Str("name", name).
Str("display", display).
Expand All @@ -102,7 +102,11 @@ func (e *Ems) ParseExports(counter *node.Node, prop *emsProp) {

// For bookend case, instanceKeys are replaced with bookendKeys
if len(bookendKeys) > 0 {
prop.InstanceKeys = append(prop.InstanceKeys, bookendKeys...)
sortedBookendKeys := util.GetSortedKeys(bookendKeys)
// Append instance keys to ems prop
for _, k := range sortedBookendKeys {
prop.InstanceKeys = append(prop.InstanceKeys, bookendKeys[k])
}
}
}

Expand All @@ -116,7 +120,7 @@ func (e *Ems) ParseResolveEms(resolveEvent *node.Node, issueEmsProp emsProp) {

// check if resolvedby is present in template
if resolveEmsName = resolveEvent.GetChildContentS("name"); resolveEmsName == "" {
e.Logger.Warn().Msg("Missing resolving event name")
e.Logger.Error().Msg("Missing resolving event name")
return
}
prop.Name = resolveEmsName
Expand All @@ -132,7 +136,7 @@ func (e *Ems) ParseResolveEms(resolveEvent *node.Node, issueEmsProp emsProp) {
} else {
// If bookendKey is missing in IssueEms, the default bookendKey is index of IssueEMs
prop.InstanceKeys = issueEmsProp.InstanceKeys[0:1]
e.Logger.Warn().Str("Ems name", issueEmsProp.Name).Msg("Missing bookend keys")
e.Logger.Error().Str("Ems name", issueEmsProp.Name).Msg("Missing bookend keys")
}
} else {
e.ParseExports(resolveKey, &prop)
Expand Down
7 changes: 1 addition & 6 deletions cmd/collectors/rest/templating.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/netapp/harvest/v2/pkg/tree/node"
"github.com/netapp/harvest/v2/pkg/util"
"regexp"
"sort"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -194,11 +193,7 @@ func (r *Rest) ParseRestCounters(counter *node.Node, prop *prop) {

// populate prop.instanceKeys
// sort keys by display name. This is needed to match counter and endpoints keys
var keys []string
for k := range instanceKeys {
keys = append(keys, k)
}
sort.Strings(keys)
keys := util.GetSortedKeys(instanceKeys)

// Append instance keys to prop
for _, k := range keys {
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"net/url"
"os"
"regexp"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -405,3 +406,12 @@ func HasDuplicates(slice []string) bool {

return false
}

func GetSortedKeys(m map[string]string) []string {
var sortedKeys []string
for k := range m {
sortedKeys = append(sortedKeys, k)
}
sort.Strings(sortedKeys)
return sortedKeys
}

0 comments on commit e73cb2e

Please sign in to comment.