Exemple #1
0
// GlobPatternPrefix exracts the longest possible fixed-string
// prefix. Ex. `statsd.foo.*.bar` should return `statsd.foo.`.
func GlobPatternPrefix(pattern string) string {
	conf := glob.Default()
	conf.Separator = '.'

	// Scan the pattern to find the longest prefix
	var i = 0
	for i = 0; i < len(pattern); i++ {
		switch pattern[i] {
		case '\\':
			// Trailing backslash
			if i+1 < len(pattern) {
				i++
			}
		case conf.Range:
			return pattern[:i]
		case conf.RangeEnd:
			return pattern[:i]
		case conf.Star:
			return pattern[:i]
		case conf.Quest:
			return pattern[:i]
		}
	}

	return pattern
}
Exemple #2
0
// Apply glob pattern to metrics from a backend
func GlobMetricsAsList(pattern string, backend Backend) ([]string, error) {
	conf := glob.Default()
	conf.Separator = '.'
	globber, err := glob.New(conf)
	if err != nil {
		return nil, err
	}

	list := make([]string, 0)
	out := make(chan string)

	backend.GetMetricsList(out)
	for name := range out {
		match, err := globber.Match(pattern, name)
		if err != nil {
			return nil, err
		}

		if match {
			list = append(list, name)
		}
	}
	return list, nil
}