|
| 1 | +// Copyright 2022 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package config |
| 15 | + |
| 16 | +import ( |
| 17 | + "fmt" |
| 18 | + "net/url" |
| 19 | + "os" |
| 20 | + "strings" |
| 21 | + "sync" |
| 22 | + |
| 23 | + "github.com/go-kit/log" |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | + "gopkg.in/yaml.v3" |
| 26 | +) |
| 27 | + |
| 28 | +var ( |
| 29 | + configReloadSuccess = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 30 | + Namespace: "postgres_exporter", |
| 31 | + Name: "config_last_reload_successful", |
| 32 | + Help: "Postgres exporter config loaded successfully.", |
| 33 | + }) |
| 34 | + |
| 35 | + configReloadSeconds = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 36 | + Namespace: "postgres_exporter", |
| 37 | + Name: "config_last_reload_success_timestamp_seconds", |
| 38 | + Help: "Timestamp of the last successful configuration reload.", |
| 39 | + }) |
| 40 | +) |
| 41 | + |
| 42 | +func init() { |
| 43 | + prometheus.MustRegister(configReloadSuccess) |
| 44 | + prometheus.MustRegister(configReloadSeconds) |
| 45 | +} |
| 46 | + |
| 47 | +type Config struct { |
| 48 | + AuthModules map[string]AuthModule `yaml:"auth_modules"` |
| 49 | +} |
| 50 | + |
| 51 | +type AuthModule struct { |
| 52 | + Type string `yaml:"type"` |
| 53 | + UserPass UserPass `yaml:"userpass,omitempty"` |
| 54 | + // Add alternative auth modules here |
| 55 | + Options map[string]string `yaml:"options"` |
| 56 | +} |
| 57 | + |
| 58 | +type UserPass struct { |
| 59 | + Username string `yaml:"username"` |
| 60 | + Password string `yaml:"password"` |
| 61 | +} |
| 62 | + |
| 63 | +type ConfigHandler struct { |
| 64 | + sync.RWMutex |
| 65 | + Config *Config |
| 66 | +} |
| 67 | + |
| 68 | +func (ch *ConfigHandler) GetConfig() *Config { |
| 69 | + ch.RLock() |
| 70 | + defer ch.RUnlock() |
| 71 | + return ch.Config |
| 72 | +} |
| 73 | + |
| 74 | +func (ch *ConfigHandler) ReloadConfig(f string, logger log.Logger) error { |
| 75 | + config := &Config{} |
| 76 | + var err error |
| 77 | + defer func() { |
| 78 | + if err != nil { |
| 79 | + configReloadSuccess.Set(0) |
| 80 | + } else { |
| 81 | + configReloadSuccess.Set(1) |
| 82 | + configReloadSeconds.SetToCurrentTime() |
| 83 | + } |
| 84 | + }() |
| 85 | + |
| 86 | + yamlReader, err := os.Open(f) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("Error opening config file %q: %s", f, err) |
| 89 | + } |
| 90 | + defer yamlReader.Close() |
| 91 | + decoder := yaml.NewDecoder(yamlReader) |
| 92 | + decoder.KnownFields(true) |
| 93 | + |
| 94 | + if err = decoder.Decode(config); err != nil { |
| 95 | + return fmt.Errorf("Error parsing config file %q: %s", f, err) |
| 96 | + } |
| 97 | + |
| 98 | + ch.Lock() |
| 99 | + ch.Config = config |
| 100 | + ch.Unlock() |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func (m AuthModule) ConfigureTarget(target string) (string, error) { |
| 105 | + // ip:port urls do not parse properly and that is the typical way users interact with postgres |
| 106 | + t := fmt.Sprintf("exporter://%s", target) |
| 107 | + u, err := url.Parse(t) |
| 108 | + if err != nil { |
| 109 | + return "", err |
| 110 | + } |
| 111 | + |
| 112 | + if m.Type == "userpass" { |
| 113 | + u.User = url.UserPassword(m.UserPass.Username, m.UserPass.Password) |
| 114 | + } |
| 115 | + |
| 116 | + query := u.Query() |
| 117 | + for k, v := range m.Options { |
| 118 | + query.Set(k, v) |
| 119 | + } |
| 120 | + u.RawQuery = query.Encode() |
| 121 | + |
| 122 | + parsed := u.String() |
| 123 | + trim := strings.TrimPrefix(parsed, "exporter://") |
| 124 | + |
| 125 | + return trim, nil |
| 126 | +} |
0 commit comments