Skip to content

Commit

Permalink
Drop sqlx.
Browse files Browse the repository at this point in the history
  • Loading branch information
jbub committed Jun 18, 2021
1 parent 8b74f7c commit 2329ee4
Show file tree
Hide file tree
Showing 12 changed files with 899 additions and 594 deletions.
12 changes: 8 additions & 4 deletions cmd/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package cmd

import (
"context"
"database/sql"
"fmt"

"github.com/jbub/pgbouncer_exporter/internal/config"
"github.com/jbub/pgbouncer_exporter/internal/store"
"github.com/jbub/pgbouncer_exporter/internal/sqlstore"

"github.com/urfave/cli/v2"
)
Expand All @@ -19,11 +20,14 @@ var Health = &cli.Command{

func checkHealth(ctx *cli.Context) error {
cfg := config.LoadFromCLI(ctx)
st, err := store.NewSQL(cfg.DatabaseURL)

db, err := sql.Open("postgres", cfg.DatabaseURL)
if err != nil {
return fmt.Errorf("unable to initialize store: %v", err)
return fmt.Errorf("could not initialize store: %v", err)
}
defer st.Close()
defer db.Close()

st := sqlstore.New(db)

checkCtx, cancel := context.WithTimeout(context.Background(), cfg.StoreTimeout)
defer cancel()
Expand Down
24 changes: 14 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package cmd

import (
"context"
"database/sql"
"fmt"
"log"

"github.com/jbub/pgbouncer_exporter/internal/collector"
"github.com/jbub/pgbouncer_exporter/internal/config"
"github.com/jbub/pgbouncer_exporter/internal/server"
"github.com/jbub/pgbouncer_exporter/internal/store"
"github.com/jbub/pgbouncer_exporter/internal/sqlstore"

"github.com/prometheus/common/log"
"github.com/prometheus/common/version"
"github.com/urfave/cli/v2"
)
Expand All @@ -23,11 +24,14 @@ var Server = &cli.Command{

func runServer(ctx *cli.Context) error {
cfg := config.LoadFromCLI(ctx)
st, err := store.NewSQL(cfg.DatabaseURL)

db, err := sql.Open("postgres", cfg.DatabaseURL)
if err != nil {
return fmt.Errorf("unable to initialize store: %v", err)
return fmt.Errorf("could not initialize store: %v", err)
}
defer st.Close()
defer db.Close()

st := sqlstore.New(db)

checkCtx, cancel := context.WithTimeout(context.Background(), cfg.StoreTimeout)
defer cancel()
Expand All @@ -39,13 +43,13 @@ func runServer(ctx *cli.Context) error {
exp := collector.New(cfg, st)
srv := server.New(cfg, exp)

log.Infoln("Starting ", collector.Name, version.Info())
log.Infoln("Server listening on", cfg.ListenAddress)
log.Infoln("Metrics available at", cfg.TelemetryPath)
log.Infoln("Build context", version.BuildContext())
log.Println("Starting ", collector.Name, version.Info())
log.Println("Server listening on", cfg.ListenAddress)
log.Println("Metrics available at", cfg.TelemetryPath)
log.Println("Build context", version.BuildContext())

if err := srv.Run(); err != nil {
return fmt.Errorf("unable to run server: %v", err)
return fmt.Errorf("could not run server: %v", err)
}
return nil
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ module github.com/jbub/pgbouncer_exporter
go 1.16

require (
github.com/jmoiron/sqlx v1.3.1
github.com/lib/pq v1.9.0
github.com/prometheus/client_golang v1.9.0
github.com/prometheus/common v0.17.0
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/lib/pq v1.10.2
github.com/prometheus/client_golang v1.11.0
github.com/prometheus/common v0.29.0
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
)
503 changes: 274 additions & 229 deletions go.sum

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions internal/collector/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package collector
import (
"context"
"fmt"
"log"
"strings"
"sync"

"github.com/jbub/pgbouncer_exporter/internal/config"
"github.com/jbub/pgbouncer_exporter/internal/domain"

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

const (
Expand Down Expand Up @@ -94,7 +94,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {

res, err := e.getStoreResult(ctx)
if err != nil {
log.Errorf("could not get store result: %v", err)
log.Printf("could not get store result: %v", err)
return
}

Expand Down Expand Up @@ -122,31 +122,31 @@ func (e *Exporter) getStoreResult(ctx context.Context) (*storeResult, error) {
if e.cfg.ExportStats {
stats, err := e.stor.GetStats(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get stats: %v", err)
return nil, fmt.Errorf("could not get stats: %v", err)
}
res.stats = stats
}

if e.cfg.ExportPools {
pools, err := e.stor.GetPools(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get pools: %v", err)
return nil, fmt.Errorf("could not get pools: %v", err)
}
res.pools = pools
}

if e.cfg.ExportDatabases {
databases, err := e.stor.GetDatabases(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get databases: %v", err)
return nil, fmt.Errorf("could not get databases: %v", err)
}
res.databases = databases
}

if e.cfg.ExportLists {
lists, err := e.stor.GetLists(ctx)
if err != nil {
return nil, fmt.Errorf("unable to get lists: %v", err)
return nil, fmt.Errorf("could not get lists: %v", err)
}
res.lists = lists
}
Expand Down
62 changes: 33 additions & 29 deletions internal/collector/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,60 @@ import (
"testing"

"github.com/jbub/pgbouncer_exporter/internal/config"
"github.com/jbub/pgbouncer_exporter/internal/store"
"github.com/jbub/pgbouncer_exporter/internal/sqlstore"

"github.com/DATA-DOG/go-sqlmock"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
)

func TestGetStoreResultExportEnabled(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer db.Close()

cfg := config.Config{
ExportStats: true,
ExportPools: true,
ExportDatabases: true,
ExportLists: true,
}
st := store.NewMockStore()
defer st.Close()

exp := New(cfg, st)
exp := New(cfg, sqlstore.New(db))
ctx := context.Background()

res, err := exp.getStoreResult(ctx)
require.NoError(t, err)

require.True(t, st.StatsCalled)
require.True(t, st.PoolsCalled)
require.True(t, st.DatabasesCalled)
require.True(t, st.ListsCalled)
mock.ExpectQuery("SHOW STATS").WillReturnRows(sqlmock.NewRows(nil))
mock.ExpectQuery("SHOW POOLS").WillReturnRows(sqlmock.NewRows(nil))
mock.ExpectQuery("SHOW DATABASES").WillReturnRows(sqlmock.NewRows(nil))
mock.ExpectQuery("SHOW LISTS").WillReturnRows(sqlmock.NewRows(nil))

require.Equal(t, res.stats, st.Stats)
require.Equal(t, res.pools, st.Pools)
require.Equal(t, res.databases, st.Databases)
require.Equal(t, res.lists, st.Lists)
_, err = exp.getStoreResult(ctx)
require.NoError(t, err)
require.NoError(t, mock.ExpectationsWereMet())
}

func TestGetStoreResultExportDisabled(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatal(err)
}
defer db.Close()

cfg := config.Config{
ExportStats: false,
ExportPools: false,
ExportDatabases: false,
ExportLists: false,
}
st := store.NewMockStore()
defer st.Close()

exp := New(cfg, st)
exp := New(cfg, sqlstore.New(db))
ctx := context.Background()

res, err := exp.getStoreResult(ctx)
_, err = exp.getStoreResult(ctx)
require.NoError(t, err)

require.False(t, st.StatsCalled)
require.False(t, st.PoolsCalled)
require.False(t, st.DatabasesCalled)
require.False(t, st.ListsCalled)

require.Equal(t, res.stats, st.Stats)
require.Equal(t, res.pools, st.Pools)
require.Equal(t, res.databases, st.Databases)
require.Equal(t, res.lists, st.Lists)
require.NoError(t, mock.ExpectationsWereMet())
}

var (
Expand Down Expand Up @@ -103,6 +99,14 @@ var (
"key2": "value2",
},
},
{
name: "multiple items with space",
value: "key=value key2=value2 ",
expected: prometheus.Labels{
"key": "value",
"key2": "value2",
},
},
}
)

Expand Down
3 changes: 0 additions & 3 deletions internal/domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,4 @@ type Store interface {

// Check checks the health of the store.
Check(ctx context.Context) error

// Close closes the store.
Close() error
}
Loading

0 comments on commit 2329ee4

Please sign in to comment.