Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add pgbouncer_version metric.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Aug 5, 2021
1 parent 9bb0c8d commit 5ad122c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
37 changes: 36 additions & 1 deletion internal/collector/pgbouncer_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ package collector

import (
"bufio"
"context"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaponry/pgscv/internal/log"
"github.com/weaponry/pgscv/internal/model"
"github.com/weaponry/pgscv/internal/store"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)

const (
// admin console query used for retrieving settings
settingsQuery = "SHOW CONFIG"
versionQuery = "SHOW VERSION"
)

type pgbouncerSettingsCollector struct {
version typedDesc
settings typedDesc
dbSettings typedDesc
poolSize typedDesc
Expand All @@ -28,6 +31,12 @@ type pgbouncerSettingsCollector struct {
// For details see https://www.pgbouncer.org/usage.html#show-config.
func NewPgbouncerSettingsCollector(constLabels labels, settings model.CollectorSettings) (Collector, error) {
return &pgbouncerSettingsCollector{
version: newBuiltinTypedDesc(
descOpts{"pgbouncer", "", "version", "Numeric representation of Pgbouncer version.", 0},
prometheus.GaugeValue,
[]string{"version"}, constLabels,
settings.Filters,
),
settings: newBuiltinTypedDesc(
descOpts{"pgbouncer", "service", "settings_info", "Labeled information about Pgbouncer configuration settings.", 0},
prometheus.GaugeValue,
Expand Down Expand Up @@ -57,6 +66,13 @@ func (c *pgbouncerSettingsCollector) Update(config Config, ch chan<- prometheus.
}
defer conn.Close()

version, versionStr, err := queryPgbouncerVersion(conn)
if err != nil {
return err
}

ch <- c.version.newConstMetric(float64(version), versionStr)

res, err := conn.Query(settingsQuery)
if err != nil {
return err
Expand Down Expand Up @@ -100,6 +116,25 @@ func (c *pgbouncerSettingsCollector) Update(config Config, ch chan<- prometheus.
return nil
}

// queryPgbouncerVersion queries version info from Pgbouncer and return numeric and string version representation.
func queryPgbouncerVersion(conn *store.DB) (int, string, error) {
var versionStr string
err := conn.Conn().QueryRow(context.Background(), versionQuery).Scan(&versionStr)
if err != nil {
return 0, "", err
}

re := regexp.MustCompile(`\d+\.\d+\.\d+`)
versionStr = re.FindString(versionStr)

version, err := semverStringToInt(versionStr)
if err != nil {
return 0, "", fmt.Errorf("parse version string '%s' failed: %s", versionStr, err)
}

return version, versionStr, nil
}

// parsePgbouncerSettings parses content of 'SHOW CONFIG' and return map with parsed settings.
func parsePgbouncerSettings(r *model.PGResult) map[string]string {
log.Debug("parse pgbouncer settings")
Expand Down
11 changes: 11 additions & 0 deletions internal/collector/pgbouncer_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"github.com/jackc/pgproto3/v2"
"github.com/stretchr/testify/assert"
"github.com/weaponry/pgscv/internal/model"
"github.com/weaponry/pgscv/internal/store"
"testing"
)

func TestPgbouncerSettingsCollector_Update(t *testing.T) {
var input = pipelineInput{
required: []string{
"pgbouncer_version",
"pgbouncer_service_settings_info",
"pgbouncer_service_database_settings_info",
"pgbouncer_service_database_pool_size",
Expand All @@ -22,6 +24,15 @@ func TestPgbouncerSettingsCollector_Update(t *testing.T) {
pipeline(t, input)
}

func Test_queryPgbouncerVersion(t *testing.T) {
db := store.NewTestPgbouncer(t)

str, num, err := queryPgbouncerVersion(db)
assert.NoError(t, err)
assert.NotEqual(t, "", str)
assert.NotEqual(t, 0, num)
}

func Test_parsePgbouncerSettings(t *testing.T) {
var testCases = []struct {
name string
Expand Down
9 changes: 8 additions & 1 deletion internal/store/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ import (
"testing"
)

const TestPostgresConnStr = "host=127.0.0.1 dbname=pgscv_fixtures user=pgscv sslmode=disable"
const TestPostgresConnStr = "host=127.0.0.1 port=5432 user=pgscv dbname=pgscv_fixtures sslmode=disable"
const TestPgbouncerConnStr = "host=127.0.0.1 port=6432 user=pgscv dbname=pgbouncer sslmode=disable"

func NewTest(t *testing.T) *DB {
db, err := New(TestPostgresConnStr)
assert.NoError(t, err)
return db
}

func NewTestPgbouncer(t *testing.T) *DB {
db, err := New(TestPgbouncerConnStr)
assert.NoError(t, err)
return db
}

0 comments on commit 5ad122c

Please sign in to comment.