-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
fake_tagger.go
139 lines (113 loc) · 3.82 KB
/
fake_tagger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package local
import (
"context"
"strconv"
"sync"
"github.com/DataDog/datadog-agent/comp/core/tagger/taggerimpl/collectors"
"github.com/DataDog/datadog-agent/comp/core/tagger/taggerimpl/empty"
"github.com/DataDog/datadog-agent/comp/core/tagger/taggerimpl/tagstore"
"github.com/DataDog/datadog-agent/comp/core/tagger/types"
"github.com/DataDog/datadog-agent/pkg/tagset"
)
// FakeTagger implements the Tagger interface
type FakeTagger struct {
errors map[string]error
store *tagstore.TagStore
sync.RWMutex
empty.Tagger
}
// NewFakeTagger returns a new fake Tagger
func NewFakeTagger() *FakeTagger {
return &FakeTagger{
errors: make(map[string]error),
store: tagstore.NewTagStore(),
}
}
// FakeTagger specific interface
// SetTags allows to set tags in store for a given source, entity
func (f *FakeTagger) SetTags(entity, source string, low, orch, high, std []string) {
f.store.ProcessTagInfo([]*types.TagInfo{
{
Source: source,
Entity: entity,
LowCardTags: low,
OrchestratorCardTags: orch,
HighCardTags: high,
StandardTags: std,
},
})
}
// SetGlobalTags allows to set tags in store for the global entity
func (f *FakeTagger) SetGlobalTags(low, orch, high, std []string) {
f.SetTags(collectors.GlobalEntityID, "static", low, orch, high, std)
}
// SetTagsFromInfo allows to set tags from list of TagInfo
func (f *FakeTagger) SetTagsFromInfo(tags []*types.TagInfo) {
f.store.ProcessTagInfo(tags)
}
// SetError allows to set an error to be returned when `Tag` or `AccumulateTagsFor` is called
// for this entity and cardinality
func (f *FakeTagger) SetError(entity string, cardinality types.TagCardinality, err error) {
f.Lock()
defer f.Unlock()
f.errors[f.getKey(entity, cardinality)] = err
}
// Tagger interface
// Start not implemented in fake tagger
func (f *FakeTagger) Start(_ context.Context) error {
return nil
}
// Stop not implemented in fake tagger
func (f *FakeTagger) Stop() error {
return nil
}
// Tag fake implementation
func (f *FakeTagger) Tag(entity string, cardinality types.TagCardinality) ([]string, error) {
tags := f.store.Lookup(entity, cardinality)
key := f.getKey(entity, cardinality)
if err := f.errors[key]; err != nil {
return nil, err
}
return tags, nil
}
// GlobalTags fake implementation
func (f *FakeTagger) GlobalTags(cardinality types.TagCardinality) ([]string, error) {
return f.Tag(collectors.GlobalEntityID, cardinality)
}
// AccumulateTagsFor fake implementation
func (f *FakeTagger) AccumulateTagsFor(entity string, cardinality types.TagCardinality, tb tagset.TagsAccumulator) error {
tags, err := f.Tag(entity, cardinality)
if err != nil {
return err
}
tb.Append(tags...)
return nil
}
// Standard fake implementation
func (f *FakeTagger) Standard(entity string) ([]string, error) {
return f.store.LookupStandard(entity)
}
// GetEntity returns faked entity corresponding to the specified id and an error
func (f *FakeTagger) GetEntity(entityID string) (*types.Entity, error) {
return f.store.GetEntity(entityID)
}
// List fake implementation
func (f *FakeTagger) List() types.TaggerListResponse {
return f.store.List()
}
// Subscribe fake implementation
func (f *FakeTagger) Subscribe(cardinality types.TagCardinality) chan []types.EntityEvent {
return f.store.Subscribe(cardinality)
}
// Unsubscribe fake implementation
func (f *FakeTagger) Unsubscribe(ch chan []types.EntityEvent) {
f.store.Unsubscribe(ch)
}
// Fake internals
func (f *FakeTagger) getKey(entity string, cardinality types.TagCardinality) string {
return entity + strconv.FormatInt(int64(cardinality), 10)
}