Path: blob/main/component/phlare/scrape/manager_test.go
4096 views
package scrape12import (3"context"4"testing"5"time"67"github.com/grafana/agent/component/phlare"8"github.com/grafana/agent/pkg/util"9"github.com/prometheus/common/model"10"github.com/prometheus/prometheus/discovery/targetgroup"11"github.com/prometheus/prometheus/model/labels"12"github.com/stretchr/testify/require"13)1415func TestManager(t *testing.T) {16reloadInterval = time.Millisecond1718m := NewManager(phlare.AppendableFunc(func(ctx context.Context, labels labels.Labels, samples []*phlare.RawSample) error {19return nil20}), util.TestLogger(t))2122defer m.Stop()23targetSetsChan := make(chan map[string][]*targetgroup.Group)24require.NoError(t, m.ApplyConfig(NewDefaultArguments()))25go m.Run(targetSetsChan)2627targetSetsChan <- map[string][]*targetgroup.Group{28"group1": {29{30Targets: []model.LabelSet{31{model.AddressLabel: "localhost:9090"},32{model.AddressLabel: "localhost:8080"},33},34Labels: model.LabelSet{"foo": "bar"},35},36},37}38require.Eventually(t, func() bool {39return len(m.TargetsActive()["group1"]) == 1040}, time.Second, 10*time.Millisecond)4142new := NewDefaultArguments()43new.ScrapeInterval = 1 * time.Second4445// Trigger a config reload46require.NoError(t, m.ApplyConfig(new))4748targetSetsChan <- map[string][]*targetgroup.Group{49"group2": {50{51Targets: []model.LabelSet{52{model.AddressLabel: "localhost:9090"},53{model.AddressLabel: "localhost:8080"},54},55Labels: model.LabelSet{"foo": "bar"},56},57},58}5960require.Eventually(t, func() bool {61return len(m.TargetsActive()["group2"]) == 1062}, time.Second, 10*time.Millisecond)6364for _, ts := range m.targetsGroups {65require.Equal(t, 1*time.Second, ts.config.ScrapeInterval)66}6768targetSetsChan <- map[string][]*targetgroup.Group{"group1": {}, "group2": {}}6970require.Eventually(t, func() bool {71return len(m.TargetsAll()["group2"]) == 0 && len(m.TargetsAll()["group1"]) == 072}, time.Second, 10*time.Millisecond)73}747576