Skip to content

Commit

Permalink
ruler: Remove dependency on dskit cache package (#10795)
Browse files Browse the repository at this point in the history
Remove a dependency on dskit/cache that was used only for a DNS
service discovery interface. Instead, define the expected interface
where it is consumed in the ruler.

Signed-off-by: Nick Pillitteri <[email protected]>
  • Loading branch information
56quarters authored Mar 4, 2025
1 parent b561724 commit 9d1ae28
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 51 deletions.
3 changes: 1 addition & 2 deletions pkg/ruler/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/cache"
"github.com/grafana/dskit/concurrency"
"github.com/grafana/dskit/user"
ot "github.com/opentracing/opentracing-go"
Expand Down Expand Up @@ -60,7 +59,7 @@ type DefaultMultiTenantManager struct {
rulerIsRunning atomic.Bool
}

func NewDefaultMultiTenantManager(cfg Config, managerFactory ManagerFactory, reg prometheus.Registerer, logger log.Logger, dnsResolver cache.AddressProvider) (*DefaultMultiTenantManager, error) {
func NewDefaultMultiTenantManager(cfg Config, managerFactory ManagerFactory, reg prometheus.Registerer, logger log.Logger, dnsResolver AddressProvider) (*DefaultMultiTenantManager, error) {
refreshMetrics := discovery.NewRefreshMetrics(reg)
ncfg, err := buildNotifierConfig(&cfg, dnsResolver, refreshMetrics)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions pkg/ruler/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

gklog "github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/grafana/dskit/cache"
"github.com/grafana/dskit/cancellation"
"github.com/grafana/dskit/crypto/tls"
"github.com/grafana/dskit/flagext"
Expand Down Expand Up @@ -142,7 +141,7 @@ func (rn *rulerNotifier) stop() {

// Builds a Prometheus config.Config from a ruler.Config with just the required
// options to configure notifications to Alertmanager.
func buildNotifierConfig(rulerConfig *Config, resolver cache.AddressProvider, rmi discovery.RefreshMetricsManager) (*config.Config, error) {
func buildNotifierConfig(rulerConfig *Config, resolver AddressProvider, rmi discovery.RefreshMetricsManager) (*config.Config, error) {
if rulerConfig.AlertmanagerURL == "" {
// no AM URLs were provided, so we can just return a default config without errors
return &config.Config{}, nil
Expand Down
36 changes: 18 additions & 18 deletions pkg/ruler/notifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -247,9 +247,9 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "alertmanager.mimir.svc.cluster.local:8080",
RefreshInterval: time.Second,
QType: dns.A,
host: "alertmanager.mimir.svc.cluster.local:8080",
refreshInterval: time.Second,
qType: dns.A,
},
},
},
Expand All @@ -259,9 +259,9 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/am",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager2.mimir.svc.cluster.local",
RefreshInterval: time.Second,
QType: dns.SRV,
host: "_http._tcp.alertmanager2.mimir.svc.cluster.local",
refreshInterval: time.Second,
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -292,8 +292,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -329,8 +329,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -368,8 +368,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -413,8 +413,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down Expand Up @@ -459,8 +459,8 @@ func TestBuildNotifierConfig(t *testing.T) {
PathPrefix: "/alertmanager",
ServiceDiscoveryConfigs: discovery.Configs{
dnsServiceDiscovery{
Host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
QType: dns.SRV,
host: "_http._tcp.alertmanager-0.default.svc.cluster.local",
qType: dns.SRV,
},
},
},
Expand Down
48 changes: 27 additions & 21 deletions pkg/ruler/service_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/url"
"time"

"github.com/grafana/dskit/cache"
"github.com/grafana/dskit/dns"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -26,14 +25,31 @@ func init() {
discovery.RegisterConfig(dnsServiceDiscovery{})
}

type dnsServiceDiscovery struct {
refreshMetrics discovery.RefreshMetricsInstantiator
// AddressProvider performs node address resolution given a list of addresses.
type AddressProvider interface {
// Resolve resolves the provided list of addresses to the actual nodes
Resolve(context.Context, []string) error

Resolver cache.AddressProvider
// Addresses returns the nodes
Addresses() []string
}

RefreshInterval time.Duration
QType dns.QType
Host string
type dnsServiceDiscovery struct {
refreshMetrics discovery.RefreshMetricsInstantiator
resolver AddressProvider
refreshInterval time.Duration
qType dns.QType
host string
}

func dnsSD(rulerConfig *Config, resolver AddressProvider, qType dns.QType, url *url.URL, rmi discovery.RefreshMetricsInstantiator) discovery.Config {
return dnsServiceDiscovery{
resolver: resolver,
refreshInterval: rulerConfig.AlertmanagerRefreshInterval,
host: url.Host,
qType: qType,
refreshMetrics: rmi,
}
}

func (dnsServiceDiscovery) Name() string {
Expand All @@ -44,7 +60,7 @@ func (c dnsServiceDiscovery) NewDiscoverer(opts discovery.DiscovererOptions) (di
return refresh.NewDiscovery(refresh.Options{
Logger: opts.Logger,
Mech: mechanismName,
Interval: c.RefreshInterval,
Interval: c.refreshInterval,
RefreshF: c.resolve,
MetricsInstantiator: c.refreshMetrics,
}), nil
Expand All @@ -55,11 +71,11 @@ func (c dnsServiceDiscovery) NewDiscovererMetrics(prometheus.Registerer, discove
}

func (c dnsServiceDiscovery) resolve(ctx context.Context) ([]*targetgroup.Group, error) {
if err := c.Resolver.Resolve(ctx, []string{string(c.QType) + "+" + c.Host}); err != nil {
if err := c.resolver.Resolve(ctx, []string{string(c.qType) + "+" + c.host}); err != nil {
return nil, err
}

resolved := c.Resolver.Addresses()
resolved := c.resolver.Addresses()
targets := make([]model.LabelSet, len(resolved))
for i, r := range resolved {
targets[i] = model.LabelSet{
Expand All @@ -69,22 +85,12 @@ func (c dnsServiceDiscovery) resolve(ctx context.Context) ([]*targetgroup.Group,

tg := &targetgroup.Group{
Targets: targets,
Source: c.Host,
Source: c.host,
}

return []*targetgroup.Group{tg}, nil
}

func dnsSD(rulerConfig *Config, resolver cache.AddressProvider, qType dns.QType, url *url.URL, rmi discovery.RefreshMetricsInstantiator) discovery.Config {
return dnsServiceDiscovery{
Resolver: resolver,
RefreshInterval: rulerConfig.AlertmanagerRefreshInterval,
Host: url.Host,
QType: qType,
refreshMetrics: rmi,
}
}

func staticTarget(url *url.URL) discovery.Config {
return discovery.StaticConfig{
{
Expand Down
16 changes: 8 additions & 8 deletions pkg/ruler/service_discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func TestConfig_TranslatesToPrometheusTargetGroup(t *testing.T) {
reg := prometheus.NewRegistry()
refreshMetrics := discovery.NewRefreshMetrics(reg)
cfg := dnsServiceDiscovery{
RefreshInterval: time.Millisecond,
Resolver: resolver,
QType: dns.A,
Host: sourceAddress,
refreshInterval: time.Millisecond,
resolver: resolver,
qType: dns.A,
host: sourceAddress,
refreshMetrics: refreshMetrics,
}
discoverer, err := cfg.NewDiscoverer(discovery.DiscovererOptions{})
Expand Down Expand Up @@ -129,10 +129,10 @@ func TestConfig_ConstructsLookupNamesCorrectly(t *testing.T) {
reg := prometheus.NewRegistry()
refreshMetrics := discovery.NewRefreshMetrics(reg)
cfg := dnsServiceDiscovery{
RefreshInterval: time.Millisecond,
Resolver: resolver,
QType: tc.qType,
Host: tc.host,
refreshInterval: time.Millisecond,
resolver: resolver,
qType: tc.qType,
host: tc.host,
refreshMetrics: refreshMetrics,
}
discoverer, err := cfg.NewDiscoverer(discovery.DiscovererOptions{})
Expand Down

0 comments on commit 9d1ae28

Please sign in to comment.