pangolin_exporter
120 строк · 2.9 Кб
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
14package config
15
16import (
17"fmt"
18"os"
19"sync"
20
21"github.com/go-kit/log"
22"github.com/prometheus/client_golang/prometheus"
23"github.com/prometheus/client_golang/prometheus/promauto"
24"gopkg.in/yaml.v3"
25)
26
27var (
28configReloadSuccess = promauto.NewGauge(prometheus.GaugeOpts{
29Namespace: "postgres_exporter",
30Name: "config_last_reload_successful",
31Help: "Postgres exporter config loaded successfully.",
32})
33
34configReloadSeconds = promauto.NewGauge(prometheus.GaugeOpts{
35Namespace: "postgres_exporter",
36Name: "config_last_reload_success_timestamp_seconds",
37Help: "Timestamp of the last successful configuration reload.",
38})
39)
40
41type Config struct {
42AuthModules map[string]AuthModule `yaml:"auth_modules"`
43}
44
45type AuthModule struct {
46Type string `yaml:"type"`
47UserPass UserPass `yaml:"userpass,omitempty"`
48// Add alternative auth modules here
49Options map[string]string `yaml:"options"`
50}
51
52type UserPass struct {
53Username string `yaml:"username"`
54Password string `yaml:"password"`
55}
56
57type Handler struct {
58sync.RWMutex
59Config *Config
60}
61
62func (ch *Handler) GetConfig() *Config {
63ch.RLock()
64defer ch.RUnlock()
65return ch.Config
66}
67
68func (ch *Handler) ReloadConfig(f string, logger log.Logger) error {
69config := &Config{}
70var err error
71defer func() {
72if err != nil {
73configReloadSuccess.Set(0)
74} else {
75configReloadSuccess.Set(1)
76configReloadSeconds.SetToCurrentTime()
77}
78}()
79
80yamlReader, err := os.Open(f)
81if err != nil {
82return fmt.Errorf("Error opening config file %q: %s", f, err)
83}
84defer yamlReader.Close()
85decoder := yaml.NewDecoder(yamlReader)
86decoder.KnownFields(true)
87
88if err = decoder.Decode(config); err != nil {
89return fmt.Errorf("Error parsing config file %q: %s", f, err)
90}
91
92ch.Lock()
93ch.Config = config
94ch.Unlock()
95return nil
96}
97
98func (m AuthModule) ConfigureTarget(target string) (DSN, error) {
99dsn, err := dsnFromString(target)
100if err != nil {
101return DSN{}, err
102}
103
104// Set the credentials from the authentication module
105// TODO(@sysadmind): What should the order of precedence be?
106if m.Type == "userpass" {
107if m.UserPass.Username != "" {
108dsn.username = m.UserPass.Username
109}
110if m.UserPass.Password != "" {
111dsn.password = m.UserPass.Password
112}
113}
114
115for k, v := range m.Options {
116dsn.query.Set(k, v)
117}
118
119return dsn, nil
120}
121