2023-12-21 16:04:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Depado/ginprom"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type prometheusService struct {
|
|
|
|
router *gin.Engine
|
|
|
|
prometheus *ginprom.Prometheus
|
|
|
|
config *prometheusConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPrometheusService(r *gin.Engine, c *prometheusConfig) *prometheusService {
|
|
|
|
var p *ginprom.Prometheus
|
|
|
|
|
|
|
|
if c.enabled {
|
|
|
|
if c.secureTokenEnabled {
|
|
|
|
p = ginprom.New(
|
|
|
|
ginprom.Engine(r),
|
|
|
|
ginprom.Namespace(Name),
|
|
|
|
ginprom.Subsystem(""),
|
|
|
|
ginprom.Path(c.path),
|
2023-12-22 12:13:44 +00:00
|
|
|
ginprom.Ignore(c.path),
|
2023-12-21 16:04:04 +00:00
|
|
|
ginprom.Token(c.secureToken),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
p = ginprom.New(
|
|
|
|
ginprom.Engine(r),
|
|
|
|
ginprom.Namespace(Name),
|
|
|
|
ginprom.Subsystem(""),
|
2023-12-22 12:13:44 +00:00
|
|
|
ginprom.Ignore(c.path),
|
2023-12-21 16:04:04 +00:00
|
|
|
ginprom.Path(c.path),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &prometheusService{
|
|
|
|
prometheus: p,
|
|
|
|
config: c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 19:11:33 +00:00
|
|
|
func (s *prometheusService) init() error {
|
2023-12-21 16:04:04 +00:00
|
|
|
if !s.config.enabled {
|
2024-06-03 19:11:33 +00:00
|
|
|
return nil
|
2023-12-21 16:04:04 +00:00
|
|
|
}
|
|
|
|
|
2024-06-03 19:11:33 +00:00
|
|
|
if err := s.registerGaugeNoLabels(metricUpdatesTotal, metricUpdatesTotalHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.registerGaugeNoLabels(metricUpdatesPending, metricUpdatesPendingHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.registerGaugeNoLabels(metricUpdatesIgnored, metricUpdatesIgnoredHelp); err != nil {
|
|
|
|
return err
|
2023-12-21 16:04:04 +00:00
|
|
|
}
|
2024-06-03 19:11:33 +00:00
|
|
|
if err := s.registerGaugeNoLabels(metricUpdatesApproved, metricUpdatesApprovedHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.registerGaugeNoLabels(metricWebhooks, metricWebhooksHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.registerGaugeNoLabels(metricEvents, metricEventsHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := s.registerGaugeNoLabels(metricActions, metricActionsHelp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-12-21 16:04:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) registerGaugeNoLabels(name string, help string) error {
|
|
|
|
return s.registerGauge(name, help, make([]string, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) registerGauge(name string, help string, labels []string) error {
|
|
|
|
if !s.config.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" || help == "" {
|
|
|
|
return errorValidationNotBlank
|
|
|
|
}
|
|
|
|
|
|
|
|
s.prometheus.AddCustomGauge(name, help, labels)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) registerCounterNoLabels(name string, help string) error {
|
|
|
|
return s.registerCounter(name, help, make([]string, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) registerCounter(name string, help string, labels []string) error {
|
|
|
|
if !s.config.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" || help == "" {
|
|
|
|
return errorValidationNotBlank
|
|
|
|
}
|
|
|
|
|
|
|
|
s.prometheus.AddCustomCounter(name, help, labels)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) setGaugeNoLabels(name string, value float64) error {
|
|
|
|
return s.setGauge(name, make([]string, 0), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) setGauge(name string, labelValues []string, value float64) error {
|
|
|
|
if !s.config.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return errorValidationNotBlank
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.prometheus.SetGaugeValue(name, labelValues, value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) increaseCounterNoLabels(name string) error {
|
|
|
|
return s.increaseCounter(name, make([]string, 0))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *prometheusService) increaseCounter(name string, labelValues []string) error {
|
|
|
|
if !s.config.enabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "" {
|
|
|
|
return errorValidationNotBlank
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := s.prometheus.IncrementCounterValue(name, labelValues); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|