Skip to content

Commit

Permalink
Merge pull request #51 from lidofinance/filter-alerts
Browse files Browse the repository at this point in the history
feature: Filter only desired Findings
  • Loading branch information
sergeyWh1te authored Dec 9, 2024
2 parents 11e95cb + d32b738 commit a4374c7
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 19 deletions.
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 09.12.2024
1. Dynamic config feature: filter only desired Findings.

## 05.12.2024
1. Remove PublishedAlerts metrics
2. Added NotifyChannels: `forwarder_notification_channel_error_total`
Expand Down
23 changes: 16 additions & 7 deletions internal/env/notify_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ type OpsGenieChannel struct {
}

type Consumer struct {
ConsumerName string `mapstructure:"consumerName"`
Type string `mapstructure:"type"`
ChannelID string `mapstructure:"channel_id"`
Severities []string `mapstructure:"severities"`
ByQuorum bool `mapstructure:"by_quorum"`
Subjects []string `mapstructure:"subjects"`
SeveritySet registry.FindingMapping
ConsumerName string `mapstructure:"consumerName"`
Type string `mapstructure:"type"`
ChannelID string `mapstructure:"channel_id"`
Severities []string `mapstructure:"severities"`
ByQuorum bool `mapstructure:"by_quorum"`
Subjects []string `mapstructure:"subjects"`
Filter []string `mapstructure:"filter"`
SeveritySet registry.FindingMapping
FindingFilterMap registry.FindingFilterMap
}

type NotificationConfig struct {
Expand Down Expand Up @@ -150,14 +152,21 @@ func ValidateConfig(cfg *NotificationConfig) error {

for _, consumer := range cfg.Consumers {
severitySet := make(registry.FindingMapping)
findingFilter := make(registry.FindingFilterMap)

for _, severity := range consumer.Severities {
if _, exists := validSeverities[databus.Severity(severity)]; !exists {
return fmt.Errorf("consumer '%s' references an unknown severity level '%s'", consumer.ConsumerName, severity)
}
severitySet[databus.Severity(severity)] = true
}

for _, alertID := range consumer.Filter {
findingFilter[alertID] = true
}

consumer.SeveritySet = severitySet
consumer.FindingFilterMap = findingFilter
}

for _, consumer := range cfg.Consumers {
Expand Down
35 changes: 23 additions & 12 deletions internal/pkg/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ type Consumer struct {
redisClient *redis.Client
repo *Repo

name string
subject string
severitySet registry.FindingMapping
byQuorum bool
quorumSize uint
notifier notifiler.FindingSender
name string
subject string
severitySet registry.FindingMapping
byQuorum bool
quorumSize uint
findingFilterMap registry.FindingFilterMap
notifier notifiler.FindingSender
}

func New(
Expand All @@ -50,6 +51,7 @@ func New(
consumerName,
subject string,
SeveritySet registry.FindingMapping,
findingFilterMap registry.FindingFilterMap,
byQuorum bool,
quorumSize uint,
notifier notifiler.FindingSender,
Expand All @@ -61,12 +63,13 @@ func New(
redisClient: redisClient,
repo: repo,

name: consumerName,
subject: subject,
severitySet: SeveritySet,
byQuorum: byQuorum,
quorumSize: quorumSize,
notifier: notifier,
name: consumerName,
subject: subject,
severitySet: SeveritySet,
findingFilterMap: findingFilterMap,
byQuorum: byQuorum,
quorumSize: quorumSize,
notifier: notifier,
}
}

Expand Down Expand Up @@ -116,6 +119,7 @@ func NewConsumers(log *slog.Logger, metrics *metrics.Store, redisClient *redis.C
consumerName,
subject,
consumerCfg.SeveritySet,
consumerCfg.FindingFilterMap,
consumerCfg.ByQuorum,
quorumSize,
notificationChannel,
Expand Down Expand Up @@ -172,6 +176,13 @@ func (c *Consumer) GetConsumeHandler(ctx context.Context) func(msg jetstream.Msg
return
}

if len(c.findingFilterMap) > 0 {
if _, ok := c.findingFilterMap[finding.AlertId]; !ok {
c.ackMessage(msg)
return
}
}

if c.byQuorum == false {
if sendErr := c.notifier.SendFinding(ctx, finding); sendErr != nil {
c.log.Error(fmt.Sprintf(`Could not send bot-finding: %v`, sendErr),
Expand Down
1 change: 1 addition & 0 deletions internal/utils/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ var CodeOwners = map[string]string{
}

type FindingMapping = map[databus.Severity]bool
type FindingFilterMap = map[string]bool
19 changes: 19 additions & 0 deletions notification.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ discord_channels:
- id: Discord2
description: Discord channel for all messages >= Low, no quorum
webhook_url: YOUR_DISCORD_WEBHOOK_URL_2
- id: AnalyticsSubscriber
description: "Analytics channel for getting only specific findings (filter: WITHDRAWALS-BIG-WITHDRAWAL-REQUEST-BATCH)"
webhook_url: YOUR_DISCORD_WEBHOOK_URL_2

opsgenie_channels:
- id: OpsGenie1
Expand Down Expand Up @@ -125,3 +128,19 @@ consumers:
- findings.protocol.steth
- findings.protocol.arb
- findings.dao.opt

- consumerName: AnalyticsSubscriber
type: Discord
channel_id: AnalyticsSubscriber
severities:
- Low
- Info
- Medium
- High
- Critical
by_quorum: True
subjects:
- findings.protocol.steth
filter:
- WITHDRAWALS-BIG-WITHDRAWAL-REQUEST-BATCH

0 comments on commit a4374c7

Please sign in to comment.