Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit a815556

Browse files
authored
Merge pull request #68 from luizm/feat-add-support-to-gcp-proxy
feat: add support to gcp sqlproxy
2 parents 6ab34ce + fb3b532 commit a815556

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

config.yml

+8
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,11 @@ databases:
33
url: postgres://localhost:5432/dba?sslmode=disable
44
- name: dbb
55
url: postgres://user:[email protected]:5432/dbb
6+
- name: dbc
7+
# Used to connect into gcp sql instance using sqlproxy, when defined the URL is not necessary.
8+
# https://cloud.google.com/sql/docs/postgres/connect-admin-proxy#go
9+
sql:
10+
connection_name: gcp-project:region:instance-name
11+
database_name: dbc
12+
database_user: user
13+
database_password: pwd

config/config.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ import (
88
"gopkg.in/yaml.v2"
99
)
1010

11+
type Sql struct {
12+
ConnectionName string `yaml:"connection_name,omitempty"`
13+
DatabaseName string `yaml:"database_name,omitempty"`
14+
DatabaseUser string `yaml:"database_user,omitempty"`
15+
DatabasePassword string `yaml:"database_password,omitempty"`
16+
}
17+
1118
type Database struct {
1219
URL string `yaml:"url,omitempty"`
1320
Name string `yaml:"name,omitempty"`
21+
Sql Sql `yaml:"sql,omitempty"`
1422
}
1523

1624
type Config struct {
@@ -38,8 +46,8 @@ func validate(config Config) error {
3846
if conf.Name == "" {
3947
return errors.New("failed to validate configuration. Database name cannot be empty")
4048
}
41-
if conf.URL == "" {
42-
return fmt.Errorf("failed to validate configuration. URL for database '%s' cannot be empty", conf.Name)
49+
if conf.URL == "" && conf.Sql.ConnectionName == "" {
50+
return fmt.Errorf("failed to validate configuration. URL or sql field cannot be empty in the '%s' database", conf.Name)
4351
}
4452
if names[conf.Name] {
4553
return fmt.Errorf("failed to validate configuration. A database named '%s' has already been declared", conf.Name)

config/testdata/valid-config.yml

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ databases:
33
url: postgres://localhost:5432/dba?sslmode=disable
44
- name: dbb
55
url: postgres://user:[email protected]:5432/dbb
6+
- name: dbc
7+
sql:
8+
connection_name: gcp-project:region:instance-name
9+
database_name: dbc
10+
database_user: user
11+
database_password: pwd

main.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package main
33
import (
44
"database/sql"
55
"flag"
6+
"fmt"
67
"net/http"
78
"time"
89

910
"github.com/ContaAzul/postgresql_exporter/config"
1011
"github.com/ContaAzul/postgresql_exporter/gauges"
12+
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
1113
"github.com/apex/httplog"
1214
"github.com/apex/log"
1315
"github.com/apex/log/handlers/logfmt"
@@ -58,9 +60,25 @@ func main() {
5860
for _, con := range config.Databases {
5961
var log = log.WithField("db", con.Name)
6062
log.Info("started monitoring")
61-
db, err := sql.Open("postgres", con.URL)
62-
if err != nil {
63-
log.WithError(err).Error("failed to open url")
63+
var db *sql.DB
64+
65+
if con.Sql.ConnectionName != "" {
66+
log.Info(fmt.Sprintf("Using sqlproxy with connection name: %s", con.Sql.ConnectionName))
67+
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
68+
con.Sql.ConnectionName,
69+
con.Sql.DatabaseUser,
70+
con.Sql.DatabasePassword,
71+
con.Sql.DatabaseName)
72+
73+
db, err = sql.Open("cloudsqlpostgres", dsn)
74+
if err != nil {
75+
log.WithError(err).Error("failed to open url")
76+
}
77+
} else {
78+
db, err = sql.Open("postgres", con.URL)
79+
if err != nil {
80+
log.WithError(err).Error("failed to open url")
81+
}
6482
}
6583
if err := db.Ping(); err != nil {
6684
log.WithError(err).Error("failed to ping database")

0 commit comments

Comments
 (0)