Skip to content
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

Add stat_activity_oldest_backend_xmin #38

Merged
merged 1 commit into from
Feb 27, 2020
Merged
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
22 changes: 21 additions & 1 deletion collector/stat_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ SELECT EXTRACT(EPOCH FROM age(clock_timestamp(), coalesce(min(query_start), cloc
SELECT EXTRACT(EPOCH FROM age(clock_timestamp(), coalesce(min(query_start), clock_timestamp())))
FROM pg_stat_activity
WHERE backend_xmin IS NOT NULL /*postgres_exporter*/`

// Oldest snapshot transaction ID
// There should be no need to coalesce to a non-null value, as this query
// itself will hold a snapshot even if no other backends have an xmin.
statActivityScraperOldestSnapshotXidQuery = `SELECT min(backend_xmin::text::float) FROM pg_stat_activity /*postgres_exporter*/`
)

type statActivityScraper struct {
Expand All @@ -58,6 +63,7 @@ type statActivityScraper struct {
xact *prometheus.Desc
active *prometheus.Desc
snapshot *prometheus.Desc
xmin *prometheus.Desc
}

// NewStatActivityScraper returns a new Scraper exposing postgres pg_stat_activity
Expand Down Expand Up @@ -93,6 +99,12 @@ func NewStatActivityScraper() Scraper {
nil,
nil,
),
xmin: prometheus.NewDesc(
"postgres_stat_activity_oldest_backend_xmin",
"The lowest backend_xmin across all backends",
nil,
nil,
),
}
}

Expand All @@ -108,7 +120,7 @@ func (c *statActivityScraper) Scrape(ctx context.Context, conn *pgx.Conn, versio
defer rows.Close()

var datname, state string
var count, oldestTx, oldestActive, oldestSnapshot float64
var count, oldestTx, oldestActive, oldestSnapshot, oldestXmin float64
var oldestBackend time.Time

for rows.Next() {
Expand Down Expand Up @@ -157,5 +169,13 @@ func (c *statActivityScraper) Scrape(ctx context.Context, conn *pgx.Conn, versio
// postgres_stat_activity_oldest_snapshot_seconds
ch <- prometheus.MustNewConstMetric(c.snapshot, prometheus.GaugeValue, oldestSnapshot)

err = conn.QueryRow(ctx, statActivityScraperOldestSnapshotXidQuery).Scan(&oldestXmin)
if err != nil {
return err
}

// postgres_stat_activity_oldest_backend_xmin
ch <- prometheus.MustNewConstMetric(c.xmin, prometheus.GaugeValue, oldestXmin)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure now that this should be a counter, not a gauge (as long as my assertion about the query that retrieves this metric always holding a snapshot is correct)

Thoughts welcome though


return nil
}