Skip to content

Commit

Permalink
[CONTP-517] Support None card in tagger (#31897)
Browse files Browse the repository at this point in the history
  • Loading branch information
gabedos authored Dec 18, 2024
1 parent ba0629f commit 7c1dae6
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 6 deletions.
2 changes: 2 additions & 0 deletions comp/core/tagger/collectors/pod_tag_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func (p *PodTagExtractor) Extract(podEntity *workloadmeta.KubernetesPod, cardina
return append(tagInfos.LowCardTags, tagInfos.OrchestratorCardTags...)
case types.LowCardinality:
return tagInfos.LowCardTags
case types.NoneCardinality:
return []string{}
default:
log.Errorf("unsupported tag cardinality %v", cardinality)
return []string{}
Expand Down
5 changes: 2 additions & 3 deletions comp/core/tagger/impl/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ func (t *TaggerWrapper) EnrichTags(tb tagset.TagsAccumulator, originInfo taggert
// | none | empty || empty |
// | empty | not empty || container prefix + originFromMsg |
// | none | not empty || container prefix + originFromMsg |
if t.datadogConfig.dogstatsdOptOutEnabled && originInfo.Cardinality == "none" {
if t.datadogConfig.dogstatsdOptOutEnabled && originInfo.Cardinality == types.NoneCardinalityString {
originInfo.ContainerIDFromSocket = packets.NoOrigin
originInfo.PodUID = ""
originInfo.ContainerID = ""
Expand Down Expand Up @@ -460,8 +460,7 @@ func (t *TaggerWrapper) EnrichTags(tb tagset.TagsAccumulator, originInfo taggert
}
default:
// Disable origin detection if cardinality is none
// TODO: The `none` cardinality should be directly supported by the Tagger.
if originInfo.Cardinality == "none" {
if originInfo.Cardinality == types.NoneCardinalityString {
originInfo.ContainerIDFromSocket = packets.NoOrigin
originInfo.PodUID = ""
originInfo.ContainerID = ""
Expand Down
37 changes: 37 additions & 0 deletions comp/core/tagger/subscriber/subscription_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,21 @@ func TestSubscriptionManager(t *testing.T) {

highCardSubscription.Unsubscribe()

// None Cardinality Subscriber
noneCardSubID := "none-card-sub"
noneCardSubscription, err := sm.Subscribe(noneCardSubID, types.NewFilterBuilder().Include(types.EntityIDPrefix("foo")).Build(types.NoneCardinality), nil)
require.NoError(t, err)

sm.Notify([]types.EntityEvent{
events["added"],
events["modified"],
events["deleted"],
events["added-with-no-id"],
events["added-with-unmatched-prefix"],
})

noneCardSubscription.Unsubscribe()

// Verify low cardinality subscriber received events
assertReceivedEvents(t, lowCardSubscription.EventsChan(), []types.EntityEvent{
{
Expand Down Expand Up @@ -192,6 +207,28 @@ func TestSubscriptionManager(t *testing.T) {
},
},
})

// Verify none cardinality subscriber received events
assertReceivedEvents(t, noneCardSubscription.EventsChan(), []types.EntityEvent{
{
EventType: types.EventTypeAdded,
Entity: types.Entity{
ID: entityID,
},
},
{
EventType: types.EventTypeModified,
Entity: types.Entity{
ID: entityID,
},
},
{
EventType: types.EventTypeDeleted,
Entity: types.Entity{
ID: entityID,
},
},
})
}

func assertReceivedEvents(t *testing.T, ch chan []types.EntityEvent, expectedEvents []types.EntityEvent) {
Expand Down
12 changes: 9 additions & 3 deletions comp/core/tagger/tagstore/entity_tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,16 @@ func (e *EntityTagsWithMultipleSources) getStandard() []string {
func (e *EntityTagsWithMultipleSources) getHashedTags(cardinality types.TagCardinality) tagset.HashedTags {
e.computeCache()

if cardinality == types.HighCardinality {
switch cardinality {
case types.HighCardinality:
return e.cachedAll
} else if cardinality == types.OrchestratorCardinality {
case types.OrchestratorCardinality:
return e.cachedOrchestrator
case types.NoneCardinality:
return tagset.HashedTags{}
default:
return e.cachedLow
}
return e.cachedLow
}

func (e *EntityTagsWithMultipleSources) computeCache() {
Expand Down Expand Up @@ -302,6 +306,8 @@ func (e *EntityTagsWithSingleSource) getHashedTags(cardinality types.TagCardinal
return e.cachedAll
case types.OrchestratorCardinality:
return e.cachedOrchestrator
case types.NoneCardinality:
return tagset.HashedTags{}
default:
return e.cachedLow
}
Expand Down
6 changes: 6 additions & 0 deletions comp/core/tagger/tagstore/entity_tags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ func TestGetHashedTags(t *testing.T) {
[]string{"l1:v1", "l2:v2", "service:s1", "o1:v1", "o2:v2", "h1:v1", "h2:v2"},
entityTags.getHashedTags(types.HighCardinality).Get(),
)

assert.Equal(
t,
[]string(nil),
entityTags.getHashedTags(types.NoneCardinality).Get(),
)
}

func TestTagsForSource(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions comp/core/tagger/tagstore/tagstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ func (s *StoreTestSuite) TestLookup() {
tagsHigh := s.tagstore.Lookup(entityID, types.HighCardinality)
tagsOrch := s.tagstore.Lookup(entityID, types.OrchestratorCardinality)
tagsLow := s.tagstore.Lookup(entityID, types.LowCardinality)
tagsNone := s.tagstore.Lookup(entityID, types.NoneCardinality)

assert.Len(s.T(), tagsHigh, 4)
assert.Len(s.T(), tagsLow, 2)
assert.Len(s.T(), tagsOrch, 3)
assert.Nil(s.T(), tagsNone)
}

func (s *StoreTestSuite) TestLookupHashedWithEntityStr() {
Expand All @@ -118,10 +120,12 @@ func (s *StoreTestSuite) TestLookupHashedWithEntityStr() {
tagsLow := s.tagstore.LookupHashedWithEntityStr(entityID, types.LowCardinality)
tagsOrch := s.tagstore.LookupHashedWithEntityStr(entityID, types.OrchestratorCardinality)
tagsHigh := s.tagstore.LookupHashedWithEntityStr(entityID, types.HighCardinality)
tagsNone := s.tagstore.LookupHashedWithEntityStr(entityID, types.NoneCardinality)

assert.ElementsMatch(s.T(), tagsLow.Get(), []string{"low1", "low2"})
assert.ElementsMatch(s.T(), tagsOrch.Get(), []string{"low1", "low2", "orchestrator1"})
assert.ElementsMatch(s.T(), tagsHigh.Get(), []string{"low1", "low2", "orchestrator1", "high1"})
assert.ElementsMatch(s.T(), tagsNone.Get(), []string{})
}

func (s *StoreTestSuite) TestLookupStandard() {
Expand Down
4 changes: 4 additions & 0 deletions comp/core/tagger/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type Store struct {
LowCardinalityQueries CardinalityTelemetry
OrchestratorCardinalityQueries CardinalityTelemetry
HighCardinalityQueries CardinalityTelemetry
NoneCardinalityQueries CardinalityTelemetry
UnknownCardinalityQueries CardinalityTelemetry
}

Expand Down Expand Up @@ -144,6 +145,7 @@ func NewStore(telemetryComp telemetry.Component) *Store {
LowCardinalityQueries: newCardinalityTelemetry(queries, types.LowCardinalityString),
OrchestratorCardinalityQueries: newCardinalityTelemetry(queries, types.OrchestratorCardinalityString),
HighCardinalityQueries: newCardinalityTelemetry(queries, types.HighCardinalityString),
NoneCardinalityQueries: newCardinalityTelemetry(queries, types.NoneCardinalityString),
UnknownCardinalityQueries: newCardinalityTelemetry(queries, types.UnknownCardinalityString),
}
})
Expand All @@ -160,6 +162,8 @@ func (s *Store) QueriesByCardinality(card types.TagCardinality) *CardinalityTele
return &s.OrchestratorCardinalityQueries
case types.HighCardinality:
return &s.HighCardinalityQueries
case types.NoneCardinality:
return &s.NoneCardinalityQueries
default:
return &s.UnknownCardinalityQueries
}
Expand Down
16 changes: 16 additions & 0 deletions comp/core/tagger/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ const (
LowCardinality TagCardinality = iota
OrchestratorCardinality
HighCardinality
NoneCardinality
)

// Entity is an entity ID + tags.
Expand All @@ -92,6 +93,10 @@ type Entity struct {
// GetTags flattens all tags from all cardinalities into a single slice of tag
// strings.
func (e Entity) GetTags(cardinality TagCardinality) []string {
if cardinality == NoneCardinality {
return []string{}
}

tagArrays := make([][]string, 0, 3)
tagArrays = append(tagArrays, e.LowCardinalityTags)

Expand All @@ -117,6 +122,11 @@ func (e Entity) Copy(cardinality TagCardinality) Entity {
case LowCardinality:
newEntity.HighCardinalityTags = nil
newEntity.OrchestratorCardinalityTags = nil
case NoneCardinality:
newEntity.HighCardinalityTags = nil
newEntity.OrchestratorCardinalityTags = nil
newEntity.LowCardinalityTags = nil
newEntity.StandardTags = nil
}

return newEntity
Expand All @@ -131,6 +141,8 @@ const (
ShortOrchestratorCardinalityString = "orch"
// HighCardinalityString is the string representation of the high cardinality
HighCardinalityString = "high"
// NoneCardinalityString is the string representation of the none cardinality
NoneCardinalityString = "none"
// UnknownCardinalityString represents an unknown level of cardinality
UnknownCardinalityString = "unknown"
)
Expand All @@ -145,6 +157,8 @@ func StringToTagCardinality(c string) (TagCardinality, error) {
return OrchestratorCardinality, nil
case LowCardinalityString:
return LowCardinality, nil
case NoneCardinalityString:
return NoneCardinality, nil
default:
return LowCardinality, fmt.Errorf("unsupported value %s received for tag cardinality", c)
}
Expand All @@ -160,6 +174,8 @@ func TagCardinalityToString(c TagCardinality) string {
return OrchestratorCardinalityString
case LowCardinality:
return LowCardinalityString
case NoneCardinality:
return NoneCardinalityString
default:
return UnknownCardinalityString
}
Expand Down
11 changes: 11 additions & 0 deletions releasenotes/notes/none-cardinality-tag-f2ceec7ec571387e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Each section from every release note are combined when the
# CHANGELOG.rst is rendered. So the text needs to be worded so that
# it does not depend on any information only available in another
# section. This may mean repeating some details, but each section
# must be readable independently of the other.
#
# Each section note must be formatted as reStructuredText.
---
enhancements:
- |
Adds support for the `none` cardinality type in the tagger.

0 comments on commit 7c1dae6

Please sign in to comment.