Welcome to mirror list, hosted at ThFree Co, Russian Federation.

config.go « blackbox « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 064b7788b1ccf21dcea520bc913293e95cd99f73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package blackbox

import (
	"fmt"
	"net/url"
	"time"

	"github.com/pelletier/go-toml"
	logconfig "gitlab.com/gitlab-org/gitaly/v14/internal/gitaly/config/log"
)

type Config struct {
	PrometheusListenAddr string `toml:"prometheus_listen_addr"`
	Sleep                int    `toml:"sleep"`
	SleepDuration        time.Duration
	Logging              logconfig.Config `toml:"logging"`
	Probes               []Probe          `toml:"probe"`
}

type Probe struct {
	Name     string `toml:"name"`
	URL      string `toml:"url"`
	User     string `toml:"user"`
	Password string `toml:"password"`
}

func ParseConfig(raw string) (*Config, error) {
	config := &Config{}
	if err := toml.Unmarshal([]byte(raw), config); err != nil {
		return nil, err
	}

	if config.PrometheusListenAddr == "" {
		return nil, fmt.Errorf("missing prometheus_listen_addr")
	}

	if config.Sleep < 0 {
		return nil, fmt.Errorf("sleep time is less than 0")
	}
	if config.Sleep == 0 {
		config.Sleep = 15 * 60
	}
	config.SleepDuration = time.Duration(config.Sleep) * time.Second

	if len(config.Probes) == 0 {
		return nil, fmt.Errorf("must define at least one probe")
	}

	for _, probe := range config.Probes {
		if len(probe.Name) == 0 {
			return nil, fmt.Errorf("all probes must have a 'name' attribute")
		}

		parsedURL, err := url.Parse(probe.URL)
		if err != nil {
			return nil, err
		}

		if s := parsedURL.Scheme; s != "http" && s != "https" {
			return nil, fmt.Errorf("unsupported probe URL scheme: %v", probe.URL)
		}
	}

	return config, nil
}