Skip to content

Add a collector for pg_proctab. #1168

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
52 changes: 26 additions & 26 deletions collector/pg_locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,39 +49,39 @@ var (
)

pgLocksQuery = `
SELECT
SELECT
pg_database.datname as datname,
tmp.mode as mode,
COALESCE(count, 0) as count
FROM
COALESCE(count, 0) as count
FROM
(
VALUES
('accesssharelock'),
('rowsharelock'),
('rowexclusivelock'),
('shareupdateexclusivelock'),
('sharelock'),
('sharerowexclusivelock'),
('exclusivelock'),
('accessexclusivelock'),
VALUES
('accesssharelock'),
('rowsharelock'),
('rowexclusivelock'),
('shareupdateexclusivelock'),
('sharelock'),
('sharerowexclusivelock'),
('exclusivelock'),
('accessexclusivelock'),
('sireadlock')
) AS tmp(mode)
CROSS JOIN pg_database
CROSS JOIN pg_database
LEFT JOIN (
SELECT
database,
lower(mode) AS mode,
count(*) AS count
FROM
pg_locks
WHERE
database IS NOT NULL
GROUP BY
database,
SELECT
database,
lower(mode) AS mode,
count(*) AS count
FROM
pg_locks
WHERE
database IS NOT NULL
GROUP BY
database,
lower(mode)
) AS tmp2 ON tmp.mode = tmp2.mode
and pg_database.oid = tmp2.database
ORDER BY
) AS tmp2 ON tmp.mode = tmp2.mode
and pg_database.oid = tmp2.database
ORDER BY
1
`
)
Expand Down
278 changes: 278 additions & 0 deletions collector/pg_proctab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
// Copyright 2025 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"
"log/slog"

"github.com/prometheus/client_golang/prometheus"
)

const proctabSubsystem = "proctab"

func init() {
registerCollector(proctabSubsystem, defaultDisabled, NewPGProctabCollector)
}

type PGProctabCollector struct {
log *slog.Logger
}

func NewPGProctabCollector(config collectorConfig) (Collector, error) {
return &PGProctabCollector{
log: config.logger,
}, nil
}

var (
pgMemusedDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"memused",
),
"used memory (from /proc/meminfo) in bytes",
[]string{}, nil,
)

pgMemfreeDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"memfree",
),
"free memory (from /proc/meminfo) in bytes",
[]string{}, nil,
)

pgMemsharedDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"memshared",
),
"shared memory (from /proc/meminfo) in bytes",
[]string{}, nil,
)

pgMembuffersDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"membuffers",
),
"buffered memory (from /proc/meminfo) in bytes",
[]string{}, nil,
)

pgMemcachedDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"memcached",
),
"cached memory (from /proc/meminfo) in bytes",
[]string{}, nil,
)
pgSwapusedDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"swapused",
),
"swap used (from /proc/meminfo) in bytes",
[]string{}, nil,
)

// Loadavg metrics
pgLoad1Desc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"load1",
),
"load1 load Average",
[]string{}, nil,
)

// CPU metrics
pgCpuUserDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"cpu_user",
),
"PG User cpu time",
[]string{}, nil,
)
pgCpuNiceDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"cpu_nice",
),
"PG nice cpu time (running queries)",
[]string{}, nil,
)
pgCpuSystemDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"cpu_system",
),
"PG system cpu time",
[]string{}, nil,
)
pgCpuIdleDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"cpu_idle",
),
"PG idle cpu time",
[]string{}, nil,
)
pgCpuIowaitDesc = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
proctabSubsystem,
"cpu_iowait",
),
"PG iowait time",
[]string{}, nil,
)

memoryQuery = `
select
memused,
memfree,
memshared,
membuffers,
memcached,
swapused
from
pg_memusage()
`

load1Query = `
select
load1
from
pg_loadavg()
`
cpuQuery = `
select
"user",
nice,
system,
idle,
iowait
from
pg_cputime()
`
)

func emitMemMetric(m sql.NullInt64, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
mM := 0.0
if m.Valid {
mM = float64(m.Int64 * 1024)
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, mM)
}
func emitCpuMetric(m sql.NullInt64, desc *prometheus.Desc, ch chan<- prometheus.Metric) {
mM := 0.0
if m.Valid {
mM = float64(m.Int64)
}
ch <- prometheus.MustNewConstMetric(desc, prometheus.CounterValue, mM)
}

// Update implements Collector and exposes database locks.
// It is called by the Prometheus registry when collecting metrics.
func (c PGProctabCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
db := instance.getDB()
// Query the list of databases
rows, err := db.QueryContext(ctx, memoryQuery)
if err != nil {
return err
}
defer rows.Close()

var memused, memfree, memshared, membuffers, memcached, swapused sql.NullInt64

for rows.Next() {
if err := rows.Scan(&memused, &memfree, &memshared, &membuffers, &memcached, &swapused); err != nil {
return err
}
emitMemMetric(memused, pgMemusedDesc, ch)
emitMemMetric(memfree, pgMemfreeDesc, ch)
emitMemMetric(memshared, pgMemsharedDesc, ch)
emitMemMetric(membuffers, pgMembuffersDesc, ch)
emitMemMetric(memcached, pgMemcachedDesc, ch)
emitMemMetric(swapused, pgSwapusedDesc, ch)
}

if err := rows.Err(); err != nil {
return err
}

rows, err = db.QueryContext(ctx, load1Query)
if err != nil {
return err
}
defer rows.Close()

var load1 sql.NullFloat64
for rows.Next() {
if err := rows.Scan(&load1); err != nil {
return err
}
load1Metric := 0.0
if load1.Valid {
load1Metric = load1.Float64
}
ch <- prometheus.MustNewConstMetric(
pgLoad1Desc,
prometheus.GaugeValue, load1Metric,
)
}
if err := rows.Err(); err != nil {
return err
}

rows, err = db.QueryContext(ctx, cpuQuery)
if err != nil {
return err
}
defer rows.Close()
var user, nice, system, idle, iowait sql.NullInt64
for rows.Next() {
if err := rows.Scan(&user, &nice, &system, &idle, &iowait); err != nil {
return err
}
emitCpuMetric(user, pgCpuUserDesc, ch)
emitCpuMetric(nice, pgCpuNiceDesc, ch)
emitCpuMetric(system, pgCpuSystemDesc, ch)
emitCpuMetric(idle, pgCpuIdleDesc, ch)
emitCpuMetric(iowait, pgCpuIowaitDesc, ch)
}
if err := rows.Err(); err != nil {
return err
}

return nil

}
Loading