Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit dd23566

Browse files
Minor fixes
Adjust usage messages. Log error and exit on failure to prepare create tmp table statement
1 parent 0379d2c commit dd23566

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

postgresql/client.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ func ParseFlags(cfg *Config) *Config {
5050
flag.StringVar(&cfg.database, "pg.database", "postgres", "The PostgreSQL database")
5151
flag.StringVar(&cfg.schema, "pg.schema", "", "The PostgreSQL schema")
5252
flag.StringVar(&cfg.sslMode, "pg.ssl-mode", "disable", "The PostgreSQL connection ssl mode")
53-
flag.StringVar(&cfg.table, "pg.table", "metrics", "The PostgreSQL table")
54-
flag.StringVar(&cfg.copyTable, "pg.copy-table", "", "The PostgreSQL table")
53+
flag.StringVar(&cfg.table, "pg.table", "metrics", "Override prefix for internal tables. It is also a view name used for querying")
54+
flag.StringVar(&cfg.copyTable, "pg.copy-table", "", "Override default table to COPY data to")
5555
flag.IntVar(&cfg.maxOpenConns, "pg.max-open-conns", 50, "The max number of open connections to the database")
5656
flag.IntVar(&cfg.maxIdleConns, "pg.max-idle-conns", 10, "The max number of idle connections to the database")
5757
flag.BoolVar(&cfg.pgPrometheusNormalize, "pg.prometheus-normalized-schema", true, "Insert metric samples into normalized schema")
@@ -68,10 +68,12 @@ type Client struct {
6868
cfg *Config
6969
}
7070

71-
const sqlCreateTmpTable = "CREATE TEMPORARY TABLE IF NOT EXISTS %s_tmp(sample prom_sample) ON COMMIT DELETE ROWS;"
72-
const sqlCopyTable = "COPY \"%s\" FROM STDIN"
73-
const sqlInsertLabels = "INSERT INTO %s_labels (metric_name, labels) SELECT prom_name(tmp.sample), prom_labels(tmp.sample) FROM %s_tmp tmp ON CONFLICT (metric_name, labels) DO NOTHING;"
74-
const sqlInsertValues = "INSERT INTO %s_values SELECT tmp.prom_time, tmp.prom_value, l.id FROM (SELECT prom_time(sample), prom_value(sample), prom_name(sample), prom_labels(sample) FROM %s_tmp) tmp INNER JOIN %s_labels l on tmp.prom_name=l.metric_name AND tmp.prom_labels=l.labels;"
71+
const (
72+
sqlCreateTmpTable = "CREATE TEMPORARY TABLE IF NOT EXISTS %s_tmp(sample prom_sample) ON COMMIT DELETE ROWS;"
73+
sqlCopyTable = "COPY \"%s\" FROM STDIN"
74+
sqlInsertLabels = "INSERT INTO %s_labels (metric_name, labels) SELECT prom_name(tmp.sample), prom_labels(tmp.sample) FROM %s_tmp tmp ON CONFLICT (metric_name, labels) DO NOTHING;"
75+
sqlInsertValues = "INSERT INTO %s_values SELECT tmp.prom_time, tmp.prom_value, l.id FROM (SELECT prom_time(sample), prom_value(sample), prom_name(sample), prom_labels(sample) FROM %s_tmp) tmp INNER JOIN %s_labels l on tmp.prom_name=l.metric_name AND tmp.prom_labels=l.labels;"
76+
)
7577

7678
var (
7779
createTmpTableStmt *sql.Stmt
@@ -111,6 +113,10 @@ func NewClient(cfg *Config) *Client {
111113
}
112114

113115
createTmpTableStmt, err = db.Prepare(fmt.Sprintf(sqlCreateTmpTable, cfg.table))
116+
if err != nil {
117+
log.Error("msg", "Error on preparing create tmp table statement", "err", err)
118+
os.Exit(1)
119+
}
114120
return client
115121
}
116122

0 commit comments

Comments
 (0)