Add basic metric sink

This commit is contained in:
Peter 2021-01-26 19:00:46 +01:00
parent cc72595398
commit 2c02b0c941
Signed by: prskr
GPG key ID: C1DB5D2E8DB512F9
2 changed files with 43 additions and 0 deletions

View file

@ -290,6 +290,15 @@ func (a *app) WithEventStream() App {
return
}
var metricSink audit.Sink
if metricSink, err = sink.NewMetricSink(); err != nil {
return
}
if err = eventStream.RegisterSink(a.ctx, metricSink); err != nil {
return
}
a.ctx = context.WithValue(a.ctx, eventStreamKey, eventStream)
return
})

View file

@ -0,0 +1,34 @@
package sink
import (
"github.com/prometheus/client_golang/prometheus"
"gitlab.com/inetmock/inetmock/pkg/audit"
"gitlab.com/inetmock/inetmock/pkg/metrics"
)
func NewMetricSink() (sink audit.Sink, err error) {
var totalEventsCounter *prometheus.CounterVec
if totalEventsCounter, err = metrics.Counter("audit", "events_total", "", "application", "transport"); err != nil {
return
}
sink = &metricSink{
eventCounter: totalEventsCounter,
}
return
}
type metricSink struct {
eventCounter *prometheus.CounterVec
}
func (metricSink) Name() string {
return "metrics"
}
func (m metricSink) OnSubscribe(evs <-chan audit.Event) {
go func(evs <-chan audit.Event) {
for ev := range evs {
m.eventCounter.WithLabelValues(ev.Application.String(), ev.Transport.String()).Inc()
}
}(evs)
}