pangolin_exporter

Форк
0
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

14
package config
15

16
import (
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

27
var (
28
	configReloadSuccess = promauto.NewGauge(prometheus.GaugeOpts{
29
		Namespace: "postgres_exporter",
30
		Name:      "config_last_reload_successful",
31
		Help:      "Postgres exporter config loaded successfully.",
32
	})
33

34
	configReloadSeconds = promauto.NewGauge(prometheus.GaugeOpts{
35
		Namespace: "postgres_exporter",
36
		Name:      "config_last_reload_success_timestamp_seconds",
37
		Help:      "Timestamp of the last successful configuration reload.",
38
	})
39
)
40

41
type Config struct {
42
	AuthModules map[string]AuthModule `yaml:"auth_modules"`
43
}
44

45
type AuthModule struct {
46
	Type     string   `yaml:"type"`
47
	UserPass UserPass `yaml:"userpass,omitempty"`
48
	// Add alternative auth modules here
49
	Options map[string]string `yaml:"options"`
50
}
51

52
type UserPass struct {
53
	Username string `yaml:"username"`
54
	Password string `yaml:"password"`
55
}
56

57
type Handler struct {
58
	sync.RWMutex
59
	Config *Config
60
}
61

62
func (ch *Handler) GetConfig() *Config {
63
	ch.RLock()
64
	defer ch.RUnlock()
65
	return ch.Config
66
}
67

68
func (ch *Handler) ReloadConfig(f string, logger log.Logger) error {
69
	config := &Config{}
70
	var err error
71
	defer func() {
72
		if err != nil {
73
			configReloadSuccess.Set(0)
74
		} else {
75
			configReloadSuccess.Set(1)
76
			configReloadSeconds.SetToCurrentTime()
77
		}
78
	}()
79

80
	yamlReader, err := os.Open(f)
81
	if err != nil {
82
		return fmt.Errorf("Error opening config file %q: %s", f, err)
83
	}
84
	defer yamlReader.Close()
85
	decoder := yaml.NewDecoder(yamlReader)
86
	decoder.KnownFields(true)
87

88
	if err = decoder.Decode(config); err != nil {
89
		return fmt.Errorf("Error parsing config file %q: %s", f, err)
90
	}
91

92
	ch.Lock()
93
	ch.Config = config
94
	ch.Unlock()
95
	return nil
96
}
97

98
func (m AuthModule) ConfigureTarget(target string) (DSN, error) {
99
	dsn, err := dsnFromString(target)
100
	if err != nil {
101
		return DSN{}, err
102
	}
103

104
	// Set the credentials from the authentication module
105
	// TODO(@sysadmind): What should the order of precedence be?
106
	if m.Type == "userpass" {
107
		if m.UserPass.Username != "" {
108
			dsn.username = m.UserPass.Username
109
		}
110
		if m.UserPass.Password != "" {
111
			dsn.password = m.UserPass.Password
112
		}
113
	}
114

115
	for k, v := range m.Options {
116
		dsn.query.Set(k, v)
117
	}
118

119
	return dsn, nil
120
}
121

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.