Skip to content

Commit dc23aea

Browse files
author
lupx
committed
fix 注册
1 parent c3d50eb commit dc23aea

File tree

3 files changed

+19
-20
lines changed

3 files changed

+19
-20
lines changed

cmd/postgres_exporter/main.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ package main
1616
import (
1717
//"context"
1818
"fmt"
19+
"github.com/prometheus-community/postgres_exporter/collector"
1920
"net/http"
2021
"os"
22+
"strings"
23+
2124
//"strconv"
2225
//"strings"
2326
//"time"
2427

2528
"github.com/go-kit/log"
2629
"github.com/go-kit/log/level"
27-
"github.com/prometheus-community/postgres_exporter/collector"
2830
"github.com/prometheus/client_golang/prometheus"
2931
"github.com/prometheus/client_golang/prometheus/promhttp"
3032
"github.com/prometheus/common/promlog"
@@ -73,24 +75,17 @@ func newHandler() http.HandlerFunc {
7375

7476
v := r.URL.Query()
7577
//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]
8084
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")
8286
// Use request context for cancellation when connection gets closed.
8387
level.Info(logger).Log(dsn)
8488

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-
9489
opts := []ExporterOpt{
9590
DisableDefaultMetrics(*disableDefaultMetrics),
9691
DisableSettingsMetrics(*disableSettingsMetrics),
@@ -106,19 +101,23 @@ func newHandler() http.HandlerFunc {
106101
exporter.servers.Close()
107102
}()
108103

109-
prometheus.MustRegister(version.NewCollector(exporterName))
110-
111-
prometheus.MustRegister(exporter)
112104

105+
registry := prometheus.NewRegistry()
106+
registry.MustRegister(version.NewCollector(exporterName))
107+
registry.MustRegister(exporter)
113108
pe, err := collector.NewPostgresCollector(logger, []string{dsn})
114109
if err != nil {
115110
level.Error(logger).Log("msg", "Failed to create PostgresCollector", "err", err.Error())
116111
os.Exit(1)
117112
}
118-
prometheus.MustRegister(pe)
119113

114+
registry.MustRegister(pe)
115+
gatherers := prometheus.Gatherers{
116+
prometheus.DefaultGatherer,
117+
registry,
118+
}
120119
// 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{})
122121
h.ServeHTTP(w, r)
123122
}
124123
}
10.4 KB
Binary file not shown.
9 KB
Binary file not shown.

0 commit comments

Comments
 (0)