diff --git a/app/vmalert/config/types.go b/app/vmalert/config/types.go index 811d42dc15..b0f3d74f63 100644 --- a/app/vmalert/config/types.go +++ b/app/vmalert/config/types.go @@ -2,6 +2,7 @@ package config import ( "fmt" + "slices" "strings" "github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage" @@ -80,12 +81,8 @@ func (t *Type) ValidateExpr(expr string) error { if err != nil { return fmt.Errorf("cannot obtain labels from LogsQL expr: %q, err: %w", expr, err) } - for i := range labels { - // VictoriaLogs inserts `_time` field as a label in result when query with `stats by (_time:step)`, - // making the result meaningless and may lead to cardinality issues. - if labels[i] == "_time" { - return fmt.Errorf("bad LogsQL expr: %q, err: cannot contain time buckets stats pipe `stats by (_time:step)`", expr) - } + if slices.Contains(labels, "_time") { + return fmt.Errorf("bad LogsQL expr: %q, err: cannot contain time buckets stats pipe `stats by (_time:step)`", expr) } default: return fmt.Errorf("unknown datasource type=%q", t.Name) diff --git a/app/vmselect/netstorage/netstorage.go b/app/vmselect/netstorage/netstorage.go index 24b4fb018a..2ae25fc073 100644 --- a/app/vmselect/netstorage/netstorage.go +++ b/app/vmselect/netstorage/netstorage.go @@ -6,6 +6,7 @@ import ( "flag" "fmt" "math" + "slices" "sort" "sync" "sync/atomic" @@ -832,12 +833,7 @@ func GraphiteTags(qt *querytracer.Tracer, filter string, limit int, deadline sea } func hasString(a []string, s string) bool { - for _, x := range a { - if x == s { - return true - } - } - return false + return slices.Contains(a, s) } // LabelValues returns label values matching the given labelName and sq until the given deadline. diff --git a/app/vmselect/prometheus/prometheus.go b/app/vmselect/prometheus/prometheus.go index f4930dcf5f..2ead02bd3d 100644 --- a/app/vmselect/prometheus/prometheus.go +++ b/app/vmselect/prometheus/prometheus.go @@ -6,6 +6,7 @@ import ( "math" "net/http" "runtime" + "slices" "strconv" "strings" "sync" @@ -1004,14 +1005,7 @@ func removeEmptyValuesAndTimeseries(tss []netstorage.Result) []netstorage.Result dst := tss[:0] for i := range tss { ts := &tss[i] - hasNaNs := false - for _, v := range ts.Values { - if math.IsNaN(v) { - hasNaNs = true - break - } - } - if !hasNaNs { + if !slices.ContainsFunc(ts.Values, math.IsNaN) { // Fast path: nothing to remove. if len(ts.Values) > 0 { dst = append(dst, *ts) diff --git a/app/vmselect/promql/eval.go b/app/vmselect/promql/eval.go index 95a454419f..b36fd6dca1 100644 --- a/app/vmselect/promql/eval.go +++ b/app/vmselect/promql/eval.go @@ -5,6 +5,7 @@ import ( "fmt" "math" "regexp" + "slices" "sort" "strings" "sync" @@ -1935,14 +1936,7 @@ func dropStaleNaNs(funcName string, values []float64, timestamps []int64) ([]flo return values, timestamps } // Remove Prometheus staleness marks, so non-default rollup functions don't hit NaN values. - hasStaleSamples := false - for _, v := range values { - if decimal.IsStaleNaN(v) { - hasStaleSamples = true - break - } - } - if !hasStaleSamples { + if !slices.ContainsFunc(values, decimal.IsStaleNaN) { // Fast path: values have no Prometheus staleness marks. return values, timestamps } diff --git a/lib/flagutil/usage.go b/lib/flagutil/usage.go index 3ddf1f77b8..705c7ff162 100644 --- a/lib/flagutil/usage.go +++ b/lib/flagutil/usage.go @@ -4,6 +4,7 @@ import ( "flag" "fmt" "os" + "slices" "strings" ) @@ -19,12 +20,7 @@ func Usage(s string) { } func hasHelpFlag(args []string) bool { - for _, arg := range args { - if isHelpArg(arg) { - return true - } - } - return false + return slices.ContainsFunc(args, isHelpArg) } func isHelpArg(arg string) bool { diff --git a/lib/promscrape/discovery/consul/watch.go b/lib/promscrape/discovery/consul/watch.go index 5b951fe470..6547f6ed23 100644 --- a/lib/promscrape/discovery/consul/watch.go +++ b/lib/promscrape/discovery/consul/watch.go @@ -7,6 +7,7 @@ import ( "flag" "fmt" "net/url" + "slices" "strings" "sync" "time" @@ -327,14 +328,7 @@ func shouldCollectServiceByTags(filterTags, tags []string) bool { return true } for _, filterTag := range filterTags { - hasTag := false - for _, tag := range tags { - if tag == filterTag { - hasTag = true - break - } - } - if !hasTag { + if !slices.Contains(tags, filterTag) { return false } } diff --git a/lib/promscrape/discovery/kubernetes/endpoints.go b/lib/promscrape/discovery/kubernetes/endpoints.go index f3dd15a649..29b722f61d 100644 --- a/lib/promscrape/discovery/kubernetes/endpoints.go +++ b/lib/promscrape/discovery/kubernetes/endpoints.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "slices" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutil" @@ -116,12 +117,7 @@ func (eps *Endpoints) getTargetLabels(gw *groupWatcher) []*promutil.Labels { // Append labels for skipped ports on seen pods. portSeen := func(port int, ports []int) bool { - for _, p := range ports { - if p == port { - return true - } - } - return false + return slices.Contains(ports, port) } appendPodMetadata := func(p *Pod, c *Container, seen []int, isInit bool) { for _, cp := range c.Ports { diff --git a/lib/promscrape/discovery/kubernetes/endpointslice.go b/lib/promscrape/discovery/kubernetes/endpointslice.go index eb4fb42593..8673bc67e8 100644 --- a/lib/promscrape/discovery/kubernetes/endpointslice.go +++ b/lib/promscrape/discovery/kubernetes/endpointslice.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "io" + "slices" "strconv" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promscrape/discoveryutil" @@ -66,12 +67,7 @@ func (eps *EndpointSlice) getTargetLabels(gw *groupWatcher) []*promutil.Labels { // Append labels for skipped ports on seen pods. portSeen := func(port int, ports []int) bool { - for _, p := range ports { - if p == port { - return true - } - } - return false + return slices.Contains(ports, port) } appendPodMetadata := func(p *Pod, c *Container, seen []int, isInit bool) { for _, cp := range c.Ports { diff --git a/lib/proxy/proxy.go b/lib/proxy/proxy.go index 2126b56d94..04c79eb30a 100644 --- a/lib/proxy/proxy.go +++ b/lib/proxy/proxy.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "slices" "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" "github.com/VictoriaMetrics/VictoriaMetrics/lib/promauth" @@ -14,12 +15,7 @@ import ( var validURLSchemes = []string{"http", "https", "socks5", "tls+socks5"} func isURLSchemeValid(scheme string) bool { - for _, vs := range validURLSchemes { - if scheme == vs { - return true - } - } - return false + return slices.Contains(validURLSchemes, scheme) } // URL implements YAML.Marshaler and yaml.Unmarshaler interfaces for url.URL. diff --git a/lib/regexutil/promregex.go b/lib/regexutil/promregex.go index 8907547a74..e977ef08c1 100644 --- a/lib/regexutil/promregex.go +++ b/lib/regexutil/promregex.go @@ -3,6 +3,7 @@ package regexutil import ( "regexp" "regexp/syntax" + "slices" "strings" "github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil" @@ -118,12 +119,7 @@ func (pr *PromRegex) MatchString(s string) bool { if len(pr.orValues) > 0 { // Fast path - pr contains only alternate strings such as 'foo|bar|baz' - for _, v := range pr.orValues { - if s == v { - return true - } - } - return false + return slices.Contains(pr.orValues, s) } // Fall back to slow path by matching the original regexp. diff --git a/lib/storage/index_db_test.go b/lib/storage/index_db_test.go index 38fbb6b1c5..2d10eeac94 100644 --- a/lib/storage/index_db_test.go +++ b/lib/storage/index_db_test.go @@ -922,12 +922,7 @@ func testIndexDBCheckTSIDByName(db *indexDB, mns []MetricName, tsids []TSID, tim } func testHasTSID(tsids []TSID, tsid *TSID) bool { - for i := range tsids { - if tsids[i] == *tsid { - return true - } - } - return false + return slices.Contains(tsids, *tsid) } func TestGetRegexpForGraphiteNodeQuery(t *testing.T) { diff --git a/lib/storage/metric_name.go b/lib/storage/metric_name.go index 00b3a5368d..d651c2912e 100644 --- a/lib/storage/metric_name.go +++ b/lib/storage/metric_name.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "runtime" + "slices" "sort" "strings" "sync" @@ -373,12 +374,7 @@ func (mn *MetricName) setAllTags(prefix string, skipTags []string, src *MetricNa } func containsString(a []string, s string) bool { - for _, x := range a { - if x == s { - return true - } - } - return false + return slices.Contains(a, s) } func hasTag(tags []string, key []byte) bool { diff --git a/lib/storage/storage_test.go b/lib/storage/storage_test.go index fffa83d178..29633fb661 100644 --- a/lib/storage/storage_test.go +++ b/lib/storage/storage_test.go @@ -724,12 +724,7 @@ func checkLabelNames(lns []string, lnsExpected map[string]bool) error { return fmt.Errorf("unexpected number of label names found; got %d; want at least %d; lns=%q, lnsExpected=%v", len(lns), len(lnsExpected), lns, lnsExpected) } hasItem := func(s string, lns []string) bool { - for _, labelName := range lns { - if s == labelName { - return true - } - } - return false + return slices.Contains(lns, s) } for labelName := range lnsExpected { if !hasItem(labelName, lns) { diff --git a/lib/uint64set/uint64set.go b/lib/uint64set/uint64set.go index 5112129ac3..bc307e27ed 100644 --- a/lib/uint64set/uint64set.go +++ b/lib/uint64set/uint64set.go @@ -2,6 +2,7 @@ package uint64set import ( "math/bits" + "slices" "sort" "sync" "sync/atomic" @@ -891,12 +892,7 @@ func (b *bucket16) has(x uint16) bool { } func (b *bucket16) hasInSmallPool(x uint16) bool { - for _, v := range b.smallPool[:b.smallPoolLen] { - if v == x { - return true - } - } - return false + return slices.Contains(b.smallPool[:b.smallPoolLen], x) } func (b *bucket16) del(x uint16) bool {