github.com/rudderlabs/rudder-go-kit@v0.30.0/stats/tags.go (about)

     1  package stats
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  
     7  	"go.opentelemetry.io/otel/attribute"
     8  )
     9  
    10  // Tags is a map of key value pairs
    11  type Tags map[string]string
    12  
    13  // Strings returns all key value pairs as an ordered list of strings, sorted by increasing key order
    14  func (t Tags) Strings() []string {
    15  	if len(t) == 0 {
    16  		return nil
    17  	}
    18  	res := make([]string, 0, len(t)*2)
    19  	// sorted by tag name (!important for consistent map iteration order)
    20  	tagNames := make([]string, 0, len(t))
    21  	for n := range t {
    22  		tagNames = append(tagNames, n)
    23  	}
    24  	sort.Strings(tagNames)
    25  	for _, tagName := range tagNames {
    26  		tagVal := t[tagName]
    27  		res = append(res, strings.ReplaceAll(tagName, ":", "-"), strings.ReplaceAll(tagVal, ":", "-"))
    28  	}
    29  	return res
    30  }
    31  
    32  // String returns all key value pairs as a single string, separated by commas, sorted by increasing key order
    33  func (t Tags) String() string {
    34  	return strings.Join(t.Strings(), ",")
    35  }
    36  
    37  // otelAttributes returns all key value pairs as a list of OpenTelemetry attributes
    38  func (t Tags) otelAttributes() []attribute.KeyValue {
    39  	attrs := make([]attribute.KeyValue, 0, len(t))
    40  	for k, v := range t {
    41  		attrs = append(attrs, attribute.String(k, v))
    42  	}
    43  	return attrs
    44  }