upda/server/service_prometheus.go

143 lines
3.1 KiB
Go
Raw Normal View History

2023-12-21 16:04:04 +00:00
package server
import (
"fmt"
2023-12-21 16:04:04 +00:00
"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),
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(""),
ginprom.Ignore(c.path),
2023-12-21 16:04:04 +00:00
ginprom.Path(c.path),
)
}
}
return &prometheusService{
prometheus: p,
config: c,
}
}
func (s *prometheusService) init() error {
2023-12-21 16:04:04 +00:00
if !s.config.enabled {
return nil
2023-12-21 16:04:04 +00:00
}
var err error
err = s.registerGaugeNoLabels(metricUpdatesTotal, metricUpdatesTotalHelp)
err = s.registerGaugeNoLabels(metricUpdatesPending, metricUpdatesPendingHelp)
err = s.registerGaugeNoLabels(metricUpdatesIgnored, metricUpdatesIgnoredHelp)
err = s.registerGaugeNoLabels(metricUpdatesApproved, metricUpdatesApprovedHelp)
err = s.registerGaugeNoLabels(metricWebhooks, metricWebhooksHelp)
err = s.registerGaugeNoLabels(metricEvents, metricEventsHelp)
err = s.registerGaugeNoLabels(metricActions, metricActionsHelp)
2023-12-21 16:04:04 +00:00
if err != nil {
return newServiceError(General, fmt.Errorf("cannot initialize service: %w", err))
2023-12-21 16:04:04 +00:00
}
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
}