mirror of
https://github.com/prometheus/prometheus.git
synced 2025-08-06 22:27:17 +02:00
refactor: use slices.Contains to simplify code
Signed-off-by: hardlydearly <799511800@qq.com>
This commit is contained in:
parent
59874fd89c
commit
ba4b058b7a
@ -30,6 +30,7 @@ import (
|
|||||||
goregexp "regexp" //nolint:depguard // The Prometheus client library requires us to pass a regexp from this package.
|
goregexp "regexp" //nolint:depguard // The Prometheus client library requires us to pass a regexp from this package.
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
@ -1921,11 +1922,9 @@ func (p *rwProtoMsgFlagParser) Set(opt string) error {
|
|||||||
if err := t.Validate(); err != nil {
|
if err := t.Validate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
for _, prev := range *p.msgs {
|
if slices.Contains(*p.msgs, t) {
|
||||||
if prev == t {
|
|
||||||
return fmt.Errorf("duplicated %v flag value, got %v already", t, *p.msgs)
|
return fmt.Errorf("duplicated %v flag value, got %v already", t, *p.msgs)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*p.msgs = append(*p.msgs, t)
|
*p.msgs = append(*p.msgs, t)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@ -1109,13 +1110,11 @@ func (v *AlertmanagerAPIVersion) UnmarshalYAML(unmarshal func(interface{}) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, supportedVersion := range SupportedAlertmanagerAPIVersions {
|
if !slices.Contains(SupportedAlertmanagerAPIVersions, *v) {
|
||||||
if *v == supportedVersion {
|
return fmt.Errorf("expected Alertmanager api version to be one of %v but got %v", SupportedAlertmanagerAPIVersions, *v)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("expected Alertmanager api version to be one of %v but got %v", SupportedAlertmanagerAPIVersions, *v)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -210,18 +211,9 @@ func (c *SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
|
|||||||
if _, ok := allowedSelectors[c.Role]; !ok {
|
if _, ok := allowedSelectors[c.Role]; !ok {
|
||||||
return fmt.Errorf("invalid role: %q, expecting one of: pod, service, endpoints, endpointslice, node or ingress", c.Role)
|
return fmt.Errorf("invalid role: %q, expecting one of: pod, service, endpoints, endpointslice, node or ingress", c.Role)
|
||||||
}
|
}
|
||||||
var allowed bool
|
if !slices.Contains(allowedSelectors[c.Role], string(selector.Role)) {
|
||||||
for _, role := range allowedSelectors[c.Role] {
|
|
||||||
if role == string(selector.Role) {
|
|
||||||
allowed = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !allowed {
|
|
||||||
return fmt.Errorf("%s role supports only %s selectors", c.Role, strings.Join(allowedSelectors[c.Role], ", "))
|
return fmt.Errorf("%s role supports only %s selectors", c.Role, strings.Join(allowedSelectors[c.Role], ", "))
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := fields.ParseSelector(selector.Field)
|
_, err := fields.ParseSelector(selector.Field)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -167,11 +167,9 @@ func (b *Builder) Del(ns ...string) *Builder {
|
|||||||
// Keep removes all labels from the base except those with the given names.
|
// Keep removes all labels from the base except those with the given names.
|
||||||
func (b *Builder) Keep(ns ...string) *Builder {
|
func (b *Builder) Keep(ns ...string) *Builder {
|
||||||
b.base.Range(func(l Label) {
|
b.base.Range(func(l Label) {
|
||||||
for _, n := range ns {
|
if slices.Contains(ns, l.Name) {
|
||||||
if l.Name == n {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
b.del = append(b.del, l.Name)
|
b.del = append(b.del, l.Name)
|
||||||
})
|
})
|
||||||
return b
|
return b
|
||||||
|
@ -95,12 +95,7 @@ func (m *FastRegexMatcher) compileMatchStringFunction() func(string) bool {
|
|||||||
|
|
||||||
return func(s string) bool {
|
return func(s string) bool {
|
||||||
if len(m.setMatches) != 0 {
|
if len(m.setMatches) != 0 {
|
||||||
for _, match := range m.setMatches {
|
return slices.Contains(m.setMatches, s)
|
||||||
if match == s {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
if m.prefix != "" && !strings.HasPrefix(s, m.prefix) {
|
if m.prefix != "" && !strings.HasPrefix(s, m.prefix) {
|
||||||
return false
|
return false
|
||||||
@ -771,18 +766,13 @@ func (m *equalMultiStringSliceMatcher) setMatches() []string {
|
|||||||
|
|
||||||
func (m *equalMultiStringSliceMatcher) Matches(s string) bool {
|
func (m *equalMultiStringSliceMatcher) Matches(s string) bool {
|
||||||
if m.caseSensitive {
|
if m.caseSensitive {
|
||||||
for _, v := range m.values {
|
return slices.Contains(m.values, s)
|
||||||
if s == v {
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
for _, v := range m.values {
|
for _, v := range m.values {
|
||||||
if strings.EqualFold(s, v) {
|
if strings.EqualFold(s, v) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,6 +20,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
@ -1008,11 +1009,8 @@ func TestMetricsUpdate(t *testing.T) {
|
|||||||
var metrics int
|
var metrics int
|
||||||
for _, m := range ms {
|
for _, m := range ms {
|
||||||
s := m.GetName()
|
s := m.GetName()
|
||||||
for _, n := range metricNames {
|
if slices.Contains(metricNames, s) {
|
||||||
if s == n {
|
|
||||||
metrics += len(m.Metric)
|
metrics += len(m.Metric)
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return metrics
|
return metrics
|
||||||
|
@ -599,11 +599,9 @@ func Intersect(its ...Postings) Postings {
|
|||||||
if len(its) == 1 {
|
if len(its) == 1 {
|
||||||
return its[0]
|
return its[0]
|
||||||
}
|
}
|
||||||
for _, p := range its {
|
if slices.Contains(its, EmptyPostings()) {
|
||||||
if p == EmptyPostings() {
|
|
||||||
return EmptyPostings()
|
return EmptyPostings()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return newIntersectPostings(its...)
|
return newIntersectPostings(its...)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user