Skip to content

Commit

Permalink
implement UNIX domain socket support for multi-target scraping
Browse files Browse the repository at this point in the history
Signed-off-by: Kohei YOSHIDA <kohei@yosida95.com>
  • Loading branch information
yosida95 committed Mar 7, 2023
1 parent 8fa4a07 commit bf70e5b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
18 changes: 13 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ func (m MySqlConfig) validateConfig() error {
return nil
}

func (m MySqlConfig) FormDSN(target string) (string, error) {
func (m MySqlConfig) FormDSN(proto, target string) (string, error) {
config := mysql.NewConfig()
config.User = m.User
config.Passwd = m.Password
config.Net = "tcp"
if target == "" {
if proto == "" || target == "" {
if m.Socket == "" {
host := "127.0.0.1"
if m.Host != "" {
Expand All @@ -191,10 +191,18 @@ func (m MySqlConfig) FormDSN(target string) (string, error) {
config.Addr = m.Socket
}
} else {
if _, _, err = net.SplitHostPort(target); err != nil {
return "", fmt.Errorf("failed to parse target: %s", err)
switch proto {
case "tcp":
if _, _, err = net.SplitHostPort(target); err != nil {
return "", fmt.Errorf("failed to parse target: %s", err)
}
config.Addr = target
case "unix":
config.Net = proto
config.Addr = target
default:
return "", fmt.Errorf("unrecognized target protocol: %q", proto)
}
config.Addr = target
}

if m.SslCa != "" {
Expand Down
16 changes: 12 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,19 +154,27 @@ func TestFormDSN(t *testing.T) {
}
convey.Convey("Default Client", func() {
cfg := c.GetConfig()
section, _ := cfg.Sections["client"]
if dsn, err = section.FormDSN(""); err != nil {
section := cfg.Sections["client"]
if dsn, err = section.FormDSN("", ""); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "root:abc@tcp(server2:3306)/")
})
convey.Convey("Target specific with explicit port", func() {
cfg := c.GetConfig()
section, _ := cfg.Sections["client.server1"]
if dsn, err = section.FormDSN("server1:5000"); err != nil {
section := cfg.Sections["client.server1"]
if dsn, err = section.FormDSN("tcp", "server1:5000"); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "test:foo@tcp(server1:5000)/")
})
convey.Convey("UNIX domain socket", func() {
cfg := c.GetConfig()
section := cfg.Sections["client.server1"]
if dsn, err = section.FormDSN("unix", "/run/mysqld/mysqld.sock"); err != nil {
t.Error(err)
}
convey.So(dsn, convey.ShouldEqual, "test:foo@unix(/run/mysqld/mysqld.sock)/")
})
})
}
2 changes: 1 addition & 1 deletion mysqld_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func newHandler(metrics collector.Metrics, scrapers []collector.Scraper, logger
if !ok {
level.Error(logger).Log("msg", "Failed to parse section [client] from config file", "err", err)
}
if dsn, err = cfgsection.FormDSN(""); err != nil {
if dsn, err = cfgsection.FormDSN("", ""); err != nil {
level.Error(logger).Log("msg", "Failed to form dsn from section [client]", "err", err)
}

Expand Down
6 changes: 5 additions & 1 deletion probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ func handleProbe(metrics collector.Metrics, scrapers []collector.Scraper, logger
http.Error(w, "target is required", http.StatusBadRequest)
return
}
proto := params.Get("protocol")
if proto == "" {
proto = "tcp"
}
collectParams := r.URL.Query()["collect[]"]

if authModule = params.Get("auth_module"); authModule == "" {
Expand All @@ -48,7 +52,7 @@ func handleProbe(metrics collector.Metrics, scrapers []collector.Scraper, logger
level.Error(logger).Log("msg", fmt.Sprintf("Failed to parse section [%s] from config file", authModule), "err", err)
http.Error(w, fmt.Sprintf("Error parsing config section [%s]", authModule), http.StatusBadRequest)
}
if dsn, err = cfgsection.FormDSN(target); err != nil {
if dsn, err = cfgsection.FormDSN(proto, target); err != nil {
level.Error(logger).Log("msg", fmt.Sprintf("Failed to form dsn from section [%s]", authModule), "err", err)
http.Error(w, fmt.Sprintf("Error forming dsn from config section [%s]", authModule), http.StatusBadRequest)
}
Expand Down

0 comments on commit bf70e5b

Please sign in to comment.