-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
basic alerting, for internal use primarily #778
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2ec972
basic alerting, for internal use primarily
heavycrystal 90ace56
activity alerting prototype, not done for all activities yet
heavycrystal 42a62b0
xremoved activity alerts for now, added slot size alert
heavycrystal 2e3e6a0
slot size alert only on env set
heavycrystal 95bb209
fixing up merge issues
heavycrystal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,9 +43,6 @@ type CDCPullConnector interface { | |
// PullFlowCleanup drops both the Postgres publication and replication slot, as a part of DROP MIRROR | ||
PullFlowCleanup(jobName string) error | ||
|
||
// SendWALHeartbeat allows for activity to progress restart_lsn on postgres. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this being removed because it's unused? Should be split out to another PR |
||
SendWALHeartbeat() error | ||
|
||
// GetSlotInfo returns the WAL (or equivalent) info of a slot for the connector. | ||
GetSlotInfo(slotName string) ([]*protos.SlotInfo, error) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package evervigil | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/nikoksr/notify" | ||
serprex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"github.com/nikoksr/notify/service/slack" | ||
"github.com/sirupsen/logrus" | ||
heavycrystal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
// alerting service, cool name | ||
type EverVigil struct { | ||
notifier *notify.Notify | ||
catalogPool *pgxpool.Pool | ||
} | ||
|
||
type slackServiceConfig struct { | ||
AuthToken string `json:"auth_token"` | ||
ChannelIDs []string `json:"channel_ids"` | ||
} | ||
|
||
func registerServicesForNotifier(catalogPool *pgxpool.Pool) (*notify.Notify, error) { | ||
notifier := notify.New() | ||
|
||
rows, err := catalogPool.Query(context.Background(), | ||
"SELECT service_type,service_config FROM peerdb_stats.alerting_config") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read everVigil config from catalog: %w", err) | ||
} | ||
|
||
registeredAtleastOneService := false | ||
var serviceType, serviceConfig string | ||
_, err = pgx.ForEachRow(rows, []any{&serviceType, &serviceConfig}, func() error { | ||
switch serviceType { | ||
case "slack": | ||
var slackServiceConfig slackServiceConfig | ||
err = json.Unmarshal([]byte(serviceConfig), &slackServiceConfig) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal Slack service config: %w", err) | ||
} | ||
|
||
slackService := slack.New(slackServiceConfig.AuthToken) | ||
slackService.AddReceivers(slackServiceConfig.ChannelIDs...) | ||
notifier.UseServices(slackService) | ||
registeredAtleastOneService = true | ||
default: | ||
return fmt.Errorf("unknown service type: %s", serviceType) | ||
} | ||
return nil | ||
}) | ||
|
||
// vigil is currently useless, marking it as such | ||
if !registeredAtleastOneService { | ||
notifier.Disabled = true | ||
} | ||
return notifier, nil | ||
} | ||
|
||
func NewVigil(catalogPool *pgxpool.Pool) (*EverVigil, error) { | ||
notifier, err := registerServicesForNotifier(catalogPool) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &EverVigil{ | ||
notifier: notifier, | ||
catalogPool: catalogPool, | ||
serprex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, nil | ||
} | ||
|
||
func (ev *EverVigil) Close() { | ||
if ev.catalogPool != nil { | ||
ev.catalogPool.Close() | ||
} | ||
Comment on lines
+77
to
+79
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catalog pool should never be closed |
||
} | ||
|
||
// Only raises an alert if another alert with the same key hasn't been raised | ||
// in the past 15 minutes | ||
func (ev *EverVigil) AlertIf(alertKey string, alertMessage string) { | ||
if ev.catalogPool != nil && ev.notifier != nil { | ||
// try to make the vigil not useless if possible | ||
if ev.notifier.Disabled { | ||
var err error | ||
ev.notifier, err = registerServicesForNotifier(ev.catalogPool) | ||
if err != nil { | ||
logrus.Warnf("failed to register services for vigil: %v", err) | ||
return | ||
} | ||
} | ||
|
||
row := ev.catalogPool.QueryRow(context.Background(), | ||
`SELECT created_timestamp FROM peerdb_stats.alerts_v1 WHERE alert_key=$1 | ||
ORDER BY created_timestamp DESC LIMIT 1`, | ||
alertKey) | ||
var createdTimestamp time.Time | ||
err := row.Scan(&createdTimestamp) | ||
if err != nil && err != pgx.ErrNoRows { | ||
logrus.Warnf("failed to send alert: %v", err) | ||
return | ||
} | ||
|
||
if time.Since(createdTimestamp) >= 15*time.Minute { | ||
err = ev.notifier.Send(context.Background(), | ||
fmt.Sprintf(":rotating_light: *Alert Alert* :rotating_light:: %s since %s", alertKey, | ||
time.Now().Format("2006-01-02 15:04:05.999999")), alertMessage) | ||
if err != nil { | ||
logrus.Warnf("failed to send alert: %v", err) | ||
return | ||
} | ||
_, _ = ev.catalogPool.Exec(context.Background(), | ||
"INSERT INTO peerdb_stats.alerts_v1(alert_key,alert_message) VALUES($1,$2)", | ||
alertKey, alertMessage) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE IF NOT EXISTS peerdb_stats.alerting_config ( | ||
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, | ||
service_type TEXT NOT NULL CHECK (service_type IN ('slack')), | ||
service_config JSONB NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS peerdb_stats.alerts_v1 ( | ||
id BIGINT PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, | ||
alert_key TEXT NOT NULL, | ||
alert_level TEXT NOT NULL CHECK (alert_level IN ('critical')) DEFAULT 'critical', | ||
alert_message TEXT NOT NULL, | ||
created_timestamp TIMESTAMP DEFAULT now() | ||
); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.