diff --git a/cmd/collectors/commonutils.go b/cmd/collectors/commonutils.go index a23068a01..8214f24d8 100644 --- a/cmd/collectors/commonutils.go +++ b/cmd/collectors/commonutils.go @@ -88,11 +88,11 @@ func InvokeRestCall(client *rest.Client, href string) ([]gjson.Result, error) { } func TruncateURL(href string) string { - indexOfQuestionMark := strings.Index(href, "?") - if indexOfQuestionMark == -1 { + before, _, found := strings.Cut(href, "?") + if !found { return href } - return href[:indexOfQuestionMark] + "..." + return before + "..." } func GetClusterTime(client *rest.Client, returnTimeOut *int, logger *slog.Logger) (time.Time, error) { diff --git a/cmd/collectors/restperf/plugins/disk/disk.go b/cmd/collectors/restperf/plugins/disk/disk.go index f449ebc5b..87d51b331 100644 --- a/cmd/collectors/restperf/plugins/disk/disk.go +++ b/cmd/collectors/restperf/plugins/disk/disk.go @@ -762,9 +762,8 @@ func (d *Disk) calculateEnvironmentMetrics(data *matrix.Matrix) { shelfEnvironmentMetricMap := make(map[string]*shelfEnvironmentMetric) for _, o := range d.shelfData { for k, instance := range o.GetInstances() { - firstInd := strings.Index(k, "#") + iKey, _, _ := strings.Cut(k, "#") lastInd := strings.LastIndex(k, "#") - iKey := k[:firstInd] iKey2 := k[lastInd+1:] if _, ok := shelfEnvironmentMetricMap[iKey]; !ok { shelfEnvironmentMetricMap[iKey] = &shelfEnvironmentMetric{key: iKey, ambientTemperature: []float64{}, nonAmbientTemperature: []float64{}, fanSpeed: []float64{}} diff --git a/cmd/tools/template/template.go b/cmd/tools/template/template.go index fe5ac22bd..d8e91b85b 100644 --- a/cmd/tools/template/template.go +++ b/cmd/tools/template/template.go @@ -281,9 +281,9 @@ func newMetric(n ast.Node, parents []string) Metric { } func trimComment(text string) string { - lastSink := strings.Index(text, "#") - if lastSink > -1 { - return strings.TrimSpace(text[:lastSink]) + before, _, found := strings.Cut(text, "#") + if found { + return strings.TrimSpace(before) } return strings.TrimSpace(text) } diff --git a/cmd/tools/template/template_test.go b/cmd/tools/template/template_test.go index b3f35b7b5..616a6a65f 100644 --- a/cmd/tools/template/template_test.go +++ b/cmd/tools/template/template_test.go @@ -591,9 +591,9 @@ func visitTemplates(t *testing.T, eachTemplate func(path string, model Model), d func collectorPath(path string) string { const conf string = "conf/" - index := strings.Index(path, conf) - if index > 0 { - splits := strings.Split(path[index+len(conf):], "/") + before, after, found := strings.Cut(path, conf) + if found && before != "" { + splits := strings.Split(after, "/") return splits[0] } return path @@ -601,9 +601,9 @@ func collectorPath(path string) string { func shortPath(path string) string { const conf string = "conf/" - index := strings.Index(path, conf) - if index > 0 { - return path[index+len(conf):] + before, after, found := strings.Cut(path, conf) + if found && before != "" { + return after } return path } diff --git a/go.sum b/go.sum index a7d25db94..971b08354 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= -github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/goccy/go-yaml v1.19.0 h1:EmkZ9RIsX+Uq4DYFowegAuJo8+xdX3T/2dwNPXbxEYE= github.com/goccy/go-yaml v1.19.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= @@ -25,16 +23,10 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/zekroTJA/timedmap/v2 v2.0.0 h1:Bo9oq8AExd0GuDFbcPXm3xoidUAtrnNsZN1d1Hc3PvY= github.com/zekroTJA/timedmap/v2 v2.0.0/go.mod h1:xHDLg687zASqLBJqoysF+WORHxL/kYNphVD36CRJxhM= -golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ= -golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= -golang.org/x/term v0.36.0 h1:zMPR+aF8gfksFprF/Nc/rd1wRS1EI6nDBGyWAvDzx2Q= -golang.org/x/term v0.36.0/go.mod h1:Qu394IJq6V6dCBRgwqshf3mPF85AqzYEzofzRdZkWss= golang.org/x/term v0.37.0 h1:8EGAD0qCmHYZg6J17DvsMy9/wJ7/D/4pV/wfnld5lTU= golang.org/x/term v0.37.0/go.mod h1:5pB4lxRNYYVZuTLmy8oR2BH8dflOR+IbTYFD8fi3254= -golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= -golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= golang.org/x/text v0.31.0 h1:aC8ghyu4JhP8VojJ2lEHBnochRno1sgL6nEi9WGFGMM= golang.org/x/text v0.31.0/go.mod h1:tKRAlv61yKIjGGHX/4tP1LTbc13YSec1pxVEWXzfoeM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/integration/test/metric_test.go b/integration/test/metric_test.go index e0797fde2..2b2a5823d 100644 --- a/integration/test/metric_test.go +++ b/integration/test/metric_test.go @@ -60,8 +60,16 @@ func TestPollerMetrics(t *testing.T) { if strings.HasPrefix(row, "#") { continue } - openBracket := strings.Index(row, "{") - firstSpace := strings.Index(row, " ") + before, _, foundBracket := strings.Cut(row, "{") + openBracket := -1 + if foundBracket { + openBracket = len(before) + } + beforeSpace, _, foundSpace := strings.Cut(row, " ") + firstSpace := -1 + if foundSpace { + firstSpace = len(beforeSpace) + } if openBracket == -1 { // this means the metric has this form // metric_without_labels 12.47 diff --git a/pkg/tree/tree.go b/pkg/tree/tree.go index 53a86d809..ffc516a82 100644 --- a/pkg/tree/tree.go +++ b/pkg/tree/tree.go @@ -9,7 +9,7 @@ import ( "github.com/goccy/go-yaml/parser" "github.com/netapp/harvest/v2/pkg/errs" "github.com/netapp/harvest/v2/pkg/tree/node" - "github.com/netapp/harvest/v2/pkg/tree/xml" + "github.com/netapp/harvest/v2/pkg/tree/treexml" "os" ) @@ -82,11 +82,11 @@ func isScalar(n ast.Node) bool { } func LoadXML(data []byte) (*node.Node, error) { - return xml.Load(data) + return treexml.Load(data) } func DumpXML(n *node.Node) ([]byte, error) { - return xml.Dump(n) + return treexml.Dump(n) } func ImportXML(filepath string) (*node.Node, error) { diff --git a/pkg/tree/xml/xml.go b/pkg/tree/treexml/xml.go similarity index 96% rename from pkg/tree/xml/xml.go rename to pkg/tree/treexml/xml.go index a213cd758..aa0d57225 100644 --- a/pkg/tree/xml/xml.go +++ b/pkg/tree/treexml/xml.go @@ -2,7 +2,7 @@ * Copyright NetApp Inc, 2021 All rights reserved */ -package xml +package treexml import ( "bytes"