-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
70 lines (57 loc) · 1.33 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
package main
import (
_ "embed"
"encoding/json"
"errors"
"html/template"
"os"
"strings"
)
//go:embed config.yml.tpl
var configYmlTpl string
type Config struct {
OTLPHost string
OTLPHTTPPort int
OTLPGRPCPort int
EnableZipkin bool
EnableProm bool
FromJSONFile string
PromTarget []string
}
func (c *Config) RenderYml() (string, error) {
tpl, err := template.New("config").Parse(configYmlTpl)
if err != nil {
return "", err
}
params, err := structToMap(c)
if err != nil {
return "", err
}
var buf strings.Builder
if err := tpl.Execute(&buf, params); err != nil {
return "", err
}
return buf.String(), nil
}
func structToMap(s interface{}) (map[string]any, error) {
var result map[string]any
jsonData, err := json.Marshal(s)
if err != nil {
return nil, err
}
err = json.Unmarshal(jsonData, &result)
if err != nil {
return nil, err
}
return result, nil
}
// Validate checks if the otel-tui configuration is valid
func (cfg *Config) Validate() error {
if cfg.EnableProm && len(cfg.PromTarget) == 0 {
return errors.New("the target endpoints for the prometheus receiver (--prom-target) must be specified when prometheus receiver enabled")
}
if _, err := os.Stat(cfg.FromJSONFile); len(cfg.FromJSONFile) > 0 && err != nil {
return errors.New("the initial data JSON file does not exist")
}
return nil
}