-
Notifications
You must be signed in to change notification settings - Fork 10
/
fake_tagger.go
143 lines (115 loc) · 3.7 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
140
141
142
143
// 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 (
"strconv"
"sync"
"github.com/n9e/n9e-agentd/pkg/apiserver/response"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/tagger/collectors"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/tagger/types"
"github.com/n9e/n9e-agentd/staging/datadog-agent/pkg/util"
)
// FakeTagger implements the Tagger interface
type FakeTagger struct {
errors map[string]error
store *tagStore
sync.RWMutex
}
// NewFakeTagger returns a new fake Tagger
func NewFakeTagger() *FakeTagger {
return &FakeTagger{
errors: make(map[string]error),
store: 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([]*collectors.TagInfo{
{
Source: source,
Entity: entity,
LowCardTags: low,
OrchestratorCardTags: orch,
HighCardTags: high,
StandardTags: std,
},
})
}
// SetTagsFromInfo allows to set tags from list of TagInfo
func (f *FakeTagger) SetTagsFromInfo(tags []*collectors.TagInfo) {
f.store.processTagInfo(tags)
}
// SetError allows to set an error to be returned when `Tag` or `TagBuilder` is called
// for this entity and cardinality
func (f *FakeTagger) SetError(entity string, cardinality collectors.TagCardinality, err error) {
f.Lock()
defer f.Unlock()
f.errors[f.getKey(entity, cardinality)] = err
}
// Tagger interface
// Init not implemented in fake tagger
func (f *FakeTagger) Init() 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 collectors.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
}
// TagBuilder fake implementation
func (f *FakeTagger) TagBuilder(entity string, cardinality collectors.TagCardinality, tb *util.TagsBuilder) 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)
}
// List fake implementation
func (f *FakeTagger) List(cardinality collectors.TagCardinality) response.TaggerListResponse {
r := response.TaggerListResponse{
Entities: make(map[string]response.TaggerListEntity),
}
f.store.RLock()
defer f.store.RUnlock()
for entityID, et := range f.store.store {
entity := response.TaggerListEntity{
Tags: make(map[string][]string),
}
for source, sourceTags := range et.sourceTags {
tags := append([]string(nil), sourceTags.lowCardTags...)
tags = append(tags, sourceTags.orchestratorCardTags...)
tags = append(tags, sourceTags.highCardTags...)
entity.Tags[source] = tags
}
r.Entities[entityID] = entity
}
return r
}
// Subscribe fake implementation
func (f *FakeTagger) Subscribe(cardinality collectors.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 collectors.TagCardinality) string {
return entity + strconv.FormatInt(int64(cardinality), 10)
}