Update multi-target handler to use new DSN type

- Moves new dsn type to config.DSN. This will prevent circular dependencies.
- Change DSN.query to be url.Values. This allows the multi-target functionality to merge values without re-parsing the query string
- Change NewProbeCollector to use the new config.DSN type
- Add DSN.GetConnectionString to return a string formatted for the sql driver to use during connection

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams
2022-09-02 10:32:44 -04:00
parent ac9fa13302
commit 7ffba684de
5 changed files with 268 additions and 235 deletions

View File

@@ -15,9 +15,7 @@ package config
import (
"fmt"
"net/url"
"os"
"strings"
"sync"
"github.com/go-kit/log"
@@ -97,26 +95,26 @@ func (ch *ConfigHandler) ReloadConfig(f string, logger log.Logger) error {
return nil
}
func (m AuthModule) ConfigureTarget(target string) (string, error) {
// ip:port urls do not parse properly and that is the typical way users interact with postgres
t := fmt.Sprintf("exporter://%s", target)
u, err := url.Parse(t)
func (m AuthModule) ConfigureTarget(target string) (DSN, error) {
dsn, err := dsnFromString(target)
if err != nil {
return "", err
return DSN{}, err
}
// Set the credentials from the authentication module
// TODO(@sysadmind): What should the order of precedence be?
if m.Type == "userpass" {
u.User = url.UserPassword(m.UserPass.Username, m.UserPass.Password)
if m.UserPass.Username != "" {
dsn.username = m.UserPass.Username
}
if m.UserPass.Password != "" {
dsn.password = m.UserPass.Password
}
}
query := u.Query()
for k, v := range m.Options {
query.Set(k, v)
dsn.query.Set(k, v)
}
u.RawQuery = query.Encode()
parsed := u.String()
trim := strings.TrimPrefix(parsed, "exporter://")
return trim, nil
return dsn, nil
}