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

63 lines
1.7 KiB
Go

package monitoring
import (
"reflect"
"testing"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/rs/zerolog"
)
func TestNewMonitorImplementsInterface(t *testing.T) {
logger := zerolog.Nop()
m := NewMonitor(&logger)
i := reflect.TypeOf((*MonitorInterface)(nil)).Elem()
if !reflect.TypeOf(m).Implements(i) {
t.Fatal("Monitor doesn't implement MonitorInterface")
}
}
func TestMonitorSetLDAPMetricSucceeds(t *testing.T) {
logger := zerolog.Nop()
m := NewMonitor(&logger)
labels := map[string]string{"type": "test"}
m.SetLDAPMetric(labels, float64(10))
mLDAPMetric := dto.Metric{}
m.ldapMetric.With(labels).Write(&mLDAPMetric)
if mLDAPMetric.GetGauge().GetValue() != float64(10) {
t.Fatalf("metric value should have been set to %v", float64(10))
}
}
func TestMonitorSetResponseTimeMetricSucceeds(t *testing.T) {
logger := zerolog.Nop()
m := NewMonitor(&logger)
labels := map[string]string{"operation": "test", "status": "0"}
m.SetResponseTimeMetric(labels, float64(10))
mResponseTimeMetric := dto.Metric{}
m.responseTime.With(labels).(prometheus.Metric).Write(&mResponseTimeMetric)
if mResponseTimeMetric.GetHistogram().GetSampleSum() != float64(10) {
t.Fatalf("metric value should have been set to %v", float64(10))
}
for _, bucket := range mResponseTimeMetric.GetHistogram().GetBucket() {
if bucket.GetUpperBound() < float64(10) && bucket.GetCumulativeCount() != 0 {
t.Fatal("there should be no count for this metric bucket")
}
if bucket.GetUpperBound() >= float64(10) && bucket.GetCumulativeCount() != 1 {
t.Fatal("there should be one entry into this metric bucket")
}
}
}