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
  • Loading branch information
yosida95 committed Feb 10, 2023
1 parent 530e351 commit eaec7cf
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
15 changes: 11 additions & 4 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (m MySqlConfig) validateConfig() error {
return nil
}

func (m MySqlConfig) FormDSN(target string) (string, error) {
func (m MySqlConfig) FormDSN(proto, target string) (string, error) {
var dsn, host, port string

user := m.User
Expand All @@ -184,10 +184,17 @@ func (m MySqlConfig) FormDSN(target string) (string, error) {
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/", user, password, host, port)
}
} else {
if host, port, err = net.SplitHostPort(target); err != nil {
return dsn, fmt.Errorf("failed to parse target: %s", err)
switch proto {
case "tcp":
if host, port, err = net.SplitHostPort(target); err != nil {
return dsn, fmt.Errorf("failed to parse target: %s", err)
}
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", user, password, host, port)
case "unix":
dsn = fmt.Sprintf("%s:%s@unix(%s)/", user, password, target)
default:
return dsn, fmt.Errorf("unrecognized target protocol: %q", proto)
}
dsn = fmt.Sprintf("%s:%s@tcp(%s:%s)/", user, password, host, port)
}

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 @@ -138,7 +138,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 eaec7cf

Please sign in to comment.