To reduce bot traffic you must login to view /lordwelch/glauth/src/commit/355c35dc795620d056c2cc18b56199ca1f41a09e/go.work.
The GitHub login only links via username.
Files
Alessandro Cabbia 94fe62cfe1 monitoring: metrics enhancements and proposal for dropping expvar (#351)
* feat: introduce new prometheus monitor object

* feat: add LDAPMonitorWatcher as a potential replacement for v0 Collector

* feat: pass monitor object as dependency and instrument core operations

* feat: instantiate monitor in main

* ci: exclude mock files

* ci: generate mocks before running tests and go vet

* ci: make vet command keep going in case of errors

reason is due to the following happening in the GetStats method of the ldap.Stats struct

```
internal/monitoring/mock_interfaces.go:92:13: assignment copies lock value to ret0: (github.com/nmcclain/ldap.Stats, bool) contains github.com/nmcclain/ldap.Stats contains sync.Mutex
internal/monitoring/mock_interfaces.go:93:9: return copies lock value: github.com/nmcclain/ldap.Stats contains sync.Mutex
internal/monitoring/ldap_test.go:23:56: call of mockLDAPServer.EXPECT().GetStats().MinTimes(1).Return copies lock value: github.com/nmcclain/ldap.Stats contains sync.Mutex
```

* deps:  move to use go.uber.org/mock/gomock
2023-10-21 11:58:12 -07:00

110 lines
2.3 KiB
Go

package monitoring
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/rs/zerolog"
)
type Monitor struct {
responseTime *prometheus.HistogramVec
ldapMetric *prometheus.GaugeVec
logger *zerolog.Logger
}
func (m *Monitor) SetResponseTimeMetric(tags map[string]string, value float64) error {
if m.responseTime == nil {
return fmt.Errorf("metric not instantiated")
}
m.responseTime.With(tags).Observe(value)
return nil
}
func (m *Monitor) SetLDAPMetric(tags map[string]string, value float64) error {
if m.ldapMetric == nil {
return fmt.Errorf("metric not instantiated")
}
m.ldapMetric.With(tags).Set(value)
return nil
}
func (m *Monitor) constLabels() map[string]string {
return map[string]string{
"library": "github.com/glauth/glauth",
}
}
func (m *Monitor) registerHistograms() {
histograms := make([]*prometheus.HistogramVec, 0)
m.responseTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "tcp_response_time_seconds",
Help: "tcp_response_time_seconds",
ConstLabels: m.constLabels(),
},
[]string{"operation", "status"},
)
histograms = append(histograms, m.responseTime)
for _, histogram := range histograms {
err := prometheus.Register(histogram)
switch err.(type) {
case nil:
return
case prometheus.AlreadyRegisteredError:
m.logger.Debug().Interface("metric", histogram).Msg("metric already registered")
default:
m.logger.Error().Interface("metric", histogram).Msg("metric could not be registered")
}
}
}
func (m *Monitor) registerGauges() {
gauges := make([]*prometheus.GaugeVec, 0)
m.ldapMetric = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "ldap_metric",
Help: "ldap_metric",
ConstLabels: m.constLabels(),
},
[]string{"type"},
)
gauges = append(gauges, m.ldapMetric)
for _, gauge := range gauges {
err := prometheus.Register(gauge)
switch err.(type) {
case nil:
return
case prometheus.AlreadyRegisteredError:
m.logger.Debug().Interface("metric", gauge).Msg("metric already registered")
default:
m.logger.Error().Interface("metric", gauge).Msg("metric could not be registered")
}
}
}
func NewMonitor(logger *zerolog.Logger) *Monitor {
m := new(Monitor)
m.logger = logger
m.registerHistograms()
m.registerGauges()
return m
}