Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
aos
GitHub Repository: aos/grafana-agent
Path: blob/main/component/phlare/scrape/manager_test.go
4096 views
1
package scrape
2
3
import (
4
"context"
5
"testing"
6
"time"
7
8
"github.com/grafana/agent/component/phlare"
9
"github.com/grafana/agent/pkg/util"
10
"github.com/prometheus/common/model"
11
"github.com/prometheus/prometheus/discovery/targetgroup"
12
"github.com/prometheus/prometheus/model/labels"
13
"github.com/stretchr/testify/require"
14
)
15
16
func TestManager(t *testing.T) {
17
reloadInterval = time.Millisecond
18
19
m := NewManager(phlare.AppendableFunc(func(ctx context.Context, labels labels.Labels, samples []*phlare.RawSample) error {
20
return nil
21
}), util.TestLogger(t))
22
23
defer m.Stop()
24
targetSetsChan := make(chan map[string][]*targetgroup.Group)
25
require.NoError(t, m.ApplyConfig(NewDefaultArguments()))
26
go m.Run(targetSetsChan)
27
28
targetSetsChan <- map[string][]*targetgroup.Group{
29
"group1": {
30
{
31
Targets: []model.LabelSet{
32
{model.AddressLabel: "localhost:9090"},
33
{model.AddressLabel: "localhost:8080"},
34
},
35
Labels: model.LabelSet{"foo": "bar"},
36
},
37
},
38
}
39
require.Eventually(t, func() bool {
40
return len(m.TargetsActive()["group1"]) == 10
41
}, time.Second, 10*time.Millisecond)
42
43
new := NewDefaultArguments()
44
new.ScrapeInterval = 1 * time.Second
45
46
// Trigger a config reload
47
require.NoError(t, m.ApplyConfig(new))
48
49
targetSetsChan <- map[string][]*targetgroup.Group{
50
"group2": {
51
{
52
Targets: []model.LabelSet{
53
{model.AddressLabel: "localhost:9090"},
54
{model.AddressLabel: "localhost:8080"},
55
},
56
Labels: model.LabelSet{"foo": "bar"},
57
},
58
},
59
}
60
61
require.Eventually(t, func() bool {
62
return len(m.TargetsActive()["group2"]) == 10
63
}, time.Second, 10*time.Millisecond)
64
65
for _, ts := range m.targetsGroups {
66
require.Equal(t, 1*time.Second, ts.config.ScrapeInterval)
67
}
68
69
targetSetsChan <- map[string][]*targetgroup.Group{"group1": {}, "group2": {}}
70
71
require.Eventually(t, func() bool {
72
return len(m.TargetsAll()["group2"]) == 0 && len(m.TargetsAll()["group1"]) == 0
73
}, time.Second, 10*time.Millisecond)
74
}
75
76