...

Source file src/edge-infra.dev/pkg/sds/etcd/manager/internal/observability/metrics.go

Documentation: edge-infra.dev/pkg/sds/etcd/manager/internal/observability

     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  	// Variables that are to be registered with prometheus. promauto automactically registers
    15  	// the variable with prometheus. If promauto is not used variable needs to be registered
    16  	// using prometheus.register. The following variable is of type Gauge metric.
    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  // NewServer creates an etcdmanager metrics server that runs on the desired port.
    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  // Run runs the metrics server on the configured port
    58  func (s *Server) Run() error {
    59  	addr := net.JoinHostPort("", strconv.Itoa(s.Port))
    60  	return s.Router.Run(addr)
    61  }
    62  
    63  // Builder Object for Server
    64  type ServerBuilder struct {
    65  	Router *gin.Engine
    66  	Port   int
    67  }
    68  
    69  // Constructor for ServerBuilder
    70  func NewServerBuilder() *ServerBuilder {
    71  	o := new(ServerBuilder)
    72  	return o
    73  }
    74  
    75  // Build Method which creates Server
    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  // WithRouter method sets the Router for the ServerBuilder
    84  func (b *ServerBuilder) WithRouter(router *gin.Engine) *ServerBuilder {
    85  	b.Router = router
    86  	return b
    87  }
    88  
    89  // WithPort method sets the Port for the ServerBuilder
    90  func (b *ServerBuilder) WithPort(port int) *ServerBuilder {
    91  	b.Port = port
    92  	return b
    93  }
    94  

View as plain text