generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
196 lines (174 loc) · 5.17 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/urfave/cli/v2"
"github.com/vshn/billing-collector-cloudservices/pkg/cmd"
"github.com/vshn/billing-collector-cloudservices/pkg/log"
)
var (
// these variables are populated by Goreleaser when releasing
version = "unknown"
commit = "-dirty-"
date = time.Now().Format("2006-01-02")
appName = "billing-collector-cloudservices"
appLongName = "Metrics collector which gathers metrics information for cloud services"
odooFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "billing_cloud_collector_http_requests_odoo_failed_total",
Help: "Total number of failed HTTP requests to Odoo",
})
odooSucceeded = promauto.NewCounter(prometheus.CounterOpts{
Name: "billing_cloud_collector_http_requests_odoo_succeeded_total",
Help: "Total number of successful HTTP requests to Odoo",
})
providerFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "billing_cloud_collector_http_requests_provider_failed_total",
Help: "Total number of failed HTTP requests to the cloud provider",
})
providerSucceeded = promauto.NewCounter(prometheus.CounterOpts{
Name: "billing_cloud_collector_http_requests_provider_succeeded_total",
Help: "Total number of successful HTTP requests to the cloud provider",
})
providerMetrics = map[string]prometheus.Counter{
"providerFailed": providerFailed,
"providerSucceeded": providerSucceeded,
}
odooMetrics = map[string]prometheus.Counter{
"odooFailed": odooFailed,
"odooSucceeded": odooSucceeded,
}
allMetrics = map[string]map[string]prometheus.Counter{
"odooMetrics": odooMetrics,
"providerMetrics": providerMetrics,
}
)
func init() {
// Remove `-v` short option from --version flag
cli.VersionFlag.(*cli.BoolFlag).Aliases = nil
}
func main() {
ctx, stop, app := newApp()
defer stop()
go func(ctx context.Context) {
ctxx, cancel := context.WithCancel(ctx)
defer cancel()
select {
case <-ctxx.Done():
fmt.Println("Shutting down prometheus server")
return
default:
http.Handle("/metrics", promhttp.Handler())
err := http.ListenAndServe(":2112", nil)
if err != nil {
fmt.Println("Error starting prometheus server: ", err.Error())
}
os.Exit(1)
}
}(ctx)
err := app.RunContext(ctx, os.Args)
// If required flags aren't set, it will return with error before we could set up logging
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func newApp() (context.Context, context.CancelFunc, *cli.App) {
var (
logLevel int
logFormat string
)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
app := &cli.App{
Name: appName,
Usage: appLongName,
Version: fmt.Sprintf("%s, revision=%s, date=%s", version, commit, date),
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.IntFlag{
Name: "log-level",
Aliases: []string{"v"},
EnvVars: []string{"LOG_LEVEL"},
Usage: "number of the log level verbosity",
Value: 0,
Destination: &logLevel,
},
&cli.StringFlag{
Name: "log-format",
EnvVars: []string{"LOG_FORMAT"},
Usage: "sets the log format (values: [json, console])",
DefaultText: "console",
Destination: &logFormat,
},
&cli.IntFlag{
Name: "collectInterval",
Usage: "Interval in which the exporter checks the cloud resources",
Value: 10,
},
&cli.IntFlag{
Name: "billingHour",
Usage: "After which hour every day the objectstorage collector should start",
Value: 6,
Action: func(c *cli.Context, i int) error {
if i > 23 || i < 0 {
return fmt.Errorf("invalid billingHour value, needs to be between 0 and 23")
}
return nil
},
},
&cli.StringFlag{
Name: "organizationOverride",
Usage: "If the collector is collecting the metrics for an APPUiO managed instance. It needs to set the name of the customer.",
Value: "",
},
&cli.StringFlag{
Name: "bind",
Usage: "Golang bind string. Will be used for the exporter",
Value: ":9123",
},
},
Before: func(c *cli.Context) error {
logger, err := log.NewLogger(appName, version, logLevel, logFormat)
if err != nil {
return fmt.Errorf("before: %w", err)
}
c.Context = log.NewLoggingContext(c.Context, logger)
log.Logger(c.Context).WithValues(
"date", date,
"commit", commit,
"go_os", runtime.GOOS,
"go_arch", runtime.GOARCH,
"go_version", runtime.Version(),
"uid", os.Getuid(),
"gid", os.Getgid(),
).Info("Starting up " + appName)
return nil
},
Action: func(c *cli.Context) error {
if true {
return cli.ShowAppHelp(c)
}
return nil
},
Commands: []*cli.Command{
cmd.ExoscaleCmds(allMetrics),
cmd.CloudscaleCmds(allMetrics),
cmd.SpksCMD(allMetrics, ctx),
},
ExitErrHandler: func(c *cli.Context, err error) {
if err != nil {
log.Logger(c.Context).Error(err, "fatal error")
cli.HandleExitCoder(cli.Exit("", 1))
}
},
}
return ctx, stop, app
}