Skip to content

Commit

Permalink
feat(receiver): add new receiver type
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed Jul 26, 2023
1 parent 10a0f6a commit b213392
Show file tree
Hide file tree
Showing 28 changed files with 885 additions and 57 deletions.
20 changes: 12 additions & 8 deletions cli/deps.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/goto/siren/plugins/receivers/httpreceiver"
"github.com/goto/siren/plugins/receivers/pagerduty"
"github.com/goto/siren/plugins/receivers/slack"
"github.com/goto/siren/plugins/receivers/slackchannel"
"github.com/newrelic/go-agent/v3/newrelic"
)

Expand Down Expand Up @@ -101,6 +102,7 @@ func InitDeps(

// plugin receiver services
slackPluginService := slack.NewPluginService(cfg.Receivers.Slack, encryptor)
slackChannelPluginService := slackchannel.NewPluginService(cfg.Receivers.Slack, encryptor)
pagerDutyPluginService := pagerduty.NewPluginService(cfg.Receivers.Pagerduty)
httpreceiverPluginService := httpreceiver.NewPluginService(logger, cfg.Receivers.HTTPReceiver)
filePluginService := file.NewPluginService()
Expand All @@ -109,10 +111,11 @@ func InitDeps(
receiverService := receiver.NewService(
receiverRepository,
map[string]receiver.ConfigResolver{
receiver.TypeSlack: slackPluginService,
receiver.TypeHTTP: httpreceiverPluginService,
receiver.TypePagerDuty: pagerDutyPluginService,
receiver.TypeFile: filePluginService,
receiver.TypeSlack: slackPluginService,
receiver.TypeSlackChannel: slackChannelPluginService,
receiver.TypeHTTP: httpreceiverPluginService,
receiver.TypePagerDuty: pagerDutyPluginService,
receiver.TypeFile: filePluginService,
},
)

Expand All @@ -126,10 +129,11 @@ func InitDeps(

// notification
notifierRegistry := map[string]notification.Notifier{
receiver.TypeSlack: slackPluginService,
receiver.TypePagerDuty: pagerDutyPluginService,
receiver.TypeHTTP: httpreceiverPluginService,
receiver.TypeFile: filePluginService,
receiver.TypeSlack: slackPluginService,
receiver.TypeSlackChannel: slackChannelPluginService,
receiver.TypePagerDuty: pagerDutyPluginService,
receiver.TypeHTTP: httpreceiverPluginService,
receiver.TypeFile: filePluginService,
}

idempotencyRepository := postgres.NewIdempotencyRepository(pgClient)
Expand Down
3 changes: 1 addition & 2 deletions core/notification/dispatch_receiver_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strconv"

"github.com/goto/siren/core/log"
"github.com/goto/siren/core/receiver"
"github.com/goto/siren/pkg/errors"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ func (s *DispatchReceiverService) PrepareMessage(ctx context.Context, n Notifica
return nil, nil, false, err
}

rcv, err := s.receiverService.Get(ctx, receiverID, receiver.GetWithData(false))
rcv, err := s.receiverService.Get(ctx, receiverID)
if err != nil {
return nil, nil, false, err
}
Expand Down
2 changes: 2 additions & 0 deletions core/receiver/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package receiver

type Filter struct {
ReceiverIDs []uint64
Labels map[string]string
Expanded bool
}
29 changes: 15 additions & 14 deletions core/receiver/mocks/config_resolver.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/receiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type Receiver struct {
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`

// The pointer to receiver parent of a child receiver. This field is required if a receiver is a child receiver
// If ParentID != 0, the receiver is a child receiver.
ParentID uint64 `json:"parent_id"`

// Type should be immutable
Type string `json:"type"`
}
2 changes: 1 addition & 1 deletion core/receiver/receiver_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ import "context"
//go:generate mockery --name=ConfigResolver -r --case underscore --with-expecter --structname ConfigResolver --filename config_resolver.go --output=./mocks
type ConfigResolver interface {
BuildData(ctx context.Context, configs map[string]any) (map[string]any, error)
PreHookDBTransformConfigs(ctx context.Context, configs map[string]any) (map[string]any, error)
PreHookDBTransformConfigs(ctx context.Context, configs map[string]any, parentID uint64) (map[string]any, error)
PostHookDBTransformConfigs(ctx context.Context, configs map[string]any) (map[string]any, error)
}
73 changes: 68 additions & 5 deletions core/receiver/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package receiver

import (
"context"
"fmt"

"github.com/goto/siren/pkg/errors"
"github.com/goto/siren/pkg/telemetry"
Expand Down Expand Up @@ -35,6 +36,13 @@ func (s *Service) List(ctx context.Context, flt Filter) ([]Receiver, error) {
return nil, err
}

if flt.Expanded {
receivers, err = s.ExpandParents(ctx, receivers)
if err != nil {
return nil, err
}
}

domainReceivers := make([]Receiver, 0, len(receivers))
for i := 0; i < len(receivers); i++ {
rcv := receivers[i]
Expand All @@ -60,7 +68,7 @@ func (s *Service) Create(ctx context.Context, rcv *Receiver) error {
return err
}

rcv.Configurations, err = receiverPlugin.PreHookDBTransformConfigs(ctx, rcv.Configurations)
rcv.Configurations, err = receiverPlugin.PreHookDBTransformConfigs(ctx, rcv.Configurations, rcv.ParentID)
if err != nil {
telemetry.IncrementInt64Counter(ctx, telemetry.MetricReceiverHookFailed,
tag.Upsert(telemetry.TagReceiverType, rcv.Type),
Expand All @@ -79,14 +87,21 @@ func (s *Service) Create(ctx context.Context, rcv *Receiver) error {
}

type getOpts struct {
withData bool
withData bool
withExpand bool
}

type GetOption func(*getOpts)

func GetWithData(withData bool) GetOption {
func GetWithData() GetOption {
return func(g *getOpts) {
g.withData = withData
g.withData = true
}
}

func GetWithExpand() GetOption {
return func(g *getOpts) {
g.withExpand = true
}
}

Expand All @@ -105,6 +120,14 @@ func (s *Service) Get(ctx context.Context, id uint64, gopts ...GetOption) (*Rece
return nil, err
}

if opt.withExpand {
receivers, err := s.ExpandParents(ctx, []Receiver{*rcv})
if err != nil {
return nil, err
}
rcv = &receivers[0]
}

receiverPlugin, err := s.getReceiverPlugin(rcv.Type)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,7 +170,7 @@ func (s *Service) Update(ctx context.Context, rcv *Receiver) error {
return err
}

rcv.Configurations, err = receiverPlugin.PreHookDBTransformConfigs(ctx, rcv.Configurations)
rcv.Configurations, err = receiverPlugin.PreHookDBTransformConfigs(ctx, rcv.Configurations, rcv.ParentID)
if err != nil {
return err
}
Expand All @@ -165,3 +188,43 @@ func (s *Service) Update(ctx context.Context, rcv *Receiver) error {
func (s *Service) Delete(ctx context.Context, id uint64) error {
return s.repository.Delete(ctx, id)
}

func (s *Service) ExpandParents(ctx context.Context, rcvs []Receiver) ([]Receiver, error) {
var uniqueParentIDsMap = map[uint64]bool{}
for _, rcv := range rcvs {
if rcv.ParentID != 0 {
uniqueParentIDsMap[rcv.ParentID] = true
}
}
if len(uniqueParentIDsMap) == 0 {
return rcvs, nil
}

var uniqueParentIDs []uint64
for k := range uniqueParentIDsMap {
uniqueParentIDs = append(uniqueParentIDs, k)
}

parentReceivers, err := s.List(ctx, Filter{ReceiverIDs: uniqueParentIDs})
if err != nil {
return nil, fmt.Errorf("failure when expanding receiver parents: %w", err)
}

var parentReceiversMap = map[uint64]Receiver{}
for _, parentRcv := range parentReceivers {
parentReceiversMap[parentRcv.ID] = parentRcv
}

// enrich existing receivers
for _, rcv := range rcvs {
if rcv.ParentID != 0 {
if len(parentReceiversMap[rcv.ParentID].Configurations) != 0 {
for k, v := range parentReceiversMap[rcv.ParentID].Configurations {
rcv.Configurations[k] = v
}
}
}
}

return rcvs, nil
}
Loading

0 comments on commit b213392

Please sign in to comment.