@@ -16,15 +16,17 @@ package main
16
16
import (
17
17
//"context"
18
18
"fmt"
19
+ "github.com/prometheus-community/postgres_exporter/collector"
19
20
"net/http"
20
21
"os"
22
+ "strings"
23
+
21
24
//"strconv"
22
25
//"strings"
23
26
//"time"
24
27
25
28
"github.com/go-kit/log"
26
29
"github.com/go-kit/log/level"
27
- "github.com/prometheus-community/postgres_exporter/collector"
28
30
"github.com/prometheus/client_golang/prometheus"
29
31
"github.com/prometheus/client_golang/prometheus/promhttp"
30
32
"github.com/prometheus/common/promlog"
@@ -73,24 +75,17 @@ func newHandler() http.HandlerFunc {
73
75
74
76
v := r .URL .Query ()
75
77
//params := v["collect[]"]
76
- insname := v .Get ("target" )
77
- dbname := v .Get ("dbname" )
78
- user := v .Get ("user" )
79
- password := v .Get ("password" )
78
+ target := v .Get ("target" )
79
+ insname := strings .Split (target ,":" )[0 ]
80
+ port := strings .Split (target ,":" )[1 ]
81
+ dbname := strings .Split (target ,":" )[4 ]
82
+ user := strings .Split (target ,":" )[2 ]
83
+ password := strings .Split (target ,":" )[3 ]
80
84
ui := url .UserPassword (user , password ).String ()
81
- dsn := fmt .Sprintf ("postgresql://" + ui + "@" + insname + "/" + dbname + "?sslmode=disable" )
85
+ dsn := fmt .Sprintf ("postgresql://" + ui + "@" + insname + ":" + port + "/" + dbname + "?sslmode=disable" )
82
86
// Use request context for cancellation when connection gets closed.
83
87
level .Info (logger ).Log (dsn )
84
88
85
- if len (dsn ) == 0 {
86
- level .Error (logger ).Log ("msg" , "Couldn't find environment variables describing the datasource to use" )
87
- //dsn, err := getDataSources()
88
- //if err != nil {
89
- // level.Error(logger).Log("msg", "Failed reading data sources", "err", err.Error())
90
- // os.Exit(1)
91
- //}
92
- }
93
-
94
89
opts := []ExporterOpt {
95
90
DisableDefaultMetrics (* disableDefaultMetrics ),
96
91
DisableSettingsMetrics (* disableSettingsMetrics ),
@@ -106,19 +101,23 @@ func newHandler() http.HandlerFunc {
106
101
exporter .servers .Close ()
107
102
}()
108
103
109
- prometheus .MustRegister (version .NewCollector (exporterName ))
110
-
111
- prometheus .MustRegister (exporter )
112
104
105
+ registry := prometheus .NewRegistry ()
106
+ registry .MustRegister (version .NewCollector (exporterName ))
107
+ registry .MustRegister (exporter )
113
108
pe , err := collector .NewPostgresCollector (logger , []string {dsn })
114
109
if err != nil {
115
110
level .Error (logger ).Log ("msg" , "Failed to create PostgresCollector" , "err" , err .Error ())
116
111
os .Exit (1 )
117
112
}
118
- prometheus .MustRegister (pe )
119
113
114
+ registry .MustRegister (pe )
115
+ gatherers := prometheus.Gatherers {
116
+ prometheus .DefaultGatherer ,
117
+ registry ,
118
+ }
120
119
// Delegate http serving to Prometheus client library, which will call collector.Collect.
121
- h := promhttp .HandlerFor (prometheus . DefaultGatherer , promhttp.HandlerOpts {})
120
+ h := promhttp .HandlerFor (gatherers , promhttp.HandlerOpts {})
122
121
h .ServeHTTP (w , r )
123
122
}
124
123
}
0 commit comments