-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathcomponent_test.go
More file actions
203 lines (182 loc) · 5.65 KB
/
Copy pathcomponent_test.go
File metadata and controls
203 lines (182 loc) · 5.65 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// 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 2026-present Datadog, Inc.
package observerimpl
import (
"strings"
"testing"
log "github.com/DataDog/datadog-agent/comp/core/log/def"
telemetry "github.com/DataDog/datadog-agent/comp/core/telemetry/def"
telemetryimpl "github.com/DataDog/datadog-agent/comp/core/telemetry/impl"
compdef "github.com/DataDog/datadog-agent/comp/def"
configmock "github.com/DataDog/datadog-agent/pkg/config/mock"
"github.com/stretchr/testify/require"
)
type testLifecycle struct {
hooks []compdef.Hook
}
func (l *testLifecycle) Append(h compdef.Hook) {
l.hooks = append(l.hooks, h)
}
type noopLogComponent struct{}
func (noopLogComponent) Trace(...interface{}) {}
func (noopLogComponent) Tracef(string, ...interface{}) {}
func (noopLogComponent) Debug(...interface{}) {}
func (noopLogComponent) Debugf(string, ...interface{}) {}
func (noopLogComponent) Info(...interface{}) {}
func (noopLogComponent) Infof(string, ...interface{}) {}
func (noopLogComponent) Warn(...interface{}) error { return nil }
func (noopLogComponent) Warnf(string, ...interface{}) error { return nil }
func (noopLogComponent) Error(...interface{}) error { return nil }
func (noopLogComponent) Errorf(string, ...interface{}) error { return nil }
func (noopLogComponent) Critical(...interface{}) error { return nil }
func (noopLogComponent) Criticalf(string, ...interface{}) error { return nil }
func (noopLogComponent) Flush() {}
var _ log.Component = noopLogComponent{}
func requireNoObserverMetricFamilies(t *testing.T, telemetryComp telemetry.Component) {
t.Helper()
metricFamilies, err := telemetryComp.Gather(false)
require.NoError(t, err)
for _, family := range metricFamilies {
if strings.HasPrefix(family.GetName(), "observer__") {
t.Fatalf("unexpected observer metric family initialized: %s", family.GetName())
}
}
}
func TestNewComponentReturnsErrorForInvalidMetricProcessingRulesConfig(t *testing.T) {
testCases := []struct {
name string
yaml string
errContains string
}{
{
name: "invalid rule type",
yaml: `
anomaly_detection:
reporting:
events:
enabled: true
metrics:
enabled: true
processing_rules:
- type: invalid_type
name: bad_rule
`,
errContains: `anomaly_detection.metrics.processing_rules: rule "bad_rule": unsupported type "invalid_type"`,
},
{
name: "invalid name pattern",
yaml: `
anomaly_detection:
reporting:
events:
enabled: true
metrics:
enabled: true
processing_rules:
- type: exclude_at_match
name: bad_pattern
name_pattern: kubernetes.*.cpu
`,
errContains: "anomaly_detection.metrics.processing_rules: rule \"bad_pattern\": name_pattern must be a prefix with an optional trailing *",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
cfg := configmock.NewFromYAML(t, tc.yaml)
lc := &testLifecycle{}
telComp := telemetryimpl.GetCompatComponent()
telComp.Reset()
t.Cleanup(telComp.Reset)
_, err := NewComponent(Requires{
Lifecycle: lc,
Config: cfg,
Log: noopLogComponent{},
Telemetry: telComp,
})
require.Error(t, err)
require.Contains(t, err.Error(), tc.errContains)
})
}
}
func TestNewComponentWithAnalysisDisabledUsesNoopHandleAndDoesNotInitializeObserverMetrics(t *testing.T) {
cfg := configmock.NewFromYAML(t, `
anomaly_detection:
metrics:
enabled: true
processing_rules:
- type: exclude_at_match
name: drop_dogstatsd
source: dogstatsd
`)
lc := &testLifecycle{}
telComp := telemetryimpl.GetCompatComponent()
telComp.Reset()
t.Cleanup(telComp.Reset)
provides, err := NewComponent(Requires{
Lifecycle: lc,
Config: cfg,
Log: noopLogComponent{},
Telemetry: telComp,
})
require.NoError(t, err)
handle := provides.Comp.GetHandle("dogstatsd")
_, ok := handle.(*noopObserveHandle)
require.Truef(t, ok, `GetHandle("dogstatsd") returned %T, want *noopObserveHandle`, handle)
requireNoObserverMetricFamilies(t, telComp)
}
func TestNewComponentReadsInactiveSeriesEvictionStorageConfig(t *testing.T) {
tt := []struct {
name string
storageConfig string
wantTTL int64
wantInterval int64
}{
{
name: "configured",
storageConfig: `
inactive_series_ttl: 30m
inactive_series_check_interval: 10m`,
wantTTL: 30 * 60,
wantInterval: 10 * 60,
},
{
name: "disabled with zero",
storageConfig: `
inactive_series_ttl: 0s
inactive_series_check_interval: 0s`,
wantTTL: 0,
wantInterval: 0,
},
{
name: "negative values retain defaults",
storageConfig: `
inactive_series_ttl: -1s
inactive_series_check_interval: -1s`,
wantTTL: storageInactiveSeriesTTLSeconds,
wantInterval: storageInactiveSeriesCheckIntervalSeconds,
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
cfg := configmock.NewFromYAML(t, `
anomaly_detection:
reporting:
events:
enabled: true
storage:
`+tc.storageConfig)
provides, err := NewComponent(Requires{
Lifecycle: &testLifecycle{},
Config: cfg,
Log: noopLogComponent{},
})
require.NoError(t, err)
obs, ok := provides.Comp.(*observerImpl)
require.True(t, ok)
require.Equal(t, tc.wantTTL, obs.engine.storage.cfg.InactiveSeriesTTLSeconds)
require.Equal(t, tc.wantInterval, obs.engine.storage.cfg.InactiveSeriesCheckIntervalSeconds)
})
}
}