-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
116 lines (102 loc) · 2.65 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"
agent "github.com/karimra/srl-ndk-demo"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"google.golang.org/grpc/metadata"
"gopkg.in/yaml.v2"
"github.com/karimra/srl-prometheus-exporter/app"
)
const (
retryInterval = 2 * time.Second
maxRetries = 50
agentName = "prometheus-exporter"
defaultConfigFileName = "/opt/prometheus-exporter/metrics.yaml"
)
var version = "dev"
// flags
var debug bool
var cfgFile string
var versionFlag bool
func main() {
pflag.StringVarP(&cfgFile, "config", "c", defaultConfigFileName, "configuration file")
pflag.BoolVarP(&debug, "debug", "d", false, "turn on debug")
pflag.BoolVarP(&versionFlag, "version", "v", false, "print version")
pflag.Parse()
if versionFlag {
fmt.Println(version)
return
}
if debug {
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}
retryCount := 0
READFILE:
var fc = new(app.FileConfig)
_, err := os.Stat(cfgFile)
if err == nil {
b, err := os.ReadFile(cfgFile)
if err != nil {
if retryCount >= maxRetries {
log.Errorf("failed to read file: max retries reached: %v", err)
os.Exit(1)
}
retryCount++
log.Errorf("failed to read the configuration file: %v", err)
time.Sleep(retryInterval)
goto READFILE
}
err = yaml.Unmarshal(b, fc)
if err != nil {
if retryCount >= maxRetries {
log.Errorf("failed to read file: max retries reached: %v", err)
os.Exit(1)
}
log.Errorf("failed to unmarshal the configuration file: %v", err)
time.Sleep(retryInterval)
goto READFILE
}
}
ctx, cancel := context.WithCancel(context.Background())
setupCloseHandler(cancel)
ctx = metadata.AppendToOutgoingContext(ctx, "agent_name", agentName)
retryCount = 0
CRAGENT:
agt, err := agent.New(ctx, agentName)
if err != nil {
if retryCount >= maxRetries {
log.Errorf("ailed to create agent: max retries reached: %v", err)
os.Exit(1)
}
log.Errorf("failed to create agent %q: %v", agentName, err)
log.Infof("retrying in %s", retryInterval)
time.Sleep(retryInterval)
goto CRAGENT
}
cfg := app.NewConfig(fc, agentName, debug)
log.Infof("starting with default configuration: %+v", cfg)
server := app.NewServer(
app.WithAgent(agt),
app.WithConfig(cfg),
)
log.Infof("starting config handler...")
server.ConfigHandler(ctx)
}
func setupCloseHandler(cancelFn context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
go func() {
sig := <-c
log.Printf("received signal '%s'. terminating...", sig.String())
cancelFn()
time.Sleep(500 * time.Millisecond)
os.Exit(0)
}()
}