fix: resolve golangci-lint issues
This commit is contained in:
parent
5c2f5c13d6
commit
059a6aea86
|
@ -14,6 +14,7 @@
|
||||||
"labtime",
|
"labtime",
|
||||||
"nonroot",
|
"nonroot",
|
||||||
"promhttp",
|
"promhttp",
|
||||||
|
"sigchan",
|
||||||
"woodpeckerci"
|
"woodpeckerci"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -9,7 +9,7 @@ indent_size = 4
|
||||||
end_of_line = lf
|
end_of_line = lf
|
||||||
charset = utf-8
|
charset = utf-8
|
||||||
trim_trailing_whitespace = false
|
trim_trailing_whitespace = false
|
||||||
insert_final_newline = false
|
insert_final_newline = true
|
||||||
|
|
||||||
[*.{json,yaml}]
|
[*.{json,yaml}]
|
||||||
indent_size = 2
|
indent_size = 2
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
---
|
||||||
|
run:
|
||||||
|
timeout: 5m
|
||||||
|
# this settings is for the CI pipeline because it fails with the default value
|
||||||
|
# we should have used the --timeout flag in the CI pipeline
|
||||||
|
# but to make the linter work locally, I need to have a config file
|
||||||
|
# so I pushed this config file until we have something to do here
|
|
@ -0,0 +1,11 @@
|
||||||
|
{
|
||||||
|
"[go]": {
|
||||||
|
"editor.codeActionsOnSave": {
|
||||||
|
// "source.organizeImports": "always"
|
||||||
|
},
|
||||||
|
"editor.formatOnSave": true
|
||||||
|
},
|
||||||
|
"go.formatTool": "goimports",
|
||||||
|
"go.lintTool": "golangci-lint"
|
||||||
|
// "go.lintFlags": ["--fast"] // We only have the default linters and it seems they are not marked as fast, but it's fast enough for me
|
||||||
|
}
|
40
main.go
40
main.go
|
@ -21,7 +21,8 @@ type Config struct {
|
||||||
Targets []struct {
|
Targets []struct {
|
||||||
// Name of the target. Used to identify the target from Prometheus.
|
// Name of the target. Used to identify the target from Prometheus.
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
// URL of the target. The target should be accessible from the machine running the exporter. The URL should contain the protocol (http:// or https://) and the port if it's not the default one.
|
// URL of the target. The target should be accessible from the machine running the exporter.
|
||||||
|
// The URL should contain the protocol (http:// or https://) and the port if it's not the default one.
|
||||||
Url string `yaml:"url"`
|
Url string `yaml:"url"`
|
||||||
// Interval to ping the target. Default is 5 seconds
|
// Interval to ping the target. Default is 5 seconds
|
||||||
Interval int `yaml:"interval,omitempty"`
|
Interval int `yaml:"interval,omitempty"`
|
||||||
|
@ -51,11 +52,13 @@ func NewConfig(configPath string) (*Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Ping(url string) (int, time.Duration, error) {
|
func Ping(url string) (int, time.Duration, error) {
|
||||||
req, err := http.NewRequest("HEAD", url, nil)
|
req, err := http.NewRequest(http.MethodHead, url, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var start, connect, dns, tlsHandshake time.Time
|
var start, connect, dns, tlsHandshake time.Time
|
||||||
|
|
||||||
trace := &httptrace.ClientTrace{
|
trace := &httptrace.ClientTrace{
|
||||||
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
|
DNSStart: func(dsi httptrace.DNSStartInfo) { dns = time.Now() },
|
||||||
DNSDone: func(ddi httptrace.DNSDoneInfo) {
|
DNSDone: func(ddi httptrace.DNSDoneInfo) {
|
||||||
|
@ -76,20 +79,23 @@ func Ping(url string) (int, time.Duration, error) {
|
||||||
fmt.Printf("Time from start to first byte: %v\n", time.Since(start))
|
fmt.Printf("Time from start to first byte: %v\n", time.Since(start))
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
|
||||||
start = time.Now()
|
start = time.Now()
|
||||||
|
|
||||||
resp, err := http.DefaultTransport.RoundTrip(req)
|
resp, err := http.DefaultTransport.RoundTrip(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
resp.Body.Close()
|
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
return resp.StatusCode, time.Since(start), nil
|
return resp.StatusCode, time.Since(start), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Load config
|
// Load config
|
||||||
// TODO: Use a flag to specify the config file
|
// TODO: Use a flag to specify the config file
|
||||||
config, err := NewConfig("config/config.yaml")
|
config, err := NewConfig("config/config.yaml")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error loading config: %s", err)
|
log.Fatalf("Error loading config: %s", err)
|
||||||
|
@ -100,7 +106,7 @@ func main() {
|
||||||
// create a scheduler
|
// create a scheduler
|
||||||
s, err := gocron.NewScheduler()
|
s, err := gocron.NewScheduler()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
log.Fatalf("Error creating the scheduler: %s",err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prometheus metrics
|
// Prometheus metrics
|
||||||
|
@ -112,7 +118,7 @@ func main() {
|
||||||
|
|
||||||
// Intercept the signal to stop the program
|
// Intercept the signal to stop the program
|
||||||
go func() {
|
go func() {
|
||||||
sigchan := make(chan os.Signal)
|
sigchan := make(chan os.Signal, 1)
|
||||||
signal.Notify(sigchan, os.Interrupt)
|
signal.Notify(sigchan, os.Interrupt)
|
||||||
<-sigchan
|
<-sigchan
|
||||||
log.Println("Program killed !")
|
log.Println("Program killed !")
|
||||||
|
@ -122,7 +128,7 @@ func main() {
|
||||||
// when you're done, shut it down
|
// when you're done, shut it down
|
||||||
err = s.Shutdown()
|
err = s.Shutdown()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
log.Fatalf("Error shutting down scheduler: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
@ -134,8 +140,11 @@ func main() {
|
||||||
gocron.DurationJob(
|
gocron.DurationJob(
|
||||||
func(interval int) time.Duration {
|
func(interval int) time.Duration {
|
||||||
if interval == 0 {
|
if interval == 0 {
|
||||||
return time.Duration(5) * time.Second
|
const defaultInterval = 5
|
||||||
|
|
||||||
|
return time.Duration(defaultInterval) * time.Second
|
||||||
}
|
}
|
||||||
|
|
||||||
return time.Duration(interval) * time.Second
|
return time.Duration(interval) * time.Second
|
||||||
}(target.Interval),
|
}(target.Interval),
|
||||||
),
|
),
|
||||||
|
@ -144,8 +153,10 @@ func main() {
|
||||||
status, elapsedTime, err := Ping(url)
|
status, elapsedTime, err := Ping(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("%s - Status: %d in %v\n", target.Name, status, elapsedTime.Seconds())
|
fmt.Printf("%s - Status: %d in %v\n", target.Name, status, elapsedTime.Seconds())
|
||||||
// push to Prometheus
|
// push to Prometheus
|
||||||
responseTimeMonitor.With(prometheus.Labels{"target_name": target.Name}).Set(elapsedTime.Seconds())
|
responseTimeMonitor.With(prometheus.Labels{"target_name": target.Name}).Set(elapsedTime.Seconds())
|
||||||
|
@ -154,7 +165,7 @@ func main() {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// handle error
|
log.Fatalf("Error creating job: %s", err)
|
||||||
}
|
}
|
||||||
// each job has a unique id
|
// each job has a unique id
|
||||||
fmt.Printf("Job %s started with ID: %s\n", target.Name, j.ID().String())
|
fmt.Printf("Job %s started with ID: %s\n", target.Name, j.ID().String())
|
||||||
|
@ -163,12 +174,9 @@ func main() {
|
||||||
// start the scheduler
|
// start the scheduler
|
||||||
s.Start()
|
s.Start()
|
||||||
|
|
||||||
// // block until you are ready to shut down
|
|
||||||
// select {
|
|
||||||
// // case <-time.After(time.Minute):
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Serve Prometheus metrics
|
// Serve Prometheus metrics
|
||||||
http.Handle("/metrics", promhttp.Handler())
|
http.Handle("/metrics", promhttp.Handler())
|
||||||
http.ListenAndServe(":2112", nil)
|
if err := http.ListenAndServe(":2112", nil); err != nil {
|
||||||
|
log.Fatalf("Error starting server: %s", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue