mirror of
https://github.com/prometheus-community/postgres_exporter
synced 2025-04-08 02:01:23 +00:00
Merge branch 'master' into fix-pg_ls_waldir-perm-pg16.6
This commit is contained in:
commit
0bbddbb9be
@ -63,6 +63,7 @@ workflows:
|
||||
- cimg/postgres:14.9
|
||||
- cimg/postgres:15.4
|
||||
- cimg/postgres:16.0
|
||||
- cimg/postgres:17.0
|
||||
- prometheus/build:
|
||||
name: build
|
||||
parallelism: 3
|
||||
|
4
.github/workflows/golangci-lint.yml
vendored
4
.github/workflows/golangci-lint.yml
vendored
@ -26,7 +26,7 @@ jobs:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
|
||||
- name: Install Go
|
||||
uses: actions/setup-go@41dfa10bad2bb2ae585af6ee5bb4d7d973ad74ed # v5.1.0
|
||||
uses: actions/setup-go@3041bf56c941b39c61721a86cd11f3bb1338122a # v5.2.0
|
||||
with:
|
||||
go-version: 1.23.x
|
||||
- name: Install snmp_exporter/generator dependencies
|
||||
@ -36,4 +36,4 @@ jobs:
|
||||
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8 # v6.1.1
|
||||
with:
|
||||
args: --verbose
|
||||
version: v1.61.0
|
||||
version: v1.62.0
|
||||
|
@ -1,7 +1,7 @@
|
||||
---
|
||||
extends: default
|
||||
ignore: |
|
||||
ui/react-app/node_modules
|
||||
**/node_modules
|
||||
|
||||
rules:
|
||||
braces:
|
||||
|
@ -61,7 +61,7 @@ PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_
|
||||
SKIP_GOLANGCI_LINT :=
|
||||
GOLANGCI_LINT :=
|
||||
GOLANGCI_LINT_OPTS ?=
|
||||
GOLANGCI_LINT_VERSION ?= v1.60.2
|
||||
GOLANGCI_LINT_VERSION ?= v1.62.0
|
||||
# golangci-lint only supports linux, darwin and windows platforms on i386/amd64/arm64.
|
||||
# windows isn't included here because of the path separator being different.
|
||||
ifeq ($(GOHOSTOS),$(filter $(GOHOSTOS),linux darwin))
|
||||
|
@ -7,7 +7,7 @@
|
||||
|
||||
Prometheus exporter for PostgreSQL server metrics.
|
||||
|
||||
CI Tested PostgreSQL versions: `11`, `12`, `13`, `14`, `15`, `16`
|
||||
CI Tested PostgreSQL versions: `11`, `12`, `13`, `14`, `15`, `16`, `17`.
|
||||
|
||||
## Quick Start
|
||||
This package is available for Docker:
|
||||
|
@ -67,7 +67,7 @@ type pgSetting struct {
|
||||
func (s *pgSetting) metric(labels prometheus.Labels) prometheus.Metric {
|
||||
var (
|
||||
err error
|
||||
name = strings.Replace(s.name, ".", "_", -1)
|
||||
name = strings.Replace(strings.Replace(s.name, ".", "_", -1), "-", "_", -1)
|
||||
unit = s.unit // nolint: ineffassign
|
||||
shortDesc = fmt.Sprintf("Server Parameter: %s", s.name)
|
||||
subsystem = "settings"
|
||||
|
@ -50,11 +50,13 @@ var (
|
||||
)
|
||||
|
||||
longRunningTransactionsQuery = `
|
||||
SELECT
|
||||
COUNT(*) as transactions,
|
||||
MAX(EXTRACT(EPOCH FROM clock_timestamp())) AS oldest_timestamp_seconds
|
||||
FROM pg_catalog.pg_stat_activity
|
||||
WHERE state is distinct from 'idle' AND query not like 'autovacuum:%'
|
||||
SELECT
|
||||
COUNT(*) as transactions,
|
||||
MAX(EXTRACT(EPOCH FROM clock_timestamp() - pg_stat_activity.xact_start)) AS oldest_timestamp_seconds
|
||||
FROM pg_catalog.pg_stat_activity
|
||||
WHERE state IS DISTINCT FROM 'idle'
|
||||
AND query NOT LIKE 'autovacuum:%'
|
||||
AND pg_stat_activity.xact_start IS NOT NULL;
|
||||
`
|
||||
)
|
||||
|
||||
|
@ -17,6 +17,7 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/blang/semver/v4"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
@ -101,7 +102,7 @@ var (
|
||||
prometheus.Labels{},
|
||||
)
|
||||
|
||||
statBGWriterQuery = `SELECT
|
||||
statBGWriterQueryBefore17 = `SELECT
|
||||
checkpoints_timed
|
||||
,checkpoints_req
|
||||
,checkpoint_write_time
|
||||
@ -114,121 +115,177 @@ var (
|
||||
,buffers_alloc
|
||||
,stats_reset
|
||||
FROM pg_stat_bgwriter;`
|
||||
|
||||
statBGWriterQueryAfter17 = `SELECT
|
||||
buffers_clean
|
||||
,maxwritten_clean
|
||||
,buffers_alloc
|
||||
,stats_reset
|
||||
FROM pg_stat_bgwriter;`
|
||||
)
|
||||
|
||||
func (PGStatBGWriterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
|
||||
db := instance.getDB()
|
||||
row := db.QueryRowContext(ctx,
|
||||
statBGWriterQuery)
|
||||
if instance.version.GE(semver.MustParse("17.0.0")) {
|
||||
db := instance.getDB()
|
||||
row := db.QueryRowContext(ctx, statBGWriterQueryAfter17)
|
||||
|
||||
var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
|
||||
var cpwt, cpst sql.NullFloat64
|
||||
var sr sql.NullTime
|
||||
var bc, mwc, ba sql.NullInt64
|
||||
var sr sql.NullTime
|
||||
|
||||
err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err := row.Scan(&bc, &mwc, &ba, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cptMetric := 0.0
|
||||
if cpt.Valid {
|
||||
cptMetric = float64(cpt.Int64)
|
||||
bcMetric := 0.0
|
||||
if bc.Valid {
|
||||
bcMetric = float64(bc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
bcMetric,
|
||||
)
|
||||
mwcMetric := 0.0
|
||||
if mwc.Valid {
|
||||
mwcMetric = float64(mwc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterMaxwrittenCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
mwcMetric,
|
||||
)
|
||||
baMetric := 0.0
|
||||
if ba.Valid {
|
||||
baMetric = float64(ba.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersAllocDesc,
|
||||
prometheus.CounterValue,
|
||||
baMetric,
|
||||
)
|
||||
srMetric := 0.0
|
||||
if sr.Valid {
|
||||
srMetric = float64(sr.Time.Unix())
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterStatsResetDesc,
|
||||
prometheus.CounterValue,
|
||||
srMetric,
|
||||
)
|
||||
} else {
|
||||
db := instance.getDB()
|
||||
row := db.QueryRowContext(ctx, statBGWriterQueryBefore17)
|
||||
|
||||
var cpt, cpr, bcp, bc, mwc, bb, bbf, ba sql.NullInt64
|
||||
var cpwt, cpst sql.NullFloat64
|
||||
var sr sql.NullTime
|
||||
|
||||
err := row.Scan(&cpt, &cpr, &cpwt, &cpst, &bcp, &bc, &mwc, &bb, &bbf, &ba, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
cptMetric := 0.0
|
||||
if cpt.Valid {
|
||||
cptMetric = float64(cpt.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsTimedDesc,
|
||||
prometheus.CounterValue,
|
||||
cptMetric,
|
||||
)
|
||||
cprMetric := 0.0
|
||||
if cpr.Valid {
|
||||
cprMetric = float64(cpr.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsReqDesc,
|
||||
prometheus.CounterValue,
|
||||
cprMetric,
|
||||
)
|
||||
cpwtMetric := 0.0
|
||||
if cpwt.Valid {
|
||||
cpwtMetric = float64(cpwt.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsReqTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
cpwtMetric,
|
||||
)
|
||||
cpstMetric := 0.0
|
||||
if cpst.Valid {
|
||||
cpstMetric = float64(cpst.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsSyncTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
cpstMetric,
|
||||
)
|
||||
bcpMetric := 0.0
|
||||
if bcp.Valid {
|
||||
bcpMetric = float64(bcp.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersCheckpointDesc,
|
||||
prometheus.CounterValue,
|
||||
bcpMetric,
|
||||
)
|
||||
bcMetric := 0.0
|
||||
if bc.Valid {
|
||||
bcMetric = float64(bc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
bcMetric,
|
||||
)
|
||||
mwcMetric := 0.0
|
||||
if mwc.Valid {
|
||||
mwcMetric = float64(mwc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterMaxwrittenCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
mwcMetric,
|
||||
)
|
||||
bbMetric := 0.0
|
||||
if bb.Valid {
|
||||
bbMetric = float64(bb.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersBackendDesc,
|
||||
prometheus.CounterValue,
|
||||
bbMetric,
|
||||
)
|
||||
bbfMetric := 0.0
|
||||
if bbf.Valid {
|
||||
bbfMetric = float64(bbf.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersBackendFsyncDesc,
|
||||
prometheus.CounterValue,
|
||||
bbfMetric,
|
||||
)
|
||||
baMetric := 0.0
|
||||
if ba.Valid {
|
||||
baMetric = float64(ba.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersAllocDesc,
|
||||
prometheus.CounterValue,
|
||||
baMetric,
|
||||
)
|
||||
srMetric := 0.0
|
||||
if sr.Valid {
|
||||
srMetric = float64(sr.Time.Unix())
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterStatsResetDesc,
|
||||
prometheus.CounterValue,
|
||||
srMetric,
|
||||
)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsTimedDesc,
|
||||
prometheus.CounterValue,
|
||||
cptMetric,
|
||||
)
|
||||
cprMetric := 0.0
|
||||
if cpr.Valid {
|
||||
cprMetric = float64(cpr.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsReqDesc,
|
||||
prometheus.CounterValue,
|
||||
cprMetric,
|
||||
)
|
||||
cpwtMetric := 0.0
|
||||
if cpwt.Valid {
|
||||
cpwtMetric = float64(cpwt.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsReqTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
cpwtMetric,
|
||||
)
|
||||
cpstMetric := 0.0
|
||||
if cpst.Valid {
|
||||
cpstMetric = float64(cpst.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterCheckpointsSyncTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
cpstMetric,
|
||||
)
|
||||
bcpMetric := 0.0
|
||||
if bcp.Valid {
|
||||
bcpMetric = float64(bcp.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersCheckpointDesc,
|
||||
prometheus.CounterValue,
|
||||
bcpMetric,
|
||||
)
|
||||
bcMetric := 0.0
|
||||
if bc.Valid {
|
||||
bcMetric = float64(bc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
bcMetric,
|
||||
)
|
||||
mwcMetric := 0.0
|
||||
if mwc.Valid {
|
||||
mwcMetric = float64(mwc.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterMaxwrittenCleanDesc,
|
||||
prometheus.CounterValue,
|
||||
mwcMetric,
|
||||
)
|
||||
bbMetric := 0.0
|
||||
if bb.Valid {
|
||||
bbMetric = float64(bb.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersBackendDesc,
|
||||
prometheus.CounterValue,
|
||||
bbMetric,
|
||||
)
|
||||
bbfMetric := 0.0
|
||||
if bbf.Valid {
|
||||
bbfMetric = float64(bbf.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersBackendFsyncDesc,
|
||||
prometheus.CounterValue,
|
||||
bbfMetric,
|
||||
)
|
||||
baMetric := 0.0
|
||||
if ba.Valid {
|
||||
baMetric = float64(ba.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterBuffersAllocDesc,
|
||||
prometheus.CounterValue,
|
||||
baMetric,
|
||||
)
|
||||
srMetric := 0.0
|
||||
if sr.Valid {
|
||||
srMetric = float64(sr.Time.Unix())
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statBGWriterStatsResetDesc,
|
||||
prometheus.CounterValue,
|
||||
srMetric,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -52,7 +52,7 @@ func TestPGStatBGWriterCollector(t *testing.T) {
|
||||
|
||||
rows := sqlmock.NewRows(columns).
|
||||
AddRow(354, 4945, 289097744, 1242257, int64(3275602074), 89320867, 450139, 2034563757, 0, int64(2725688749), srT)
|
||||
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
|
||||
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryBefore17)).WillReturnRows(rows)
|
||||
|
||||
ch := make(chan prometheus.Metric)
|
||||
go func() {
|
||||
@ -113,7 +113,7 @@ func TestPGStatBGWriterCollectorNullValues(t *testing.T) {
|
||||
|
||||
rows := sqlmock.NewRows(columns).
|
||||
AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
mock.ExpectQuery(sanitizeQuery(statBGWriterQuery)).WillReturnRows(rows)
|
||||
mock.ExpectQuery(sanitizeQuery(statBGWriterQueryBefore17)).WillReturnRows(rows)
|
||||
|
||||
ch := make(chan prometheus.Metric)
|
||||
go func() {
|
||||
|
221
collector/pg_stat_checkpointer.go
Normal file
221
collector/pg_stat_checkpointer.go
Normal file
@ -0,0 +1,221 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
const statCheckpointerSubsystem = "stat_checkpointer"
|
||||
|
||||
func init() {
|
||||
// WARNING:
|
||||
// Disabled by default because this set of metrics is only available from Postgres 17
|
||||
registerCollector(statCheckpointerSubsystem, defaultDisabled, NewPGStatCheckpointerCollector)
|
||||
}
|
||||
|
||||
type PGStatCheckpointerCollector struct {
|
||||
}
|
||||
|
||||
func NewPGStatCheckpointerCollector(collectorConfig) (Collector, error) {
|
||||
return &PGStatCheckpointerCollector{}, nil
|
||||
}
|
||||
|
||||
var (
|
||||
statCheckpointerNumTimedDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "num_timed_total"),
|
||||
"Number of scheduled checkpoints due to timeout",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerNumRequestedDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "num_requested_total"),
|
||||
"Number of requested checkpoints that have been performed",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerRestartpointsTimedDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_timed_total"),
|
||||
"Number of scheduled restartpoints due to timeout or after a failed attempt to perform it",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerRestartpointsReqDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_req_total"),
|
||||
"Number of requested restartpoints",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerRestartpointsDoneDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "restartpoints_done_total"),
|
||||
"Number of restartpoints that have been performed",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerWriteTimeDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "write_time_total"),
|
||||
"Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are written to disk, in milliseconds",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerSyncTimeDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "sync_time_total"),
|
||||
"Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are synchronized to disk, in milliseconds",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerBuffersWrittenDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "buffers_written_total"),
|
||||
"Number of buffers written during checkpoints and restartpoints",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
statCheckpointerStatsResetDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(namespace, statCheckpointerSubsystem, "stats_reset_total"),
|
||||
"Time at which these statistics were last reset",
|
||||
[]string{},
|
||||
prometheus.Labels{},
|
||||
)
|
||||
|
||||
statCheckpointerQuery = `SELECT
|
||||
num_timed
|
||||
,num_requested
|
||||
,restartpoints_timed
|
||||
,restartpoints_req
|
||||
,restartpoints_done
|
||||
,write_time
|
||||
,sync_time
|
||||
,buffers_written
|
||||
,stats_reset
|
||||
FROM pg_stat_checkpointer;`
|
||||
)
|
||||
|
||||
func (PGStatCheckpointerCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
|
||||
db := instance.getDB()
|
||||
row := db.QueryRowContext(ctx, statCheckpointerQuery)
|
||||
|
||||
// num_timed = nt = bigint
|
||||
// num_requested = nr = bigint
|
||||
// restartpoints_timed = rpt = bigint
|
||||
// restartpoints_req = rpr = bigint
|
||||
// restartpoints_done = rpd = bigint
|
||||
// write_time = wt = double precision
|
||||
// sync_time = st = double precision
|
||||
// buffers_written = bw = bigint
|
||||
// stats_reset = sr = timestamp
|
||||
|
||||
var nt, nr, rpt, rpr, rpd, bw sql.NullInt64
|
||||
var wt, st sql.NullFloat64
|
||||
var sr sql.NullTime
|
||||
|
||||
err := row.Scan(&nt, &nr, &rpt, &rpr, &rpd, &wt, &st, &bw, &sr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ntMetric := 0.0
|
||||
if nt.Valid {
|
||||
ntMetric = float64(nt.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerNumTimedDesc,
|
||||
prometheus.CounterValue,
|
||||
ntMetric,
|
||||
)
|
||||
|
||||
nrMetric := 0.0
|
||||
if nr.Valid {
|
||||
nrMetric = float64(nr.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerNumRequestedDesc,
|
||||
prometheus.CounterValue,
|
||||
nrMetric,
|
||||
)
|
||||
|
||||
rptMetric := 0.0
|
||||
if rpt.Valid {
|
||||
rptMetric = float64(rpt.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerRestartpointsTimedDesc,
|
||||
prometheus.CounterValue,
|
||||
rptMetric,
|
||||
)
|
||||
|
||||
rprMetric := 0.0
|
||||
if rpr.Valid {
|
||||
rprMetric = float64(rpr.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerRestartpointsReqDesc,
|
||||
prometheus.CounterValue,
|
||||
rprMetric,
|
||||
)
|
||||
|
||||
rpdMetric := 0.0
|
||||
if rpd.Valid {
|
||||
rpdMetric = float64(rpd.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerRestartpointsDoneDesc,
|
||||
prometheus.CounterValue,
|
||||
rpdMetric,
|
||||
)
|
||||
|
||||
wtMetric := 0.0
|
||||
if wt.Valid {
|
||||
wtMetric = float64(wt.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerWriteTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
wtMetric,
|
||||
)
|
||||
|
||||
stMetric := 0.0
|
||||
if st.Valid {
|
||||
stMetric = float64(st.Float64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerSyncTimeDesc,
|
||||
prometheus.CounterValue,
|
||||
stMetric,
|
||||
)
|
||||
|
||||
bwMetric := 0.0
|
||||
if bw.Valid {
|
||||
bwMetric = float64(bw.Int64)
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerBuffersWrittenDesc,
|
||||
prometheus.CounterValue,
|
||||
bwMetric,
|
||||
)
|
||||
|
||||
srMetric := 0.0
|
||||
if sr.Valid {
|
||||
srMetric = float64(sr.Time.Unix())
|
||||
}
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
statCheckpointerStatsResetDesc,
|
||||
prometheus.CounterValue,
|
||||
srMetric,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
143
collector/pg_stat_checkpointer_test.go
Normal file
143
collector/pg_stat_checkpointer_test.go
Normal file
@ -0,0 +1,143 @@
|
||||
// Copyright 2024 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
package collector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/go-sqlmock"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
"github.com/smartystreets/goconvey/convey"
|
||||
)
|
||||
|
||||
func TestPGStatCheckpointerCollector(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Error opening a stub db connection: %s", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
inst := &instance{db: db}
|
||||
|
||||
columns := []string{
|
||||
"num_timed",
|
||||
"num_requested",
|
||||
"restartpoints_timed",
|
||||
"restartpoints_req",
|
||||
"restartpoints_done",
|
||||
"write_time",
|
||||
"sync_time",
|
||||
"buffers_written",
|
||||
"stats_reset"}
|
||||
|
||||
srT, err := time.Parse("2006-01-02 15:04:05.00000-07", "2023-05-25 17:10:42.81132-07")
|
||||
if err != nil {
|
||||
t.Fatalf("Error parsing time: %s", err)
|
||||
}
|
||||
|
||||
rows := sqlmock.NewRows(columns).
|
||||
AddRow(354, 4945, 289097744, 1242257, int64(3275602074), 89320867, 450139, 2034563757, srT)
|
||||
mock.ExpectQuery(sanitizeQuery(statCheckpointerQuery)).WillReturnRows(rows)
|
||||
|
||||
ch := make(chan prometheus.Metric)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
c := PGStatCheckpointerCollector{}
|
||||
|
||||
if err := c.Update(context.Background(), inst, ch); err != nil {
|
||||
t.Errorf("Error calling PGStatCheckpointerCollector.Update: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
expected := []MetricResult{
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 354},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 4945},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 289097744},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1242257},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 3275602074},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 89320867},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 450139},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 2034563757},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 1685059842},
|
||||
}
|
||||
|
||||
convey.Convey("Metrics comparison", t, func() {
|
||||
for _, expect := range expected {
|
||||
m := readMetric(<-ch)
|
||||
convey.So(expect, convey.ShouldResemble, m)
|
||||
}
|
||||
})
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled exceptions: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPGStatCheckpointerCollectorNullValues(t *testing.T) {
|
||||
db, mock, err := sqlmock.New()
|
||||
if err != nil {
|
||||
t.Fatalf("Error opening a stub db connection: %s", err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
inst := &instance{db: db}
|
||||
|
||||
columns := []string{
|
||||
"num_timed",
|
||||
"num_requested",
|
||||
"restartpoints_timed",
|
||||
"restartpoints_req",
|
||||
"restartpoints_done",
|
||||
"write_time",
|
||||
"sync_time",
|
||||
"buffers_written",
|
||||
"stats_reset"}
|
||||
|
||||
rows := sqlmock.NewRows(columns).
|
||||
AddRow(nil, nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
mock.ExpectQuery(sanitizeQuery(statCheckpointerQuery)).WillReturnRows(rows)
|
||||
|
||||
ch := make(chan prometheus.Metric)
|
||||
go func() {
|
||||
defer close(ch)
|
||||
c := PGStatCheckpointerCollector{}
|
||||
|
||||
if err := c.Update(context.Background(), inst, ch); err != nil {
|
||||
t.Errorf("Error calling PGStatCheckpointerCollector.Update: %s", err)
|
||||
}
|
||||
}()
|
||||
|
||||
expected := []MetricResult{
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
{labels: labelMap{}, metricType: dto.MetricType_COUNTER, value: 0},
|
||||
}
|
||||
|
||||
convey.Convey("Metrics comparison", t, func() {
|
||||
for _, expect := range expected {
|
||||
m := readMetric(<-ch)
|
||||
convey.So(expect, convey.ShouldResemble, m)
|
||||
}
|
||||
})
|
||||
if err := mock.ExpectationsWereMet(); err != nil {
|
||||
t.Errorf("there were unfulfilled exceptions: %s", err)
|
||||
}
|
||||
}
|
18
go.mod
18
go.mod
@ -9,8 +9,8 @@ require (
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/prometheus/client_golang v1.20.5
|
||||
github.com/prometheus/client_model v0.6.1
|
||||
github.com/prometheus/common v0.60.1
|
||||
github.com/prometheus/exporter-toolkit v0.13.1
|
||||
github.com/prometheus/common v0.61.0
|
||||
github.com/prometheus/exporter-toolkit v0.13.2
|
||||
github.com/smartystreets/goconvey v1.8.1
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
@ -36,11 +36,11 @@ require (
|
||||
github.com/rogpeppe/go-internal v1.10.0 // indirect
|
||||
github.com/smarty/assertions v1.15.0 // indirect
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
|
||||
golang.org/x/crypto v0.28.0 // indirect
|
||||
golang.org/x/net v0.29.0 // indirect
|
||||
golang.org/x/oauth2 v0.23.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.26.0 // indirect
|
||||
golang.org/x/text v0.19.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
golang.org/x/crypto v0.31.0 // indirect
|
||||
golang.org/x/net v0.32.0 // indirect
|
||||
golang.org/x/oauth2 v0.24.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/sys v0.28.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.35.2 // indirect
|
||||
)
|
||||
|
40
go.sum
40
go.sum
@ -54,10 +54,10 @@ github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+
|
||||
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.60.1 h1:FUas6GcOw66yB/73KC+BOZoFJmbo/1pojoILArPAaSc=
|
||||
github.com/prometheus/common v0.60.1/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
|
||||
github.com/prometheus/exporter-toolkit v0.13.1 h1:Evsh0gWQo2bdOHlnz9+0Nm7/OFfIwhE2Ws4A2jIlR04=
|
||||
github.com/prometheus/exporter-toolkit v0.13.1/go.mod h1:ujdv2YIOxtdFxxqtloLpbqmxd5J0Le6IITUvIRSWjj0=
|
||||
github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ=
|
||||
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
|
||||
github.com/prometheus/exporter-toolkit v0.13.2 h1:Z02fYtbqTMy2i/f+xZ+UK5jy/bl1Ex3ndzh06T/Q9DQ=
|
||||
github.com/prometheus/exporter-toolkit v0.13.2/go.mod h1:tCqnfx21q6qN1KA4U3Bfb8uWzXfijIrJz3/kTIqMV7g=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
@ -69,24 +69,24 @@ github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sS
|
||||
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
|
||||
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
|
||||
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
||||
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/oauth2 v0.23.0 h1:PbgcYx2W7i4LvjJWEbf0ngHV6qJYr86PkAV3bXdLEbs=
|
||||
golang.org/x/oauth2 v0.23.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
|
||||
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
|
||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
|
||||
golang.org/x/net v0.32.0/go.mod h1:CwU0IoeOlnQQWJ6ioyFrfRuomB8GKF6KbYXZVyeXNfs=
|
||||
golang.org/x/oauth2 v0.24.0 h1:KTBBxWqUa0ykRPLtV69rRto9TLXcqYkeswu48x/gvNE=
|
||||
golang.org/x/oauth2 v0.24.0/go.mod h1:XYTD2NtWslqkgxebSiOHnXEap4TF09sJSc7H1sXbhtI=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
|
||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
|
||||
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
|
Loading…
Reference in New Issue
Block a user