Skip to content

Commit

Permalink
[target-allocator] drop unnecessary job to scrape config map
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikołaj Świątek committed Apr 10, 2023
1 parent 45996a9 commit 27bdc2a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
11 changes: 4 additions & 7 deletions cmd/otel-allocator/target/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ type Discoverer struct {
manager *discovery.Manager
close chan struct{}
configsMap map[allocatorWatcher.EventSource]*config.Config
jobToScrapeConfig map[string]*config.ScrapeConfig
hook discoveryHook
scrapeConfigsHash uint64
scrapeConfigsUpdater scrapeConfigsUpdater
Expand All @@ -59,41 +58,39 @@ func NewDiscoverer(log logr.Logger, manager *discovery.Manager, hook discoveryHo
manager: manager,
close: make(chan struct{}),
configsMap: make(map[allocatorWatcher.EventSource]*config.Config),
jobToScrapeConfig: make(map[string]*config.ScrapeConfig),
hook: hook,
scrapeConfigsUpdater: scrapeConfigsUpdater,
}
}

func (m *Discoverer) ApplyConfig(source allocatorWatcher.EventSource, cfg *config.Config) error {
m.configsMap[source] = cfg
newJobToScrapeConfig := make(map[string]*config.ScrapeConfig)
jobToScrapeConfig := make(map[string]*config.ScrapeConfig)

discoveryCfg := make(map[string]discovery.Configs)
relabelCfg := make(map[string][]*relabel.Config)

for _, value := range m.configsMap {
for _, scrapeConfig := range value.ScrapeConfigs {
newJobToScrapeConfig[scrapeConfig.JobName] = scrapeConfig
jobToScrapeConfig[scrapeConfig.JobName] = scrapeConfig
discoveryCfg[scrapeConfig.JobName] = scrapeConfig.ServiceDiscoveryConfigs
relabelCfg[scrapeConfig.JobName] = scrapeConfig.RelabelConfigs
}
}

hash, err := hashstructure.Hash(newJobToScrapeConfig, nil)
hash, err := hashstructure.Hash(jobToScrapeConfig, nil)
if err != nil {
return err
}
// If the hash has changed, updated stored hash and send the new config.
// Otherwise skip updating scrape configs.
if m.scrapeConfigsUpdater != nil && m.scrapeConfigsHash != hash {
err := m.scrapeConfigsUpdater.UpdateScrapeConfigResponse(newJobToScrapeConfig)
err := m.scrapeConfigsUpdater.UpdateScrapeConfigResponse(jobToScrapeConfig)
if err != nil {
return err
}

m.scrapeConfigsHash = hash
m.jobToScrapeConfig = newJobToScrapeConfig // only assign after we've successfully updated
}

if m.hook != nil {
Expand Down
16 changes: 9 additions & 7 deletions cmd/otel-allocator/target/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ func TestDiscovery_ScrapeConfigHashing(t *testing.T) {
}
var (
lastValidHash uint64
expectedConfig map[string]*promconfig.ScrapeConfig
lastValidConfig map[string]*promconfig.ScrapeConfig
)

Expand All @@ -306,24 +307,25 @@ func TestDiscovery_ScrapeConfigHashing(t *testing.T) {
t.Run(tc.description, func(t *testing.T) {
err := manager.ApplyConfig(allocatorWatcher.EventSourcePrometheusCR, tc.cfg)
if !tc.expectErr {
expectedConfig = make(map[string]*promconfig.ScrapeConfig)
for _, value := range manager.configsMap {
for _, scrapeConfig := range value.ScrapeConfigs {
expectedConfig[scrapeConfig.JobName] = scrapeConfig
}
}
assert.NoError(t, err)
assert.NotZero(t, manager.scrapeConfigsHash)
// Assert that scrape configs in manager are correctly
// reflected in the scrape job updater.
assert.Equal(t, manager.jobToScrapeConfig, scu.mockCfg)
assert.Equal(t, expectedConfig, scu.mockCfg)

lastValidHash = manager.scrapeConfigsHash
// make a copy of the config, or else it'll change underneath us
lastValidConfig = make(map[string]*promconfig.ScrapeConfig)
for key, value := range manager.jobToScrapeConfig {
lastValidConfig[key] = value
}
lastValidConfig = expectedConfig
} else {
// In case of error, assert that we retain the last
// known valid config.
assert.Error(t, err)
assert.Equal(t, lastValidHash, manager.scrapeConfigsHash)
assert.Equal(t, lastValidConfig, manager.jobToScrapeConfig)
assert.Equal(t, lastValidConfig, scu.mockCfg)
}

Expand Down

0 comments on commit 27bdc2a

Please sign in to comment.