-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
global.go
135 lines (114 loc) · 4.37 KB
/
global.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
// 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 tagger
import (
"fmt"
"sync"
"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"
)
var (
// globalTagger is the global tagger instance backing the global Tag functions
// // TODO(components) (tagger): globalTagger is a legacy global variable but still in use, to be eliminated
globalTagger Component
// initOnce ensures that the global tagger is only initialized once. It is reset every
// time the default tagger is set.
initOnce sync.Once
// ChecksCardinality defines the cardinality of tags we should send for check metrics
// this can still be overridden when calling get_tags in python checks.
ChecksCardinality types.TagCardinality
// DogstatsdCardinality defines the cardinality of tags we should send for metrics from
// dogstatsd.
DogstatsdCardinality types.TagCardinality
)
// SetGlobalTaggerClient sets the global taggerClient instance
func SetGlobalTaggerClient(t Component) {
initOnce.Do(func() {
globalTagger = t
})
}
// UnlockGlobalTaggerClient releases the initOnce lock on the global tagger. For testing only.
func UnlockGlobalTaggerClient() {
initOnce = sync.Once{}
}
// GetEntity returns the hash for the provided entity id.
func GetEntity(entityID string) (*types.Entity, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling GetEntity")
}
return globalTagger.GetEntity(entityID)
}
// Tag is an interface function that queries taggerclient singleton
func Tag(entity string, cardinality types.TagCardinality) ([]string, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling Tag")
}
return globalTagger.Tag(entity, cardinality)
}
// AccumulateTagsFor is an interface function that queries taggerclient singleton
func AccumulateTagsFor(entity string, cardinality types.TagCardinality, tb tagset.TagsAccumulator) error {
if globalTagger == nil {
return fmt.Errorf("a global tagger must be set before calling AccumulateTagsFor")
}
return globalTagger.AccumulateTagsFor(entity, cardinality, tb)
}
// GetEntityHash is an interface function that queries taggerclient singleton
func GetEntityHash(entity string, cardinality types.TagCardinality) string {
if globalTagger != nil {
return globalTagger.GetEntityHash(entity, cardinality)
}
return ""
}
// StandardTags is an interface function that queries taggerclient singleton
func StandardTags(entity string) ([]string, error) {
if globalTagger == nil {
return nil, fmt.Errorf("a global tagger must be set before calling StandardTags")
}
return globalTagger.Standard(entity)
}
// 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 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 globalTagger.GlobalTags(cardinality)
}
// List the content of the defaulTagger
func List() types.TaggerListResponse {
if globalTagger != nil {
return globalTagger.List()
}
return types.TaggerListResponse{}
}
// GetTaggerInstance returns the global Tagger instance
func GetTaggerInstance() Component {
return globalTagger
}
// SetNewCaptureTagger will set capture tagger in global tagger instance by using provided capture tagger
func SetNewCaptureTagger(newCaptureTagger Component) {
if globalTagger != nil {
globalTagger.SetNewCaptureTagger(newCaptureTagger)
}
}
// ResetCaptureTagger will reset capture tagger
func ResetCaptureTagger() {
if globalTagger != nil {
globalTagger.ResetCaptureTagger()
}
}
// EnrichTags is an interface function that queries taggerclient singleton
func EnrichTags(tb tagset.TagsAccumulator, originInfo taggertypes.OriginInfo) {
if globalTagger != nil {
globalTagger.EnrichTags(tb, originInfo)
}
}