Skip to content

Setup monitoring #16

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

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion pkg/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ func New(cfg Config, kubeClient k8sutil.KubernetesClient, pgSpec acidv1.Postgres
pgSpec.Spec.Users[monitorUsername] = flg
}
}

cluster := &Cluster{
Config: cfg,
Postgresql: pgSpec,
Expand Down Expand Up @@ -422,6 +421,29 @@ func (c *Cluster) Create() (err error) {
// something fails, report warning
c.createConnectionPooler(c.installLookupFunction)

//Setup cpo monitoring related sql statements
if c.Spec.Monitoring != nil {
c.logger.Info("setting up CPO monitoring")

// Open a new connection to the postgres db tp setup monitoring struc and permissions
if err := c.initDbConnWithName("postgres"); err != nil {
return fmt.Errorf("could not init database connection")
}
defer func() {
if c.connectionIsClosed() {
return
}

if err := c.closeDbConn(); err != nil {
c.logger.Errorf("could not close database connection: %v", err)
}
}()
_, err := c.pgDb.Exec(CPOmonitoring)
if err != nil {
return fmt.Errorf("CPO monitoring could not be setup: %v", err)
}
}

// remember slots to detect deletion from manifest
for slotName, desiredSlot := range c.Spec.Patroni.Slots {
c.replicationSlots[slotName] = desiredSlot
Expand Down
16 changes: 16 additions & 0 deletions pkg/cluster/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ const (
TO {{.pooler_user}};
GRANT USAGE ON SCHEMA {{.pooler_schema}} TO {{.pooler_user}};
`
CPOmonitoring = `
GRANT pg_monitor TO cpo_exporter;
GRANT SELECT ON TABLE pg_authid TO cpo_exporter;

CREATE SCHEMA IF NOT EXISTS exporter;
ALTER SCHEMA exporter OWNER TO cpo_exporter;
CREATE EXTENSION IF NOT EXISTS pgnodemx with SCHEMA exporter;
alter extension pgnodemx UPDATE;
CREATE TABLE IF NOT EXISTS exporter.pgbackrestbackupinfo (
name text NOT NULL,
data jsonb NOT NULL,
data_time timestamp with time zone DEFAULT now() NOT NULL
)
WITH (autovacuum_analyze_scale_factor='0', autovacuum_vacuum_scale_factor='0', autovacuum_vacuum_threshold='2', autovacuum_analyze_threshold='2');
ALTER TABLE exporter.pgbackrestbackupinfo OWNER TO cpo_exporter;
`
)

func (c *Cluster) pgConnectionString(dbname string) string {
Expand Down