Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CONTP-388] use entityid struct in tagger component #30133

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/serverless/dependencies_linux_amd64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ github.com/DataDog/datadog-agent/comp/core/log/impl
github.com/DataDog/datadog-agent/comp/core/secrets
github.com/DataDog/datadog-agent/comp/core/status
github.com/DataDog/datadog-agent/comp/core/tagger
github.com/DataDog/datadog-agent/comp/core/tagger/common
github.com/DataDog/datadog-agent/comp/core/tagger/noopimpl
github.com/DataDog/datadog-agent/comp/core/tagger/telemetry
github.com/DataDog/datadog-agent/comp/core/tagger/types
Expand Down
1 change: 1 addition & 0 deletions cmd/serverless/dependencies_linux_arm64.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ github.com/DataDog/datadog-agent/comp/core/log/impl
github.com/DataDog/datadog-agent/comp/core/secrets
github.com/DataDog/datadog-agent/comp/core/status
github.com/DataDog/datadog-agent/comp/core/tagger
github.com/DataDog/datadog-agent/comp/core/tagger/common
github.com/DataDog/datadog-agent/comp/core/tagger/noopimpl
github.com/DataDog/datadog-agent/comp/core/tagger/telemetry
github.com/DataDog/datadog-agent/comp/core/tagger/types
Expand Down
4 changes: 2 additions & 2 deletions comp/core/autodiscovery/listeners/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ func (s *service) GetPorts(_ context.Context) ([]ContainerPort, error) {

// GetTags returns the tags associated with the service.
func (s *service) GetTags() ([]string, error) {
return tagger.Tag(taggercommon.BuildTaggerEntityID(s.entity.GetID()).String(), tagger.ChecksCardinality())
return tagger.Tag(taggercommon.BuildTaggerEntityID(s.entity.GetID()), tagger.ChecksCardinality())
}

// GetTagsWithCardinality returns the tags with given cardinality.
func (s *service) GetTagsWithCardinality(cardinality string) ([]string, error) {
checkCard, err := types.StringToTagCardinality(cardinality)
if err == nil {
return tagger.Tag(taggercommon.BuildTaggerEntityID(s.entity.GetID()).String(), checkCard)
return tagger.Tag(taggercommon.BuildTaggerEntityID(s.entity.GetID()), checkCard)
}
log.Warnf("error converting cardinality %s to TagCardinality: %v", cardinality, err)
return s.GetTags()
Expand Down
15 changes: 11 additions & 4 deletions comp/core/tagger/common/entity_id_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
package common

import (
"fmt"
"strings"

"github.com/DataDog/datadog-agent/comp/core/tagger/types"
workloadmeta "github.com/DataDog/datadog-agent/comp/core/workloadmeta/def"
"github.com/DataDog/datadog-agent/pkg/util/log"
Expand Down Expand Up @@ -37,14 +40,18 @@ func BuildTaggerEntityID(entityID workloadmeta.EntityID) types.EntityID {
}

var globalEntityID = types.NewEntityID("internal", "global-entity-id")
var globalEntityIDString = globalEntityID.String()

// GetGlobalEntityID returns the entity ID that holds global tags
func GetGlobalEntityID() types.EntityID {
return globalEntityID
}

// GetGlobalEntityIDString returns, in a plain string format, the entity ID that holds global tags
func GetGlobalEntityIDString() string {
return globalEntityIDString
// ExtractPrefixAndID extracts prefix and id from tagger entity id and returns an error if the received entityID is not valid
func ExtractPrefixAndID(entityID string) (prefix types.EntityIDPrefix, id string, err error) {
extractedPrefix, extractedID, found := strings.Cut(entityID, "://")
if !found {
return "", "", fmt.Errorf("unsupported tagger entity id format %q, correct format is `{prefix}://{id}`", entityID)
}

return types.EntityIDPrefix(extractedPrefix), extractedID, nil
}
2 changes: 1 addition & 1 deletion comp/core/tagger/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Component interface {
Stop() error
ReplayTagger() ReplayTagger
GetTaggerTelemetryStore() *telemetry.Store
Tag(entityID string, cardinality types.TagCardinality) ([]string, error)
Tag(entityID types.EntityID, cardinality types.TagCardinality) ([]string, error)
AccumulateTagsFor(entityID types.EntityID, cardinality types.TagCardinality, tb tagset.TagsAccumulator) error
Standard(entityID types.EntityID) ([]string, error)
List() types.TaggerListResponse
Expand Down
31 changes: 25 additions & 6 deletions comp/core/tagger/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
package tagger

import (
"fmt"
"errors"
"sync"

"github.com/DataDog/datadog-agent/comp/core/tagger/common"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
taggertypes "github.com/DataDog/datadog-agent/pkg/tagger/types"
"github.com/DataDog/datadog-agent/pkg/tagset"
Expand Down Expand Up @@ -39,15 +40,33 @@ func UnlockGlobalTaggerClient() {
// GetEntity returns the hash for the provided entity id.
func GetEntity(entityID types.EntityID) (*types.Entity, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling GetEntity")
return nil, errors.New("a global tagger must be set before calling GetEntity")
}
return globalTagger.GetEntity(entityID)
}

// LegacyTag is an interface function that queries taggerclient singleton
// If possible, avoid using this function, and use the Tag interface function instead.
// This function exists in order not to break backward compatibility with rtloader and python
// integrations using the tagger
func LegacyTag(entity string, cardinality types.TagCardinality) ([]string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since AIUI this API is not going away any time soon, would it make sense to give it a descriptive name? Something like GetTagsByString.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your point, but the thing here is that we need to push developers away from using this method because it includes an extra step of string parsing, which we can avoid by calling Tag method of the tagger.

This is why I think it is good to include something in the function name indicating that calling it should be avoided except in very specific cases.

How about:

LegacyTagByString ?

if globalTagger == nil {
return nil, errors.New("a global tagger must be set before calling Tag")
}

prefix, id, err := common.ExtractPrefixAndID(entity)
if err != nil {
return nil, err
}

entityID := types.NewEntityID(prefix, id)
return globalTagger.Tag(entityID, cardinality)
}

// Tag is an interface function that queries taggerclient singleton
func Tag(entity string, cardinality types.TagCardinality) ([]string, error) {
func Tag(entity types.EntityID, cardinality types.TagCardinality) ([]string, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling Tag")
return nil, errors.New("a global tagger must be set before calling Tag")
}
return globalTagger.Tag(entity, cardinality)
}
Expand All @@ -63,15 +82,15 @@ func GetEntityHash(entityID types.EntityID, cardinality types.TagCardinality) st
// AgentTags is an interface function that queries taggerclient singleton
func AgentTags(cardinality types.TagCardinality) ([]string, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling AgentTags")
return nil, errors.New("a global tagger must be set before calling AgentTags")
}
return globalTagger.AgentTags(cardinality)
}

// GlobalTags is an interface function that queries taggerclient singleton
func GlobalTags(cardinality types.TagCardinality) ([]string, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling GlobalTags")
return nil, errors.New("a global tagger must be set before calling GlobalTags")
}
return globalTagger.GlobalTags(cardinality)
}
Expand Down
2 changes: 1 addition & 1 deletion comp/core/tagger/noopimpl/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (n *noopTagger) GetTaggerTelemetryStore() *telemetry.Store {
return nil
}

func (n *noopTagger) Tag(string, types.TagCardinality) ([]string, error) {
func (n *noopTagger) Tag(types.EntityID, types.TagCardinality) ([]string, error) {
return nil, nil
}

Expand Down
10 changes: 0 additions & 10 deletions comp/core/tagger/taggerimpl/generic_store/composite_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ func (os *compositeObjectStore[T]) Get(entityID types.EntityID) (object T, found
return
}

// GetWithEntityIDStr implements ObjectStore#GetWithEntityIDStr
func (os *compositeObjectStore[T]) GetWithEntityIDStr(id string) (object T, found bool) {
entityID, err := types.NewEntityIDFromString(id)
if err != nil {
return
}

return os.Get(entityID)
}

// Set implements ObjectStore#Set
func (os *compositeObjectStore[T]) Set(entityID types.EntityID, object T) {
prefix := entityID.GetPrefix()
Expand Down
60 changes: 22 additions & 38 deletions comp/core/tagger/taggerimpl/generic_store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,6 @@ func TestObjectStore_GetSet(t *testing.T) {
assert.Falsef(t, found, "item should not be found in store")
}

func TestObjectStore_GetWithEntityIDStr(t *testing.T) {
store := NewObjectStore[any]()

id := types.NewEntityID("prefix", "id")
idStr := id.String()
// getting a non-existent item
obj, found := store.GetWithEntityIDStr(idStr)
assert.Nil(t, obj)
assert.Falsef(t, found, "item should not be found in store")

// set item
store.Set(id, struct{}{})

// getting item
obj, found = store.GetWithEntityIDStr(idStr)
assert.NotNil(t, obj)
assert.Truef(t, found, "item should be found in store")

// unsetting item
store.Unset(id)

// getting a non-existent item
obj, found = store.GetWithEntityIDStr(idStr)
assert.Nil(t, obj)
assert.Falsef(t, found, "item should not be found in store")
}

func TestObjectStore_Size(t *testing.T) {
store := NewObjectStore[any]()

Expand Down Expand Up @@ -99,25 +72,37 @@ func TestObjectStore_ListObjects(t *testing.T) {
assert.Equalf(t, len(list), 0, "ListObjects should return an empty list")

// add some items
ids := []string{"prefix1://id1", "prefix2://id2", "prefix3://id3", "prefix4://id4"}
for _, id := range ids {
entityID, _ := types.NewEntityIDFromString(id)
store.Set(entityID, id)
ids := []types.EntityID{
types.NewEntityID(types.EntityIDPrefix("prefix1"), "id1"),
types.NewEntityID(types.EntityIDPrefix("prefix2"), "id2"),
types.NewEntityID(types.EntityIDPrefix("prefix3"), "id3"),
types.NewEntityID(types.EntityIDPrefix("prefix4"), "id4"),
}

for _, entityID := range ids {
store.Set(entityID, entityID)
}

// list should return empty
list = store.ListObjects(filter)
expectedListing := []any{"prefix1://id1", "prefix2://id2"}
expectedListing := []types.EntityID{
types.NewEntityID(types.EntityIDPrefix("prefix1"), "id1"),
types.NewEntityID(types.EntityIDPrefix("prefix2"), "id2"),
}
assert.ElementsMatch(t, expectedListing, list)
}

func TestObjectStore_ForEach(t *testing.T) {
store := NewObjectStore[any]()

// add some items
ids := []string{"prefix1://id1", "prefix2://id2", "prefix3://id3", "prefix4://id4"}
for _, id := range ids {
entityID, _ := types.NewEntityIDFromString(id)
ids := []types.EntityID{
types.NewEntityID(types.EntityIDPrefix("prefix1"), "id1"),
types.NewEntityID(types.EntityIDPrefix("prefix2"), "id2"),
types.NewEntityID(types.EntityIDPrefix("prefix3"), "id3"),
types.NewEntityID(types.EntityIDPrefix("prefix4"), "id4"),
}

for _, entityID := range ids {
store.Set(entityID, struct{}{})
}

Expand All @@ -128,8 +113,7 @@ func TestObjectStore_ForEach(t *testing.T) {
fb.Include(types.EntityIDPrefix("prefix1"), types.EntityIDPrefix("prefix2"))
filter := fb.Build(types.HighCardinality)

// only elements matching the filter should be included in the accumulator
store.ForEach(filter, func(id types.EntityID, _ any) { accumulator = append(accumulator, id.String()) })

// list should return empty
assert.ElementsMatch(t, accumulator, []string{"prefix1://id1", "prefix2://id2"})
}
11 changes: 5 additions & 6 deletions comp/core/tagger/taggerimpl/local/fake_tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,10 @@ func (f *FakeTagger) GetTaggerTelemetryStore() *telemetry.Store {
}

// Tag fake implementation
func (f *FakeTagger) Tag(entityID string, cardinality types.TagCardinality) ([]string, error) {
id, _ := types.NewEntityIDFromString(entityID)
tags := f.store.Lookup(id, cardinality)
func (f *FakeTagger) Tag(entityID types.EntityID, cardinality types.TagCardinality) ([]string, error) {
tags := f.store.Lookup(entityID, cardinality)

key := f.getKey(id, cardinality)
key := f.getKey(entityID, cardinality)
if err := f.errors[key]; err != nil {
return nil, err
}
Expand All @@ -111,12 +110,12 @@ func (f *FakeTagger) Tag(entityID string, cardinality types.TagCardinality) ([]s

// GlobalTags fake implementation
func (f *FakeTagger) GlobalTags(cardinality types.TagCardinality) ([]string, error) {
return f.Tag(common.GetGlobalEntityIDString(), cardinality)
return f.Tag(common.GetGlobalEntityID(), cardinality)
}

// AccumulateTagsFor fake implementation
func (f *FakeTagger) AccumulateTagsFor(entityID types.EntityID, cardinality types.TagCardinality, tb tagset.TagsAccumulator) error {
tags, err := f.Tag(entityID.String(), cardinality)
tags, err := f.Tag(entityID, cardinality)
if err != nil {
return err
}
Expand Down
6 changes: 2 additions & 4 deletions comp/core/tagger/taggerimpl/local/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,8 @@ func (t *Tagger) AccumulateTagsFor(entityID types.EntityID, cardinality types.Ta
}

// Tag returns a copy of the tags for a given entity
func (t *Tagger) Tag(entityID string, cardinality types.TagCardinality) ([]string, error) {
compositeEntityID, _ := types.NewEntityIDFromString(entityID)

tags, err := t.getTags(compositeEntityID, cardinality)
func (t *Tagger) Tag(entityID types.EntityID, cardinality types.TagCardinality) ([]string, error) {
tags, err := t.getTags(entityID, cardinality)
if err != nil {
return nil, err
}
Expand Down
7 changes: 3 additions & 4 deletions comp/core/tagger/taggerimpl/local/tagger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ func TestAccumulateTagsFor(t *testing.T) {

func TestTag(t *testing.T) {
entityID := types.NewEntityID(types.ContainerID, "123")
entityIDStr := entityID.String()

store := fxutil.Test[workloadmetamock.Mock](t, fx.Options(
fx.Supply(config.Params{}),
Expand Down Expand Up @@ -99,15 +98,15 @@ func TestTag(t *testing.T) {
},
})

lowCardTags, err := tagger.Tag(entityIDStr, types.LowCardinality)
lowCardTags, err := tagger.Tag(entityID, types.LowCardinality)
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"low1", "low2"}, lowCardTags)

orchestratorCardTags, err := tagger.Tag(entityIDStr, types.OrchestratorCardinality)
orchestratorCardTags, err := tagger.Tag(entityID, types.OrchestratorCardinality)
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"low1", "low2", "orchestrator1", "orchestrator2"}, orchestratorCardTags)

highCardTags, err := tagger.Tag(entityIDStr, types.HighCardinality)
highCardTags, err := tagger.Tag(entityID, types.HighCardinality)
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"low1", "low2", "orchestrator1", "orchestrator2", "high1", "high2"}, highCardTags)
}
7 changes: 3 additions & 4 deletions comp/core/tagger/taggerimpl/remote/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,8 @@ func (t *Tagger) GetTaggerTelemetryStore() *telemetry.Store {
}

// Tag returns tags for a given entity at the desired cardinality.
func (t *Tagger) Tag(entityID string, cardinality types.TagCardinality) ([]string, error) {
id, _ := types.NewEntityIDFromString(entityID)
entity := t.store.getEntity(id)
func (t *Tagger) Tag(entityID types.EntityID, cardinality types.TagCardinality) ([]string, error) {
entity := t.store.getEntity(entityID)
if entity != nil {
t.telemetryStore.QueriesByCardinality(cardinality).Success.Inc()
return entity.GetTags(cardinality), nil
Expand All @@ -215,7 +214,7 @@ func (t *Tagger) Tag(entityID string, cardinality types.TagCardinality) ([]strin

// AccumulateTagsFor returns tags for a given entity at the desired cardinality.
func (t *Tagger) AccumulateTagsFor(entityID types.EntityID, cardinality types.TagCardinality, tb tagset.TagsAccumulator) error {
tags, err := t.Tag(entityID.String(), cardinality)
tags, err := t.Tag(entityID, cardinality)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions comp/core/tagger/taggerimpl/replay/tagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ func (t *Tagger) Stop() error {
}

// Tag returns tags for a given entity at the desired cardinality.
func (t *Tagger) Tag(entityID string, cardinality types.TagCardinality) ([]string, error) {
id, _ := types.NewEntityIDFromString(entityID)
tags := t.store.Lookup(id, cardinality)
func (t *Tagger) Tag(entityID types.EntityID, cardinality types.TagCardinality) ([]string, error) {
tags := t.store.Lookup(entityID, cardinality)
return tags, nil
}

Expand Down
2 changes: 1 addition & 1 deletion comp/core/tagger/taggerimpl/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (s *Server) TaggerFetchEntity(_ context.Context, in *pb.FetchEntityRequest)
return nil, status.Errorf(codes.InvalidArgument, `missing "id" parameter`)
}

entityID := types.EntityIDPrefix(in.Id.Prefix).ToUID(in.Id.Uid)
entityID := types.NewEntityID(types.EntityIDPrefix(in.Id.Prefix), in.Id.Uid)
cardinality, err := proto.Pb2TaggerCardinality(in.GetCardinality())
if err != nil {
return nil, err
Expand Down
Loading
Loading