...
1 package observability
2
3 import (
4 "net"
5 "strconv"
6
7 "github.com/gin-gonic/gin"
8 "github.com/prometheus/client_golang/prometheus"
9 "github.com/prometheus/client_golang/prometheus/promauto"
10 "github.com/prometheus/client_golang/prometheus/promhttp"
11 )
12
13 var (
14
15
16
17 EtcdClusterHealth = promauto.NewGaugeVec(
18 prometheus.GaugeOpts{
19 Name: "edge_etcd_manager_cluster_health",
20 Help: "Whether the etcd cluster is healthy and has quorum",
21 }, []string{})
22 EtcdAlarmStateCorrupt = promauto.NewGaugeVec(
23 prometheus.GaugeOpts{
24 Name: "edge_etcd_alarm_state_corrupt",
25 Help: "Whether the etcd cluster has a corrupt alarm raised or not",
26 }, []string{})
27 EtcdAlarmStateNospace = promauto.NewGaugeVec(
28 prometheus.GaugeOpts{
29 Name: "edge_etcd_alarm_state_nospace",
30 Help: "Whether the etcd cluster has a nospace alarm raised or not",
31 }, []string{})
32 )
33
34 var metricsEndpoint = "/metrics"
35
36 type Server struct {
37 Router *gin.Engine
38 Port int
39 }
40
41 func init() {
42 gin.SetMode(gin.ReleaseMode)
43 gin.DisableConsoleColor()
44 }
45
46
47 func NewServer(port int) *Server {
48 router := gin.New()
49 server := NewServerBuilder().WithRouter(router).WithPort(port).Build()
50
51 metricsHandlerFunc := gin.WrapH(promhttp.Handler())
52 server.Router.GET(metricsEndpoint, metricsHandlerFunc)
53
54 return server
55 }
56
57
58 func (s *Server) Run() error {
59 addr := net.JoinHostPort("", strconv.Itoa(s.Port))
60 return s.Router.Run(addr)
61 }
62
63
64 type ServerBuilder struct {
65 Router *gin.Engine
66 Port int
67 }
68
69
70 func NewServerBuilder() *ServerBuilder {
71 o := new(ServerBuilder)
72 return o
73 }
74
75
76 func (b *ServerBuilder) Build() *Server {
77 o := new(Server)
78 o.Router = b.Router
79 o.Port = b.Port
80 return o
81 }
82
83
84 func (b *ServerBuilder) WithRouter(router *gin.Engine) *ServerBuilder {
85 b.Router = router
86 return b
87 }
88
89
90 func (b *ServerBuilder) WithPort(port int) *ServerBuilder {
91 b.Port = port
92 return b
93 }
94
View as plain text