Append new query label

Signed-off-by: nikolay.deryugin <nikolay.deryugin@itglobal.com>
Signed-off-by: Deryugin, Nikolay <nikolay.deryugin@itglobal.com>
This commit is contained in:
nikolay.deryugin 2023-09-25 11:12:14 +03:00 committed by Deryugin, Nikolay
parent 4e521d460e
commit 37384c758d
2 changed files with 41 additions and 35 deletions

View File

@ -40,34 +40,34 @@ func NewPGStatStatementsCollector(config collectorConfig) (Collector, error) {
} }
var ( var (
statSTatementsCallsTotal = prometheus.NewDesc( statStatementsCallsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "calls_total"), prometheus.BuildFQName(namespace, statStatementsSubsystem, "calls_total"),
"Number of times executed", "Number of times executed",
[]string{"user", "datname", "queryid"}, []string{"user", "datname", "queryid", "query"},
prometheus.Labels{}, prometheus.Labels{},
) )
statStatementsSecondsTotal = prometheus.NewDesc( statStatementsSecondsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "seconds_total"), prometheus.BuildFQName(namespace, statStatementsSubsystem, "seconds_total"),
"Total time spent in the statement, in seconds", "Total time spent in the statement, in seconds",
[]string{"user", "datname", "queryid"}, []string{"user", "datname", "queryid", "query"},
prometheus.Labels{}, prometheus.Labels{},
) )
statStatementsRowsTotal = prometheus.NewDesc( statStatementsRowsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "rows_total"), prometheus.BuildFQName(namespace, statStatementsSubsystem, "rows_total"),
"Total number of rows retrieved or affected by the statement", "Total number of rows retrieved or affected by the statement",
[]string{"user", "datname", "queryid"}, []string{"user", "datname", "queryid", "query"},
prometheus.Labels{}, prometheus.Labels{},
) )
statStatementsBlockReadSecondsTotal = prometheus.NewDesc( statStatementsBlockReadSecondsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "block_read_seconds_total"), prometheus.BuildFQName(namespace, statStatementsSubsystem, "block_read_seconds_total"),
"Total time the statement spent reading blocks, in seconds", "Total time the statement spent reading blocks, in seconds",
[]string{"user", "datname", "queryid"}, []string{"user", "datname", "queryid", "query"},
prometheus.Labels{}, prometheus.Labels{},
) )
statStatementsBlockWriteSecondsTotal = prometheus.NewDesc( statStatementsBlockWriteSecondsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "block_write_seconds_total"), prometheus.BuildFQName(namespace, statStatementsSubsystem, "block_write_seconds_total"),
"Total time the statement spent writing blocks, in seconds", "Total time the statement spent writing blocks, in seconds",
[]string{"user", "datname", "queryid"}, []string{"user", "datname", "queryid", "query"},
prometheus.Labels{}, prometheus.Labels{},
) )
@ -75,6 +75,7 @@ var (
pg_get_userbyid(userid) as user, pg_get_userbyid(userid) as user,
pg_database.datname, pg_database.datname,
pg_stat_statements.queryid, pg_stat_statements.queryid,
pg_stat_statements.query as query,
pg_stat_statements.calls as calls_total, pg_stat_statements.calls as calls_total,
pg_stat_statements.total_time / 1000.0 as seconds_total, pg_stat_statements.total_time / 1000.0 as seconds_total,
pg_stat_statements.rows as rows_total, pg_stat_statements.rows as rows_total,
@ -96,6 +97,7 @@ var (
pg_get_userbyid(userid) as user, pg_get_userbyid(userid) as user,
pg_database.datname, pg_database.datname,
pg_stat_statements.queryid, pg_stat_statements.queryid,
pg_stat_statements.query as query,
pg_stat_statements.calls as calls_total, pg_stat_statements.calls as calls_total,
pg_stat_statements.total_exec_time / 1000.0 as seconds_total, pg_stat_statements.total_exec_time / 1000.0 as seconds_total,
pg_stat_statements.rows as rows_total, pg_stat_statements.rows as rows_total,
@ -128,11 +130,11 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
} }
defer rows.Close() defer rows.Close()
for rows.Next() { for rows.Next() {
var user, datname, queryid sql.NullString var user, datname, queryid, query sql.NullString
var callsTotal, rowsTotal sql.NullInt64 var callsTotal, rowsTotal sql.NullInt64
var secondsTotal, blockReadSecondsTotal, blockWriteSecondsTotal sql.NullFloat64 var secondsTotal, blockReadSecondsTotal, blockWriteSecondsTotal sql.NullFloat64
if err := rows.Scan(&user, &datname, &queryid, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal); err != nil { if err := rows.Scan(&user, &datname, &queryid, &query, &callsTotal, &secondsTotal, &rowsTotal, &blockReadSecondsTotal, &blockWriteSecondsTotal); err != nil {
return err return err
} }
@ -148,16 +150,20 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
if queryid.Valid { if queryid.Valid {
queryidLabel = queryid.String queryidLabel = queryid.String
} }
queryLabel := "unknown"
if query.Valid {
queryLabel = query.String
}
callsTotalMetric := 0.0 callsTotalMetric := 0.0
if callsTotal.Valid { if callsTotal.Valid {
callsTotalMetric = float64(callsTotal.Int64) callsTotalMetric = float64(callsTotal.Int64)
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statSTatementsCallsTotal, statStatementsCallsTotal,
prometheus.CounterValue, prometheus.CounterValue,
callsTotalMetric, callsTotalMetric,
userLabel, datnameLabel, queryidLabel, userLabel, datnameLabel, queryidLabel, queryLabel,
) )
secondsTotalMetric := 0.0 secondsTotalMetric := 0.0
@ -168,7 +174,7 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
statStatementsSecondsTotal, statStatementsSecondsTotal,
prometheus.CounterValue, prometheus.CounterValue,
secondsTotalMetric, secondsTotalMetric,
userLabel, datnameLabel, queryidLabel, userLabel, datnameLabel, queryidLabel, queryLabel,
) )
rowsTotalMetric := 0.0 rowsTotalMetric := 0.0
@ -179,7 +185,7 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
statStatementsRowsTotal, statStatementsRowsTotal,
prometheus.CounterValue, prometheus.CounterValue,
rowsTotalMetric, rowsTotalMetric,
userLabel, datnameLabel, queryidLabel, userLabel, datnameLabel, queryidLabel, queryLabel,
) )
blockReadSecondsTotalMetric := 0.0 blockReadSecondsTotalMetric := 0.0
@ -190,7 +196,7 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
statStatementsBlockReadSecondsTotal, statStatementsBlockReadSecondsTotal,
prometheus.CounterValue, prometheus.CounterValue,
blockReadSecondsTotalMetric, blockReadSecondsTotalMetric,
userLabel, datnameLabel, queryidLabel, userLabel, datnameLabel, queryidLabel, queryLabel,
) )
blockWriteSecondsTotalMetric := 0.0 blockWriteSecondsTotalMetric := 0.0
@ -201,7 +207,7 @@ func (PGStatStatementsCollector) Update(ctx context.Context, instance *instance,
statStatementsBlockWriteSecondsTotal, statStatementsBlockWriteSecondsTotal,
prometheus.CounterValue, prometheus.CounterValue,
blockWriteSecondsTotalMetric, blockWriteSecondsTotalMetric,
userLabel, datnameLabel, queryidLabel, userLabel, datnameLabel, queryidLabel, queryLabel,
) )
} }
if err := rows.Err(); err != nil { if err := rows.Err(); err != nil {

View File

@ -32,9 +32,9 @@ func TestPGStateStatementsCollector(t *testing.T) {
inst := &instance{db: db, version: semver.MustParse("12.0.0")} inst := &instance{db: db, version: semver.MustParse("12.0.0")}
columns := []string{"user", "datname", "queryid", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"} columns := []string{"user", "datname", "queryid", "query", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"}
rows := sqlmock.NewRows(columns). rows := sqlmock.NewRows(columns).
AddRow("postgres", "postgres", 1500, 5, 0.4, 100, 0.1, 0.2) AddRow("postgres", "postgres", 1500, "SELECT", 5, 0.4, 100, 0.1, 0.2)
mock.ExpectQuery(sanitizeQuery(pgStatStatementsQuery)).WillReturnRows(rows) mock.ExpectQuery(sanitizeQuery(pgStatStatementsQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric) ch := make(chan prometheus.Metric)
@ -48,11 +48,11 @@ func TestPGStateStatementsCollector(t *testing.T) {
}() }()
expected := []MetricResult{ expected := []MetricResult{
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 5}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 5},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.4}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.4},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 100}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 100},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.1}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.1},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.2}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.2},
} }
convey.Convey("Metrics comparison", t, func() { convey.Convey("Metrics comparison", t, func() {
@ -75,9 +75,9 @@ func TestPGStateStatementsCollectorNull(t *testing.T) {
inst := &instance{db: db, version: semver.MustParse("13.3.7")} inst := &instance{db: db, version: semver.MustParse("13.3.7")}
columns := []string{"user", "datname", "queryid", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"} columns := []string{"user", "datname", "queryid", "query", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"}
rows := sqlmock.NewRows(columns). rows := sqlmock.NewRows(columns).
AddRow(nil, nil, nil, nil, nil, nil, nil, nil) AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil)
mock.ExpectQuery(sanitizeQuery(pgStatStatementsNewQuery)).WillReturnRows(rows) mock.ExpectQuery(sanitizeQuery(pgStatStatementsNewQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric) ch := make(chan prometheus.Metric)
@ -91,11 +91,11 @@ func TestPGStateStatementsCollectorNull(t *testing.T) {
}() }()
expected := []MetricResult{ expected := []MetricResult{
{labels: labelMap{"user": "unknown", "datname": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0}, {labels: labelMap{"user": "unknown", "datname": "unknown", "query": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"user": "unknown", "datname": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0}, {labels: labelMap{"user": "unknown", "datname": "unknown", "query": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"user": "unknown", "datname": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0}, {labels: labelMap{"user": "unknown", "datname": "unknown", "query": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"user": "unknown", "datname": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0}, {labels: labelMap{"user": "unknown", "datname": "unknown", "query": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
{labels: labelMap{"user": "unknown", "datname": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0}, {labels: labelMap{"user": "unknown", "datname": "unknown", "query": "unknown", "queryid": "unknown"}, metricType: dto.MetricType_COUNTER, value: 0},
} }
convey.Convey("Metrics comparison", t, func() { convey.Convey("Metrics comparison", t, func() {
@ -118,9 +118,9 @@ func TestPGStateStatementsCollectorNewPG(t *testing.T) {
inst := &instance{db: db, version: semver.MustParse("13.3.7")} inst := &instance{db: db, version: semver.MustParse("13.3.7")}
columns := []string{"user", "datname", "queryid", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"} columns := []string{"user", "datname", "queryid", "query", "calls_total", "seconds_total", "rows_total", "block_read_seconds_total", "block_write_seconds_total"}
rows := sqlmock.NewRows(columns). rows := sqlmock.NewRows(columns).
AddRow("postgres", "postgres", 1500, 5, 0.4, 100, 0.1, 0.2) AddRow("postgres", "postgres", 1500, "SELECT", 5, 0.4, 100, 0.1, 0.2)
mock.ExpectQuery(sanitizeQuery(pgStatStatementsNewQuery)).WillReturnRows(rows) mock.ExpectQuery(sanitizeQuery(pgStatStatementsNewQuery)).WillReturnRows(rows)
ch := make(chan prometheus.Metric) ch := make(chan prometheus.Metric)
@ -134,11 +134,11 @@ func TestPGStateStatementsCollectorNewPG(t *testing.T) {
}() }()
expected := []MetricResult{ expected := []MetricResult{
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 5}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 5},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.4}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.4},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 100}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 100},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.1}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.1},
{labels: labelMap{"user": "postgres", "datname": "postgres", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.2}, {labels: labelMap{"user": "postgres", "datname": "postgres", "query": "SELECT", "queryid": "1500"}, metricType: dto.MetricType_COUNTER, value: 0.2},
} }
convey.Convey("Metrics comparison", t, func() { convey.Convey("Metrics comparison", t, func() {