Skip to content

Commit

Permalink
fixup! Support non-indexable high-cardinality metric tags/metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
codebien committed Oct 17, 2022
1 parent 5bd69b7 commit da4669c
Showing 1 changed file with 108 additions and 1 deletion.
109 changes: 108 additions & 1 deletion metrics/tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,114 @@ func TestTagSetContains(t *testing.T) {
assert.False(t, st.Contains(outer))
}

// TODO: Add TagsAndMeta tests
func TestTagsAndMetaSetTag(t *testing.T) {
t.Parallel()

r := NewRegistry()
tm := TagsAndMeta{Tags: r.RootTagSet()}
tm.SetTag("k1", "v1")
_, ok := tm.Tags.Get("k1")
assert.True(t, ok)
}

func TestTagsAndMetaDeleteTag(t *testing.T) {
t.Parallel()

r := NewRegistry()
tm := TagsAndMeta{Tags: r.RootTagSet()}
tm.Tags = tm.Tags.With("k1", "v1")
_, ok := tm.Tags.Get("k1")
assert.True(t, ok)

tm.DeleteTag("k1")
_, ok = tm.Tags.Get("k1")
assert.False(t, ok)
}

func TestTagsAndMetaSetMetadata(t *testing.T) {
t.Parallel()

t.Run("WhenNil", func(t *testing.T) {
tm := TagsAndMeta{}
tm.SetMetadata("k1", "v1")
_, ok := tm.Metadata["k1"]
assert.True(t, ok)
})

t.Run("WhenNotNil", func(t *testing.T) {
tm := TagsAndMeta{Metadata: make(map[string]string)}
tm.SetMetadata("k2", "v2")
_, ok := tm.Metadata["k2"]
assert.True(t, ok)
})
}

func TestTagsAndMetaDeleteMetadata(t *testing.T) {
t.Parallel()

tm := TagsAndMeta{Metadata: make(map[string]string)}
tm.Metadata["k1"] = "v1"
_, ok := tm.Metadata["k1"]
assert.True(t, ok)

tm.DeleteMetadata("k1")
_, ok = tm.Metadata["k1"]
assert.False(t, ok)
}

func TestTagsAndMetaSetSystemTagOrMetaIfEnabled(t *testing.T) {
t.Parallel()
tm := TagsAndMeta{}

tm.SetSystemTagOrMetaIfEnabled(&DefaultSystemTagSet, TagIter, "10")
_, ok := tm.Metadata["iter"]
assert.False(t, ok)

tm.SetSystemTagOrMetaIfEnabled(&NonIndexableSystemTags, TagIter, "10")
_, ok = tm.Metadata["iter"]
assert.True(t, ok)
}

func TestTagsAndMetaSetSystemTagOrMeta(t *testing.T) {
t.Parallel()

t.Run("Tag", func(t *testing.T) {
r := NewRegistry()
tm := TagsAndMeta{Tags: r.RootTagSet()}
tm.SetSystemTagOrMeta(TagIter, "10")

_, ok := tm.Metadata["iter"]
assert.True(t, ok)
_, ok = tm.Tags.Get("iter")
assert.False(t, ok)
})

t.Run("Metadata", func(t *testing.T) {
r := NewRegistry()
tm := TagsAndMeta{Tags: r.RootTagSet()}
tm.SetSystemTagOrMeta(TagName, "hello-request")

_, ok := tm.Tags.Get("name")
assert.True(t, ok)
_, ok = tm.Metadata["name"]
assert.False(t, ok)
})
}

func TestTagsAndMetaClone(t *testing.T) {
t.Parallel()

r := NewRegistry()
tags := r.RootTagSet().With("k1", "v1")
meta := map[string]string{"k2": "v2"}
tm := TagsAndMeta{Tags: tags, Metadata: meta}

tm2 := tm.Clone()
require.NotNil(t, tm2.Tags)
require.NotNil(t, tm2.Metadata)
assert.Equal(t, tm, tm2)
assert.False(t, &tm.Metadata == &tm2.Metadata)
}

func TestEnabledTagsMarshalJSON(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit da4669c

Please sign in to comment.