Skip to content

Commit

Permalink
Add revive as linter (#243)
Browse files Browse the repository at this point in the history
Add revive as linter

https://github.com/mgechev/revive

Fixed most of the issues and relaxed some of the rules regarding method
length and complexity to avoid need to write some of the scrapers on
this iteration..

Also update to go 1.24.5 and add docker compose with prometheus and
postgres 14.
  • Loading branch information
rnaveiras authored Jan 18, 2025
2 parents 30747c4 + 98c4d23 commit 9691e3f
Show file tree
Hide file tree
Showing 21 changed files with 218 additions and 1,714 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: set up go
uses: actions/setup-go@v5
with:
go-version: 1.22.1
go-version: 1.23.5
id: go
- name: go mod tidy
run: go mod tidy
Expand All @@ -55,7 +55,7 @@ jobs:
- name: set up go
uses: actions/setup-go@v5
with:
go-version: 1.22.1
go-version: 1.23.5
id: go
- run: go version
- name: Promu
Expand Down
20 changes: 15 additions & 5 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
---
run:
deadline: 5m

output:
sort-results: true

Expand All @@ -10,7 +7,7 @@ linters:
# - depguard
- gofumpt
- goimports
# - revive
- revive
- bodyclose
# - gochecknoglobals
- errname
Expand All @@ -25,5 +22,18 @@ linters:
- sqlclosecheck

linters-settings:
revive:
enable-all-rules: true
ignore-generated-header: true
rules:
- name: line-length-limit
arguments: [160]
- name: function-length
arguments: [50, 150] # Maximum number of lines allowed in a function
- name: cognitive-complexity
arguments: [12]
- name: cyclomatic
arguments: [12]
errcheck:
exclude: .errcheck_excludes.txt
exclude-functions:
- (github.com/go-kit/log.Logger).Log
2 changes: 1 addition & 1 deletion .promu.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
go:
version: 1.22.1
version: 1.23.5
repository:
path: github.com/rnaveiras/postgres_exporter
build:
Expand Down
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# syntax=docker/dockerfile:1
FROM golang:1.22.1-alpine3.19 as builder
FROM golang:1.23.5-alpine3.21 AS builder

ENV PROMU_SHA256=f92fd94dbd5941c7f2925860c3d6a1f24b7630cb2b192df43835c8dda9e76b5d \
PROMU_VERSION=0.15.0
PROMU_VERSION=0.15.0

SHELL ["/bin/ash", "-euox", "pipefail", "-c"]

Expand All @@ -23,7 +22,7 @@ RUN set -x \
&& find ./output

FROM alpine:3.19
LABEL maintainer="Raul Naveiras <rnaveiras@gmail.com>"
LABEL org.opencontainers.image.authors="Raul Naveiras <rnaveiras@gmail.com>"

COPY --from=builder /go/src/app/output/postgres_exporter /bin/postgres_exporter

Expand Down
4 changes: 2 additions & 2 deletions collector/disk_usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func NewDiskUsageScraper() Scraper {
}
}

func (c *diskUsageScraper) Name() string {
func (*diskUsageScraper) Name() string {
return "DiskUsageScraper"
}

func (c *diskUsageScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *diskUsageScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
var datname, schemaname, tablename, indexname string
var sizeBytes float64
var rows pgx.Rows
Expand Down
47 changes: 26 additions & 21 deletions collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ import (
"github.com/prometheus/client_golang/prometheus"
)

const (
versionBitSize = 64
infoQuery = `SHOW server_version /*postgres_exporter*/`
listDatnameQuery = `
SELECT datname FROM pg_database
WHERE datallowconn = true AND datistemplate = false
AND datname != 'cloudsqladmin' /*postgres_exporter*/`
successValue = 1.0
failureValue = 0.0
infoMetricValue = 1.0
levelError = "error" // Used for error logging
)

var (
upDesc = prometheus.NewDesc(
"postgres_up",
Expand Down Expand Up @@ -40,14 +53,6 @@ var (
)
)

const (
infoQuery = `SHOW server_version /*postgres_exporter*/`
listDatnameQuery = `
SELECT datname FROM pg_database
WHERE datallowconn = true AND datistemplate = false
AND datname != 'cloudsqladmin' /*postgres_exporter*/`
)

// Scraper is the interface each scraper has to implement.
type Scraper interface {
Name() string
Expand All @@ -70,7 +75,7 @@ type Version struct {

func NewVersion(v string) Version {
values := strings.Split(v, " ")
version, _ := strconv.ParseFloat(values[0], 64)
version, _ := strconv.ParseFloat(values[0], versionBitSize)
return Version{
version: version,
}
Expand Down Expand Up @@ -114,7 +119,7 @@ func NewExporter(ctx context.Context, logger kitlog.Logger, connConfig *pgx.Conn
}

// Describe implements the prometheus.Collector interface.
func (e Exporter) Describe(ch chan<- *prometheus.Desc) {
func (Exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
}
Expand All @@ -123,31 +128,31 @@ func (e Exporter) Describe(ch chan<- *prometheus.Desc) {
func (e Exporter) Collect(ch chan<- prometheus.Metric) {
conn, err := pgx.ConnectConfig(e.ctx, e.connConfig)
if err != nil {
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 0)
level.Error(e.logger).Log("error", err)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, failureValue)
level.Error(e.logger).Log(levelError, err)
return // cannot continue without a valid connection
}

defer conn.Close(e.ctx)
// postgres_up
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, 1)
ch <- prometheus.MustNewConstMetric(upDesc, prometheus.GaugeValue, successValue)

var version string
if err := conn.QueryRow(e.ctx, infoQuery).Scan(&version); err != nil {
level.Error(e.logger).Log("error", err)
level.Error(e.logger).Log(levelError, err)
return // cannot continue without a version
}

v := NewVersion(version)
// postgres_info
ch <- prometheus.MustNewConstMetric(infoDesc, prometheus.GaugeValue, 1, v.String())
ch <- prometheus.MustNewConstMetric(infoDesc, prometheus.GaugeValue, infoMetricValue, v.String())

// discovery databases
var dbnames []string

rows, err := conn.Query(e.ctx, listDatnameQuery)
if err != nil {
level.Error(e.logger).Log("error", err)
level.Error(e.logger).Log(levelError, err)
return
}
defer rows.Close()
Expand All @@ -156,7 +161,7 @@ func (e Exporter) Collect(ch chan<- prometheus.Metric) {
var dbname string
err := rows.Scan(&dbname)
if err != nil {
level.Error(e.logger).Log("error", err)
level.Error(e.logger).Log(levelError, err)
return
}
dbnames = append(dbnames, dbname)
Expand All @@ -175,7 +180,7 @@ func (e Exporter) Collect(ch chan<- prometheus.Metric) {
// establish a new connection
conn, err := pgx.ConnectConfig(e.ctx, e.connConfig)
if err != nil {
level.Error(e.logger).Log("error", err)
level.Error(e.logger).Log(levelError, err)
return // cannot continue without a valid connection
}

Expand All @@ -197,11 +202,11 @@ func (e Exporter) scrape(scraper Scraper, conn *pgx.Conn, version Version, ch ch

logger := kitlog.With(e.logger, "scraper", scraper.Name(), "duration", duration.Seconds())
if err != nil {
logger.Log("error", err)
success = 0
logger.Log(levelError, err)
success = successValue
} else {
level.Debug(logger).Log("event", "scraper.success")
success = 1
success = failureValue
}

datname := e.connConfig.Config.Database
Expand Down
4 changes: 2 additions & 2 deletions collector/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func NewInfoScraper() Scraper {
}
}

func (c *infoScraper) Name() string {
func (*infoScraper) Name() string {
return "InfoScraper"
}

func (c *infoScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *infoScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
var recovery, backup int64
var startTime, configLoadTime time.Time

Expand Down
4 changes: 2 additions & 2 deletions collector/locks.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func NewLocksScraper() Scraper {
}
}

func (c *locksScraper) Name() string {
func (*locksScraper) Name() string {
return "LocksScraper"
}

func (c *locksScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *locksScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
rows, err := conn.Query(ctx, locksQuery)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions collector/stat_activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ func NewStatActivityScraper() Scraper {
}
}

func (c *statActivityScraper) Name() string {
func (*statActivityScraper) Name() string {
return "StatActivityScraper"
}

func (c *statActivityScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *statActivityScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
rows, err := conn.Query(ctx, statActivityQuery)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions collector/stat_archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func NewStatArchiverScraper() Scraper {
}
}

func (c *statArchiverScraper) Name() string {
func (*statArchiverScraper) Name() string {
return "StatArchiverScraper"
}

func (c *statArchiverScraper) Scrape(ctx context.Context, db *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *statArchiverScraper) Scrape(ctx context.Context, db *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
var archivedCount, failedCount int64
var statsReset time.Time

Expand Down
4 changes: 2 additions & 2 deletions collector/stat_bgwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,11 @@ func NewStatBgwriterScraper() Scraper {
}
}

func (c *statBgwriterScraper) Name() string {
func (*statBgwriterScraper) Name() string {
return "StatBgwriterScraper"
}

func (c *statBgwriterScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *statBgwriterScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
var checkpointsTimedCounter, checkpointsReqCounter,
buffersCheckpoint, buffersClean, maxWrittenClean,
buffersBackend, buffersBackendFsync, buffersAlloc int64
Expand Down
4 changes: 2 additions & 2 deletions collector/stat_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ func NewStatDatabaseScraper() Scraper {
}
}

func (c *statDatabaseScraper) Name() string {
func (*statDatabaseScraper) Name() string {
return "StatDatabaseScraper"
}

func (c *statDatabaseScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *statDatabaseScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
rows, err := conn.Query(ctx, statDatabaseQuery)
if err != nil {
return err
Expand Down
32 changes: 22 additions & 10 deletions collector/stat_progress_vacuum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
)

const (
// metricEnabled represent the value used when a metrics state is
// active/enabled
metricEnabled = 1.0

// Scrape query
statVacuumProgress = `
SELECT V.pid::text
Expand Down Expand Up @@ -135,11 +139,11 @@ func NewStatVacuumProgressScraper() Scraper {
}
}

func (c *statVacuumProgressScraper) Name() string {
func (*statVacuumProgressScraper) Name() string {
return "StatVacuumProgressScraper"
}

func (c *statVacuumProgressScraper) Scrape(ctx context.Context, conn *pgx.Conn, version Version, ch chan<- prometheus.Metric) error {
func (c *statVacuumProgressScraper) Scrape(ctx context.Context, conn *pgx.Conn, _ Version, ch chan<- prometheus.Metric) error {
rows, err := conn.Query(ctx, statVacuumProgress)
if err != nil {
return err
Expand All @@ -166,30 +170,38 @@ func (c *statVacuumProgressScraper) Scrape(ctx context.Context, conn *pgx.Conn,
}

// postgres_stat_vacuum_progress_running
ch <- prometheus.MustNewConstMetric(c.running, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.running, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)

switch phase {
case "initializing":
// postgres_stat_vacuum_progress_phase_initializing
ch <- prometheus.MustNewConstMetric(c.phaseInitializing, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseInitializing, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "scanning heap":
// postgres_stat_vacuum_progress_phase_scanning_heap
ch <- prometheus.MustNewConstMetric(c.phaseScanningHeap, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseScanningHeap, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "vacuuming indexes":
// postgres_stat_vacuum_progress_phase_vacuuming_indexes
ch <- prometheus.MustNewConstMetric(c.phaseVacuumingIndexes, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseVacuumingIndexes, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "vacuuming heap":
// postgres_stat_vacuum_progress_phase_vacuuming_heap
ch <- prometheus.MustNewConstMetric(c.phaseVacuumingHeap, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseVacuumingHeap, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "cleaning up indexes":
// postgres_stat_vacuum_progress_phase_cleaning_up_indexes
ch <- prometheus.MustNewConstMetric(c.phaseCleaningUpIndexes, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseCleaningUpIndexes, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "truncating heap":
// postgres_stat_vacuum_progress_phase_truncating_heap
ch <- prometheus.MustNewConstMetric(c.phaseTruncatingHeap, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phaseTruncatingHeap, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
case "performing final cleanup":
// postgres_stat_vacuum_progress_phase_performing_final_cleanup
ch <- prometheus.MustNewConstMetric(c.phasePerformingFinalCleanup, prometheus.GaugeValue, 1, pid, queryStart, schemaname, datname, relname)
ch <- prometheus.MustNewConstMetric(c.phasePerformingFinalCleanup, prometheus.GaugeValue, metricEnabled,
pid, queryStart, schemaname, datname, relname)
}

// postgres_stat_vacuum_progress_heap_blks_total
Expand Down
Loading

0 comments on commit 9691e3f

Please sign in to comment.