github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/storage/segment/key_test.go (about)

     1  package segment
     2  
     3  import (
     4  	. "github.com/onsi/ginkgo/v2"
     5  	. "github.com/onsi/gomega"
     6  
     7  	"github.com/pyroscope-io/pyroscope/pkg/flameql"
     8  )
     9  
    10  var _ = Describe("segment key", func() {
    11  	Context("ParseKey", func() {
    12  		It("no tags version works", func() {
    13  			k, err := ParseKey("foo")
    14  			Expect(err).ToNot(HaveOccurred())
    15  			Expect(k.labels).To(Equal(map[string]string{"__name__": "foo"}))
    16  		})
    17  
    18  		It("simple values work", func() {
    19  			k, err := ParseKey("foo{bar=1,baz=2}")
    20  			Expect(err).ToNot(HaveOccurred())
    21  			Expect(k.labels).To(Equal(map[string]string{"__name__": "foo", "bar": "1", "baz": "2"}))
    22  		})
    23  
    24  		It("simple values with spaces work", func() {
    25  			k, err := ParseKey(" foo { bar = 1 , baz = 2 } ")
    26  			Expect(err).ToNot(HaveOccurred())
    27  			Expect(k.labels).To(Equal(map[string]string{"__name__": "foo", "bar": "1", "baz": "2"}))
    28  		})
    29  	})
    30  
    31  	Context("Key", func() {
    32  		Context("Normalize", func() {
    33  			It("no tags version works", func() {
    34  				k, err := ParseKey("foo")
    35  				Expect(err).ToNot(HaveOccurred())
    36  				Expect(k.Normalized()).To(Equal("foo{}"))
    37  			})
    38  
    39  			It("simple values work", func() {
    40  				k, err := ParseKey("foo{bar=1,baz=2}")
    41  				Expect(err).ToNot(HaveOccurred())
    42  				Expect(k.Normalized()).To(Equal("foo{bar=1,baz=2}"))
    43  			})
    44  
    45  			It("unsorted values work", func() {
    46  				k, err := ParseKey("foo{baz=1,bar=2}")
    47  				Expect(err).ToNot(HaveOccurred())
    48  				Expect(k.Normalized()).To(Equal("foo{bar=2,baz=1}"))
    49  			})
    50  		})
    51  	})
    52  
    53  	Context("Key", func() {
    54  		Context("Match", func() {
    55  			It("reports whether a segments key satisfies tag matchers", func() {
    56  				type evalTestCase struct {
    57  					query string
    58  					match bool
    59  					key   string
    60  				}
    61  
    62  				testCases := []evalTestCase{
    63  					// No matchers specified except app name.
    64  					{`app.name`, true, `app.name{foo=bar}`},
    65  					{`app.name{}`, true, `app.name{foo=bar}`},
    66  					{`app.name`, false, `x.name{foo=bar}`},
    67  					{`app.name{}`, false, `x.name{foo=bar}`},
    68  
    69  					{`app.name{foo="bar"}`, true, `app.name{foo=bar}`},
    70  					{`app.name{foo!="bar"}`, true, `app.name{foo=baz}`},
    71  					{`app.name{foo="bar"}`, false, `app.name{foo=baz}`},
    72  					{`app.name{foo!="bar"}`, false, `app.name{foo=bar}`},
    73  
    74  					// Tag key not present.
    75  					{`app.name{foo="bar"}`, false, `app.name{bar=baz}`},
    76  					{`app.name{foo!="bar"}`, true, `app.name{bar=baz}`},
    77  
    78  					{`app.name{foo="bar",baz="qux"}`, true, `app.name{foo=bar,baz=qux}`},
    79  					{`app.name{foo="bar",baz!="qux"}`, true, `app.name{foo=bar,baz=fred}`},
    80  					{`app.name{foo="bar",baz="qux"}`, false, `app.name{foo=bar}`},
    81  					{`app.name{foo="bar",baz!="qux"}`, false, `app.name{foo=bar,baz=qux}`},
    82  					{`app.name{foo="bar",baz!="qux"}`, false, `app.name{baz=fred,bar=baz}`},
    83  				}
    84  
    85  				for _, tc := range testCases {
    86  					qry, _ := flameql.ParseQuery(tc.query)
    87  					k, _ := ParseKey(tc.key)
    88  					if matched := k.Match(qry); matched != tc.match {
    89  						Expect(matched).To(Equal(tc.match), tc.query, tc.key)
    90  					}
    91  				}
    92  			})
    93  		})
    94  	})
    95  })