@@ -3,11 +3,13 @@ package main
3
3
import (
4
4
"database/sql"
5
5
"flag"
6
+ "fmt"
6
7
"net/http"
7
8
"time"
8
9
9
10
"github.com/ContaAzul/postgresql_exporter/config"
10
11
"github.com/ContaAzul/postgresql_exporter/gauges"
12
+ _ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
11
13
"github.com/apex/httplog"
12
14
"github.com/apex/log"
13
15
"github.com/apex/log/handlers/logfmt"
@@ -58,9 +60,25 @@ func main() {
58
60
for _ , con := range config .Databases {
59
61
var log = log .WithField ("db" , con .Name )
60
62
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
+ }
64
82
}
65
83
if err := db .Ping (); err != nil {
66
84
log .WithError (err ).Error ("failed to ping database" )
0 commit comments