github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/logql/log/util.go (about)

     1  package log
     2  
     3  import (
     4  	"strings"
     5  )
     6  
     7  func uniqueString(s []string) []string {
     8  	unique := make(map[string]bool, len(s))
     9  	us := make([]string, 0, len(s))
    10  	for _, elem := range s {
    11  		if len(elem) != 0 {
    12  			if !unique[elem] {
    13  				us = append(us, elem)
    14  				unique[elem] = true
    15  			}
    16  		}
    17  	}
    18  	return us
    19  }
    20  
    21  func sanitizeLabelKey(key string, isPrefix bool) string {
    22  	if len(key) == 0 {
    23  		return key
    24  	}
    25  	key = strings.TrimSpace(key)
    26  	if len(key) == 0 {
    27  		return key
    28  	}
    29  	if isPrefix && key[0] >= '0' && key[0] <= '9' {
    30  		key = "_" + key
    31  	}
    32  	return strings.Map(func(r rune) rune {
    33  		if (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || r == '_' || (r >= '0' && r <= '9') {
    34  			return r
    35  		}
    36  		return '_'
    37  	}, key)
    38  }