-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
common.go
132 lines (118 loc) · 3.89 KB
/
common.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
// 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 2017-present Datadog, Inc.
//go:build !serverless
//nolint:revive // TODO(CINT) Fix revive linter
package listeners
import (
"fmt"
"hash/fnv"
"strconv"
"github.com/DataDog/datadog-agent/comp/core/autodiscovery/common/types"
"github.com/DataDog/datadog-agent/pkg/config"
"github.com/DataDog/datadog-agent/pkg/util/containers"
"github.com/DataDog/datadog-agent/pkg/util/kubernetes"
"github.com/DataDog/datadog-agent/pkg/util/log"
)
const (
// Keys of standard tags
tagKeyEnv = "env"
tagKeyVersion = "version"
tagKeyService = "service"
)
// containerFilters holds container filters for AD listeners
type containerFilters struct {
global *containers.Filter
metrics *containers.Filter
logs *containers.Filter
}
// getStandardTags extract standard tags from labels of kubernetes services
func getStandardTags(labels map[string]string) []string {
tags := []string{}
if labels == nil {
return tags
}
labelToTagKeys := map[string]string{
kubernetes.EnvTagLabelKey: tagKeyEnv,
kubernetes.VersionTagLabelKey: tagKeyVersion,
kubernetes.ServiceTagLabelKey: tagKeyService,
}
for labelKey, tagKey := range labelToTagKeys {
if tagValue, found := labels[labelKey]; found {
tags = append(tags, fmt.Sprintf("%s:%s", tagKey, tagValue))
}
}
return tags
}
// standardTagsDigest computes the hash of standard tags in a map
func standardTagsDigest(labels map[string]string) string {
if labels == nil {
return ""
}
h := fnv.New64()
// the implementation of h.Write never returns a non-nil error
_, _ = h.Write([]byte(labels[kubernetes.EnvTagLabelKey]))
_, _ = h.Write([]byte(labels[kubernetes.VersionTagLabelKey]))
_, _ = h.Write([]byte(labels[kubernetes.ServiceTagLabelKey]))
return strconv.FormatUint(h.Sum64(), 16)
}
// newContainerFilters instantiates the required container filters for AD listeners
func newContainerFilters() (*containerFilters, error) {
global, err := containers.NewAutodiscoveryFilter(containers.GlobalFilter)
if err != nil {
return nil, err
}
metrics, err := containers.NewAutodiscoveryFilter(containers.MetricsFilter)
if err != nil {
return nil, err
}
logs, err := containers.NewAutodiscoveryFilter(containers.LogsFilter)
if err != nil {
return nil, err
}
return &containerFilters{
global: global,
metrics: metrics,
logs: logs,
}, nil
}
func (f *containerFilters) IsExcluded(filter containers.FilterType, annotations map[string]string, name, image, ns string) bool {
switch filter {
case containers.GlobalFilter:
return f.global.IsExcluded(annotations, name, image, ns)
case containers.MetricsFilter:
return f.metrics.IsExcluded(annotations, name, image, ns)
case containers.LogsFilter:
return f.logs.IsExcluded(annotations, name, image, ns)
}
return false
}
// getPrometheusIncludeAnnotations returns the Prometheus AD include annotations based on the Prometheus config
func getPrometheusIncludeAnnotations() types.PrometheusAnnotations {
annotations := types.PrometheusAnnotations{}
tmpConfigString := config.Datadog.GetString("prometheus_scrape.checks")
var checks []*types.PrometheusCheck
if len(tmpConfigString) > 0 {
var err error
checks, err = types.PrometheusScrapeChecksTransformer(tmpConfigString)
if err != nil {
log.Warnf("Couldn't get configurations from 'prometheus_scrape.checks': %v", err)
return annotations
}
}
if len(checks) == 0 {
annotations[types.PrometheusScrapeAnnotation] = "true"
return annotations
}
for _, check := range checks {
if err := check.Init(config.Datadog.GetInt("prometheus_scrape.version")); err != nil {
log.Errorf("Couldn't init check configuration: %v", err)
continue
}
for k, v := range check.AD.GetIncludeAnnotations() {
annotations[k] = v
}
}
return annotations
}