/
metrics-exporter.go
64 lines (55 loc) · 1.98 KB
/
metrics-exporter.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
package watchtower
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rocket-pool/smartnode/rocketpool/watchtower/collectors"
"github.com/rocket-pool/smartnode/shared/services"
"github.com/rocket-pool/smartnode/shared/utils/log"
"github.com/urfave/cli"
)
func runMetricsServer(c *cli.Context, logger log.ColorLogger, scrubCollector *collectors.ScrubCollector, bondReductionCollector *collectors.BondReductionCollector, soloMigrationCollector *collectors.SoloMigrationCollector) error {
// Get services
cfg, err := services.GetConfig(c)
if err != nil {
return err
}
// Return if metrics are disabled
if cfg.EnableMetrics.Value == false {
if strings.ToLower(os.Getenv("ENABLE_METRICS")) == "true" {
logger.Printlnf("ENABLE_METRICS override set to true, will start Metrics exporter anyway!")
} else {
return nil
}
}
// Set up Prometheus
registry := prometheus.NewRegistry()
registry.MustRegister(scrubCollector)
registry.MustRegister(bondReductionCollector)
registry.MustRegister(soloMigrationCollector)
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
// Start the HTTP server
metricsAddress := c.GlobalString("metricsAddress")
metricsPort := c.GlobalUint("metricsPort")
logger.Printlnf("Starting metrics exporter on %s:%d.", metricsAddress, metricsPort)
metricsPath := "/metrics"
http.Handle(metricsPath, handler)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Rocket Pool Watchtower Metrics Exporter</title></head>
<body>
<h1>Rocket Pool Watchtower Metrics Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>`,
))
})
err = http.ListenAndServe(fmt.Sprintf("%s:%d", metricsAddress, metricsPort), nil)
if err != nil {
return fmt.Errorf("Error running HTTP server: %w", err)
}
return nil
}