Skip to content

Commit

Permalink
alerting: use ch-proto/pgerrcode constants (#2563)
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex authored Feb 12, 2025
1 parent 2e048eb commit 94ea569
Showing 1 changed file with 18 additions and 34 deletions.
52 changes: 18 additions & 34 deletions flow/alerting/classifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"strings"
"syscall"

chproto "github.com/ClickHouse/ch-go/proto"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"golang.org/x/crypto/ssh"

Expand All @@ -34,23 +36,18 @@ type ErrorClass struct {

var (
ErrorNotifyOOM = ErrorClass{
// ClickHouse Code 241
Class: "NOTIFY_OOM", action: NotifyUser,
}
ErrorNotifyMVOrView = ErrorClass{
// ClickHouse Code 349 / Code 48 with "while pushing to view"
Class: "NOTIFY_MV_OR_VIEW", action: NotifyUser,
}
ErrorNotifyConnectivity = ErrorClass{
// ClickHouse Code 81 or Postgres Code 28P01
Class: "NOTIFY_CONNECTIVITY", action: NotifyUser,
}
ErrorNotifySlotInvalid = ErrorClass{
// Postgres Code 55000 with "cannot read from logical replication slot"
Class: "NOTIFY_SLOT_INVALID", action: NotifyUser,
}
ErrorNotifyTerminate = ErrorClass{
// Postgres Code 57P01
Class: "NOTIFY_TERMINATE", action: NotifyUser,
}
ErrorNotifyConnectTimeout = ErrorClass{
Expand All @@ -62,19 +59,15 @@ var (
Class: "EVENT_INTERNAL", action: NotifyTelemetry,
}
ErrorIgnoreEOF = ErrorClass{
// io.EOF || io.ErrUnexpectedEOF
Class: "IGNORE_EOF", action: Ignore,
}
ErrorIgnoreConnReset = ErrorClass{
// net.OpError with "connection reset by peer"
Class: "IGNORE_CONN_RESET", action: Ignore,
}
ErrorIgnoreContextCancelled = ErrorClass{
// context.Canceled
Class: "IGNORE_CONTEXT_CANCELLED", action: Ignore,
}
ErrorInternalClickHouse = ErrorClass{
// Code 999 or 341
Class: "INTERNAL_CLICKHOUSE", action: NotifyTelemetry,
}
ErrorOther = ErrorClass{
Expand Down Expand Up @@ -111,48 +104,39 @@ func GetErrorClass(ctx context.Context, err error) ErrorClass {
// ClickHouse specific errors
var exception *clickhouse.Exception
if errors.As(err, &exception) {
switch exception.Code {
case 241: // MEMORY_LIMIT_EXCEEDED
switch chproto.Error(exception.Code) {
case chproto.ErrMemoryLimitExceeded:
return ErrorNotifyOOM
case 349: // CANNOT_INSERT_NULL_IN_ORDINARY_COLUMN
case chproto.ErrCannotInsertNullInOrdinaryColumn,
chproto.ErrNotImplemented:
if isClickHouseMvError(exception) {
return ErrorNotifyMVOrView
}
case 48: // NOT_IMPLEMENTED
if isClickHouseMvError(exception) {
return ErrorNotifyMVOrView
}
case 81: // UNKNOWN_DATABASE
case chproto.ErrUnknownDatabase:
return ErrorNotifyConnectivity
case 999: // KEEPER_EXCEPTION
return ErrorInternalClickHouse
case 341: // UNFINISHED
return ErrorInternalClickHouse
case 236: // ABORTED
case chproto.ErrKeeperException,
chproto.ErrUnfinished,
chproto.ErrAborted:
return ErrorInternalClickHouse
}
}
// Postgres specific errors
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
switch pgErr.Code {
case "28000": // invalid_authorization_specification
case pgerrcode.InvalidAuthorizationSpecification,
pgerrcode.InvalidPassword,
pgerrcode.InsufficientPrivilege,
pgerrcode.UndefinedTable,
pgerrcode.CannotConnectNow:
return ErrorNotifyConnectivity
case "28P01": // invalid_password
return ErrorNotifyConnectivity
case "42P01": // undefined_table
return ErrorNotifyConnectivity
case "42501": // insufficient_privilege
return ErrorNotifyConnectivity
case "57P01": // admin_shutdown
case pgerrcode.AdminShutdown:
return ErrorNotifyTerminate
case "57P03": // cannot_connect_now
return ErrorNotifyConnectivity
case "55000": // object_not_in_prerequisite_state
case pgerrcode.ObjectNotInPrerequisiteState:
if strings.Contains(pgErr.Message, "cannot read from logical replication slot") {
return ErrorNotifySlotInvalid
}
case "53300": // too_many_connections
case pgerrcode.TooManyConnections:
return ErrorNotifyConnectivity // Maybe we can return something else?
}
}
Expand Down

0 comments on commit 94ea569

Please sign in to comment.