-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
79 lines (65 loc) · 1.76 KB
/
config.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package pgopher
import (
"fmt"
"os"
"time"
"gopkg.in/yaml.v3"
)
type Config struct {
ListenAddress string `yaml:"listenAddress"`
PprofListenAddress string `yaml:"pprofListenAddress"`
ProfilingTargets []ProfilingTarget `yaml:"profilingTargets"`
Sink Sink `yaml:"sink"`
}
type ProfilingTarget struct {
Name string `yaml:"name"`
URL string `yaml:"url"`
Duration time.Duration `yaml:"duration"`
Schedule string `yaml:"schedule"`
}
type Sink struct {
Type string `yaml:"type"`
FileSinkOptions FileSinkOptions `yaml:"fileSinkOptions"`
S3SinkOptions S3SinkOptions `yaml:"s3SinkOptions"`
KubernetesSinkOptions KubernetesSinkOptions `yaml:"kubernetesSinkOptions"`
}
type FileSinkOptions struct {
Folder string `yaml:"folder"`
}
type S3SinkOptions struct {
Bucket string `yaml:"bucket"`
}
type KubernetesSinkOptions struct {
APIServerURL string `yaml:"apiServerURL"`
Namespace string `yaml:"namespace"`
}
func DefaultConfig() Config {
return Config{
ListenAddress: ":8000",
PprofListenAddress: "localhost:8010",
ProfilingTargets: make([]ProfilingTarget, 0),
Sink: Sink{
Type: "file",
FileSinkOptions: FileSinkOptions{
Folder: "profiles",
},
KubernetesSinkOptions: KubernetesSinkOptions{
Namespace: "pgopher",
},
},
}
}
func LoadConfig(path string) (Config, error) {
cfg := DefaultConfig()
file, err := os.Open(path)
if err != nil {
return cfg, fmt.Errorf("failed to open config file: %w", err)
}
decoder := yaml.NewDecoder(file)
decoder.KnownFields(true)
err = decoder.Decode(&cfg)
if err != nil {
return cfg, fmt.Errorf("failed to decode config: %w", err)
}
return cfg, nil
}