github.com/xmidt-org/webpa-common@v1.11.9/service/monitor/filter.go (about) 1 package monitor 2 3 import ( 4 "sort" 5 6 "github.com/xmidt-org/webpa-common/service" 7 ) 8 9 // Filter represents a preprocessing strategy for discovered service instances 10 type Filter func([]string) []string 11 12 // NopFilter does nothing. It returns the slice of instances as is. 13 func NopFilter(i []string) []string { 14 return i 15 } 16 17 // NewNormalizeFilter returns a Filter that uses service.NormalizeInstance to ensure that each instance 18 // is a valid URI with scheme and port (where applicable). The defaultScheme is used if an instance has 19 // no scheme, e.g. "localhost:8080". 20 func NewNormalizeFilter(defaultScheme string) Filter { 21 return func(original []string) []string { 22 if len(original) == 0 { 23 return original 24 } 25 26 filtered := make([]string, 0, len(original)) 27 for _, o := range original { 28 if normalized, err := service.NormalizeInstance(defaultScheme, o); err == nil { 29 filtered = append(filtered, normalized) 30 } 31 } 32 33 sort.Strings(filtered) 34 return filtered 35 } 36 } 37 38 var defaultFilter = NewNormalizeFilter(service.DefaultScheme) 39 40 // DefaultFilter returns the global default Filter instance. 41 func DefaultFilter() Filter { 42 return defaultFilter 43 }