Skip to content

Commit

Permalink
feat: update proto and add some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mabdh committed Jul 27, 2023
1 parent 0e55f17 commit f6fc9b0
Show file tree
Hide file tree
Showing 12 changed files with 856 additions and 790 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ NAME="github.com/goto/siren"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
APP_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "4bc9cb827506c196b93895ea687f634fb3d7661c"
PROTON_COMMIT := "c933570586af26a7703b85d54f11e138edf9fc1b"

.PHONY: all build test clean dist vet proto install

Expand Down
31 changes: 18 additions & 13 deletions core/receiver/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +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
}
if !flt.Expanded {
return receivers, nil
}

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

domainReceivers := make([]Receiver, 0, len(receivers))
Expand All @@ -59,6 +61,7 @@ func (s *Service) List(ctx context.Context, flt Filter) ([]Receiver, error) {

domainReceivers = append(domainReceivers, rcv)
}

return domainReceivers, nil
}

Expand All @@ -75,7 +78,7 @@ func (s *Service) Create(ctx context.Context, rcv *Receiver) error {
tag.Upsert(telemetry.TagHookCondition, telemetry.HookConditionPreHookDB),
)

return err
return errors.ErrInvalid.WithMsgf("%s", err.Error())
}

err = s.repository.Create(ctx, rcv)
Expand Down Expand Up @@ -120,19 +123,21 @@ 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]
if !opt.withExpand {
return rcv, nil
}

receiverPlugin, err := s.getReceiverPlugin(rcv.Type)
if err != nil {
return nil, err
}

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

transformedConfigs, err := receiverPlugin.PostHookDBTransformConfigs(ctx, rcv.Configurations)
if err != nil {
telemetry.IncrementInt64Counter(ctx, telemetry.MetricReceiverHookFailed,
Expand Down Expand Up @@ -172,7 +177,7 @@ func (s *Service) Update(ctx context.Context, rcv *Receiver) error {

rcv.Configurations, err = receiverPlugin.PreHookDBTransformConfigs(ctx, rcv.Configurations, rcv.ParentID)
if err != nil {
return err
return errors.ErrInvalid.WithMsgf("%s", err.Error())
}

if err = s.repository.Update(ctx, rcv); err != nil {
Expand Down
8 changes: 2 additions & 6 deletions docker/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ processors:
batch:

exporters:
otlp:
endpoint: https://otlp.nr-data.net:4317
headers:
"api-key": "NEW_RELIC_API_KEY"
prometheusremotewrite:
endpoint: http://host.docker.internal:9009/api/v1/push
tls:
Expand Down Expand Up @@ -37,8 +33,8 @@ service:
traces:
receivers: [opencensus]
processors: [batch]
exporters: [otlp, logging]
exporters: [logging]
metrics:
receivers: [opencensus]
processors: [batch]
exporters: [prometheusremotewrite,otlp, logging]
exporters: [prometheusremotewrite, logging]
2 changes: 2 additions & 0 deletions internal/api/v1beta1/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (s *GRPCServer) CreateReceiver(ctx context.Context, req *sirenv1beta1.Creat
Type: req.GetType(),
Labels: req.GetLabels(),
Configurations: req.GetConfigurations().AsMap(),
ParentID: req.GetParentId(),
}

err := s.receiverService.Create(ctx, rcv)
Expand Down Expand Up @@ -103,6 +104,7 @@ func (s *GRPCServer) UpdateReceiver(ctx context.Context, req *sirenv1beta1.Updat
Name: req.GetName(),
Labels: req.GetLabels(),
Configurations: req.GetConfigurations().AsMap(),
ParentID: req.GetParentId(),
}

if err := s.receiverService.Update(ctx, rcv); err != nil {
Expand Down
19 changes: 14 additions & 5 deletions internal/store/model/receiver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package model

import (
"database/sql"
"time"

"github.com/goto/siren/core/receiver"
Expand All @@ -14,7 +15,7 @@ type Receiver struct {
Labels pgc.StringStringMap `db:"labels"`
Configurations pgc.StringAnyMap `db:"configurations"`
Data pgc.StringAnyMap `db:"-"` //TODO do we need this?
ParentID uint64 `db:"parent_id"`
ParentID sql.NullInt64 `db:"parent_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Expand All @@ -26,7 +27,12 @@ func (rcv *Receiver) FromDomain(t receiver.Receiver) {
rcv.Labels = t.Labels
rcv.Configurations = pgc.StringAnyMap(t.Configurations)
rcv.Data = t.Data
rcv.ParentID = t.ParentID
rcv.ParentID = sql.NullInt64{
Valid: true,
// since postgres does not support unsigned integer and ids in siren is autogenerated by postgres and never be < 0 (bigserial)
// this operation would be safe and no overflow is expected
Int64: int64(t.ParentID),
}
rcv.CreatedAt = t.CreatedAt
rcv.UpdatedAt = t.UpdatedAt
}
Expand All @@ -39,8 +45,11 @@ func (rcv *Receiver) ToDomain() *receiver.Receiver {
Labels: rcv.Labels,
Configurations: rcv.Configurations,
Data: rcv.Data,
ParentID: rcv.ParentID,
CreatedAt: rcv.CreatedAt,
UpdatedAt: rcv.UpdatedAt,

// since postgres does not support unsigned integer and ids in siren is autogenerated by postgres and never be < 0 (bigserial)
// this operation would be safe and no overflow is expected
ParentID: uint64(rcv.ParentID.Int64),
CreatedAt: rcv.CreatedAt,
UpdatedAt: rcv.UpdatedAt,
}
}
9 changes: 8 additions & 1 deletion internal/store/postgres/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package postgres

import (
"context"
"database/sql"
"time"

sq "github.com/Masterminds/squirrel"
Expand Down Expand Up @@ -119,8 +120,14 @@ func (r AlertRepository) List(ctx context.Context, flt alert.Filter) ([]alert.Al

func (r AlertRepository) BulkUpdateSilence(ctx context.Context, alertIDs []int64, silenceStatus string) error {
sqlAlertIDs := pq.Array(alertIDs)
var silenceStatusPG sql.NullString
if silenceStatus == "" {
silenceStatusPG = sql.NullString{Valid: false, String: ""}
} else {
silenceStatusPG = sql.NullString{Valid: true, String: silenceStatus}
}
if _, err := r.client.ExecContext(ctx, pgc.OpUpdate, r.tableName, alertUpdateBulkSilenceQuery,
silenceStatus,
silenceStatusPG,
sqlAlertIDs,
); err != nil {
return err
Expand Down
9 changes: 7 additions & 2 deletions plugins/receivers/slackchannel/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package slackchannel

import (
"fmt"

"github.com/goto/siren/plugins/receivers/slack"
)

Expand All @@ -12,8 +14,8 @@ type ReceiverConfig struct {
}

func (c *ReceiverConfig) Validate() error {
if err := c.SlackReceiverConfig.Validate(); err != nil {
return err
if c.ChannelName == "" {
return fmt.Errorf("invalid slack_channel receiver config, channel_name can't be empty")
}
return nil
}
Expand All @@ -37,6 +39,9 @@ type NotificationConfig struct {
// channel_name is not mandatory because in NotifyToReceiver flow, channel_name
// is being passed from the request (not from the config)
func (c *NotificationConfig) Validate() error {
if err := c.ReceiverConfig.SlackReceiverConfig.Validate(); err != nil {
return err
}
if err := c.ReceiverConfig.Validate(); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions plugins/receivers/slackchannel/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func TestNotificationConfig(t *testing.T) {
Token: "token",
Workspace: "workspace",
},
ChannelName: "a-channel",
},
},
wantErr: false,
Expand Down
13 changes: 12 additions & 1 deletion plugins/receivers/slackchannel/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"

"github.com/goto/siren/core/notification"
"github.com/goto/siren/pkg/errors"
"github.com/goto/siren/plugins/receivers/base"
"github.com/goto/siren/plugins/receivers/slack"
"github.com/mitchellh/mapstructure"
)

// PluginService is a plugin service layer for slack channel type
Expand All @@ -24,7 +26,16 @@ func NewPluginService(cfg slack.AppConfig, cryptoClient slack.Encryptor, opts ..

func (s *PluginService) PreHookDBTransformConfigs(ctx context.Context, configurations map[string]any, parentID uint64) (map[string]any, error) {
if parentID == 0 {
return nil, fmt.Errorf("type `slackchannel` needs receiver parent ID")
return nil, fmt.Errorf("type slack_channel needs receiver parent ID")
}

receiverConfig := &ReceiverConfig{}
if err := mapstructure.Decode(configurations, receiverConfig); err != nil {
return nil, fmt.Errorf("failed to transform configurations to receiver config: %w", err)
}

if err := receiverConfig.Validate(); err != nil {
return nil, errors.ErrInvalid.WithMsgf(err.Error())
}

return configurations, nil
Expand Down
Loading

0 comments on commit f6fc9b0

Please sign in to comment.