Add basic metric sink
This commit is contained in:
parent
cc72595398
commit
2c02b0c941
2 changed files with 43 additions and 0 deletions
|
@ -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
|
||||
})
|
||||
|
|
34
pkg/audit/sink/metric_sink.go
Normal file
34
pkg/audit/sink/metric_sink.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue