This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.go
118 lines (102 loc) · 2.67 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
117
118
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
fqdn "github.com/Showmax/go-fqdn"
"github.com/Showmax/sockrus"
"github.com/sirupsen/logrus"
)
// global vars
//
var (
cfgPath string
dummy bool
verbose uint
hostname string
httpClientTimeout time.Duration
logger *logrus.Entry
defaultConfPath = "/etc/prometheus-pusher/conf.d"
defaultLogSocket = "/run/showmax/socket_to_amqp.sock"
serviceName = "prometheus-pusher"
version string
versionFlag bool
printMutex = &sync.Mutex{}
)
func init() {
// parse arguments
flag.StringVar(&cfgPath, "config", defaultConfPath,
"Config file or directory. If directory is specified then all "+
"files in the directory will be loaded.")
flag.BoolVar(&dummy, "dummy", false,
"Do not post the metrics, just print them to stdout")
flag.UintVar(&verbose, "verbosity", 1, "Set logging verbosity.")
flag.DurationVar(&httpClientTimeout, "http-timeout", 30*time.Second, "Timeout for HTTP requests")
flag.BoolVar(&versionFlag, "version", false, "Print version and exit")
flag.Parse()
if versionFlag {
fmt.Println(version)
os.Exit(0)
}
// set log level
var logLevel logrus.Level
switch verbose {
case 0:
logLevel = logrus.ErrorLevel
case 1:
logLevel = logrus.InfoLevel
default:
logLevel = logrus.DebugLevel
}
hostname = fqdn.Get()
// create logger instance
_, logger = sockrus.NewSockrus(sockrus.Config{
LogLevel: logLevel,
Service: serviceName,
SocketAddr: defaultLogSocket,
SocketProtocol: "unix",
})
}
func main() {
logger.Info("Starting prometheus-pusher")
// read config files
cfgData, err := concatConfigFiles(cfgPath)
if err != nil {
logger.Fatalf("Failed to read config files - %s", err.Error())
}
// parse config data
pusherCfg, err := parseConfig(cfgData)
if err != nil {
logger.Fatalf("Failed to parse config data - %s", err.Error())
}
// prepare global route map if there is any
var globalRouteMap *routeMap
if pusherCfg.defaultRoute != "" && pusherCfg.routeMap != "" {
globalRouteMap = newRouteMap(pusherCfg.routeMap, pusherCfg.defaultRoute)
}
// spawn resources
resources := createResources(pusherCfg, globalRouteMap)
// handle signals for clean shutdown
signal.Notify(resources.sig, syscall.SIGINT, syscall.SIGTERM)
go func() {
for s := range resources.sig {
logger.Infof("Received %s signal, will shut down", s)
resources.shutdown()
return
}
}()
resources.process(pusherCfg)
for {
select {
case <-resources.run():
resources.process(pusherCfg)
case <-resources.stop():
logger.Info("Resources processing stopped")
os.Exit(0)
}
}
}