Skip to content

Commit

Permalink
feat(alertmanager): complete internal provider
Browse files Browse the repository at this point in the history
  • Loading branch information
grandwizard28 committed Feb 17, 2025
1 parent 831d81e commit 1c69201
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 48 deletions.
2 changes: 2 additions & 0 deletions pkg/alertmanager/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"go.signoz.io/signoz/pkg/errors"
"go.signoz.io/signoz/pkg/factory"
"go.signoz.io/signoz/pkg/types/alertmanagertypes"
)

Expand All @@ -12,6 +13,7 @@ var (
)

type Alertmanager interface {
factory.Service
// GetAlerts gets the alerts from the alertmanager per organization.
GetAlerts(context.Context, string, alertmanagertypes.GettableAlertsParams) (alertmanagertypes.GettableAlerts, error)

Expand Down
66 changes: 38 additions & 28 deletions pkg/alertmanager/internalalertmanager/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"go.signoz.io/signoz/pkg/alertmanager/alertmanagerstore/sqlalertmanagerstore"
"go.signoz.io/signoz/pkg/factory"
"go.signoz.io/signoz/pkg/sqlstore"
"go.signoz.io/signoz/pkg/types/alertmanagertypes"
)

var _ alertmanager.Alertmanager = (*provider)(nil)

type provider struct {
*alertmanager.Service
ticker *time.Ticker
syncC chan struct{}
service *alertmanager.Service
config alertmanager.Config
settings factory.ScopedProviderSettings
}

func NewFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[alertmanager.Alertmanager, alertmanager.Config] {
Expand All @@ -24,37 +23,48 @@ func NewFactory(sqlstore sqlstore.SQLStore) factory.ProviderFactory[alertmanager
})
}

func New(ctx context.Context, settings factory.ProviderSettings, config alertmanager.Config, sqlstore sqlstore.SQLStore) (alertmanager.Alertmanager, error) {
ticker := time.NewTicker(config.Internal.PollInterval)
syncC := make(chan struct{})

func New(ctx context.Context, providerSettings factory.ProviderSettings, config alertmanager.Config, sqlstore sqlstore.SQLStore) (alertmanager.Alertmanager, error) {
settings := factory.NewScopedProviderSettings(providerSettings, "go.signoz.io/signoz/pkg/alertmanager/internalalertmanager")
return &provider{
Service: alertmanager.New(
service: alertmanager.New(
ctx,
factory.NewScopedProviderSettings(settings, "go.signoz.io/signoz/pkg/alertmanager/internalalertmanager"),
settings,
config,
sqlalertmanagerstore.NewStateStore(sqlstore),
sqlalertmanagerstore.NewConfigStore(sqlstore),
alertmanager.SyncFunc(func(ctx context.Context) <-chan struct{} {
go func() {
select {
case <-ticker.C:
syncC <- struct{}{}
case <-ctx.Done():
return
}
}()
return syncC
}),
),
ticker: ticker,
syncC: syncC,
settings: settings,
config: config,
}, nil
}

func (provider *provider) Start(ctx context.Context) error {
ticker := time.NewTicker(provider.config.Internal.PollInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
if err := provider.service.SyncServers(ctx); err != nil {
provider.settings.Logger().ErrorContext(ctx, "failed to sync alertmanager servers", "error", err)
}
}
}
}

func (provider *provider) Stop(ctx context.Context) error {
provider.ticker.Stop()
close(provider.syncC)
provider.Service.Stop(ctx)
return nil
return provider.service.Stop(ctx)
}

func (provider *provider) GetAlerts(ctx context.Context, orgID string, params alertmanagertypes.GettableAlertsParams) (alertmanagertypes.GettableAlerts, error) {
return provider.service.GetAlerts(ctx, orgID, params)
}

func (provider *provider) PutAlerts(ctx context.Context, orgID string, alerts alertmanagertypes.PostableAlerts) error {
return provider.service.PutAlerts(ctx, orgID, alerts)
}

func (provider *provider) TestReceiver(ctx context.Context, orgID string, receiver alertmanagertypes.Receiver) error {
return provider.service.TestReceiver(ctx, orgID, receiver)
}
21 changes: 1 addition & 20 deletions pkg/alertmanager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"go.signoz.io/signoz/pkg/types/alertmanagertypes"
)

type SyncFunc func(context.Context) <-chan struct{}

type Service struct {
// config is the config for the alertmanager service
config Config
Expand All @@ -30,38 +28,21 @@ type Service struct {

// Mutex to protect the servers map
serversMtx sync.RWMutex

// syncC is the channel to trigger the sync of the alertmanager servers
syncFunc SyncFunc
}

func New(ctx context.Context, settings factory.ScopedProviderSettings, config Config, stateStore alertmanagertypes.StateStore, configStore alertmanagertypes.ConfigStore, syncFunc SyncFunc) *Service {
func New(ctx context.Context, settings factory.ScopedProviderSettings, config Config, stateStore alertmanagertypes.StateStore, configStore alertmanagertypes.ConfigStore) *Service {
service := &Service{
config: config,
stateStore: stateStore,
configStore: configStore,
settings: settings,
servers: make(map[string]*server.Server),
serversMtx: sync.RWMutex{},
syncFunc: syncFunc,
}

return service
}

func (service *Service) Start(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
case <-service.syncFunc(ctx):
if err := service.SyncServers(ctx); err != nil {
service.settings.Logger().Error("failed to sync alertmanager servers", "error", err)
}
}
}
}

func (service *Service) SyncServers(ctx context.Context) error {
orgIDs, err := service.configStore.ListOrgs(ctx)
if err != nil {
Expand Down

0 comments on commit 1c69201

Please sign in to comment.