-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to dynamically configure some thresholds (#1057)
Co-authored-by: Kevin Biju <[email protected]>
- Loading branch information
1 parent
19da0fb
commit 7cdfb86
Showing
5 changed files
with
82 additions
and
21 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package dynamicconf | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"strconv" | ||
"time" | ||
|
||
utils "github.com/PeerDB-io/peer-flow/connectors/utils/catalog" | ||
"github.com/jackc/pgx/v5/pgtype" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
) | ||
|
||
func dynamicConfKeyExists(ctx context.Context, conn *pgxpool.Pool, key string) bool { | ||
var exists pgtype.Bool | ||
query := "SELECT EXISTS(SELECT 1 FROM alerting_settings WHERE config_name = $1)" | ||
err := conn.QueryRow(ctx, query, key).Scan(&exists) | ||
if err != nil { | ||
slog.Error("Failed to check if key exists: %v", err) | ||
return false | ||
} | ||
|
||
return exists.Bool | ||
} | ||
|
||
func dynamicConfUint32(ctx context.Context, key string, defaultValue uint32) uint32 { | ||
conn, err := utils.GetCatalogConnectionPoolFromEnv() | ||
if err != nil { | ||
slog.Error("Failed to get catalog connection pool: %v", err) | ||
return defaultValue | ||
} | ||
|
||
if !dynamicConfKeyExists(ctx, conn, key) { | ||
return defaultValue | ||
} | ||
|
||
var value pgtype.Text | ||
query := "SELECT config_value FROM alerting_settings WHERE config_name = $1" | ||
err = conn.QueryRow(ctx, query, key).Scan(&value) | ||
if err != nil { | ||
slog.Error("Failed to get key: %v", err) | ||
return defaultValue | ||
} | ||
|
||
result, err := strconv.ParseUint(value.String, 10, 32) | ||
if err != nil { | ||
slog.Error("Failed to parse uint32: %v", err) | ||
return defaultValue | ||
} | ||
|
||
return uint32(result) | ||
} | ||
|
||
// PEERDB_SLOT_LAG_MB_ALERT_THRESHOLD, 0 disables slot lag alerting entirely | ||
func PeerDBSlotLagMBAlertThreshold(ctx context.Context) uint32 { | ||
return dynamicConfUint32(ctx, "PEERDB_SLOT_LAG_MB_ALERT_THRESHOLD", 5000) | ||
} | ||
|
||
// PEERDB_ALERTING_GAP_MINUTES, 0 disables all alerting entirely | ||
func PeerDBAlertingGapMinutesAsDuration(ctx context.Context) time.Duration { | ||
why := int64(dynamicConfUint32(ctx, "PEERDB_ALERTING_GAP_MINUTES", 15)) | ||
return time.Duration(why) * time.Minute | ||
} | ||
|
||
// PEERDB_PGPEER_OPEN_CONNECTIONS_ALERT_THRESHOLD, 0 disables open connections alerting entirely | ||
func PeerDBOpenConnectionsAlertThreshold(ctx context.Context) uint32 { | ||
return dynamicConfUint32(ctx, "PEERDB_PGPEER_OPEN_CONNECTIONS_ALERT_THRESHOLD", 5) | ||
} |
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,7 @@ | ||
CREATE TABLE alerting_settings( | ||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, | ||
config_name TEXT NOT NULL, | ||
config_value TEXT NOT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX idx_alerting_settings_config_name ON alerting_settings (config_name); |