diff --git a/event/event.go b/event/event.go index 25e3822..a9d74a6 100644 --- a/event/event.go +++ b/event/event.go @@ -71,6 +71,7 @@ func (e *EventBus) Subscribe(eventType EventType) (EventSubscriberId, <-chan Eve } evtTypeSubs := e.subscribers[eventType] evtTypeSubs[subId] = evtCh + metricSubscribers.WithLabelValues(string(eventType)).Inc() return subId, evtCh } @@ -96,6 +97,7 @@ func (e *EventBus) Unsubscribe(eventType EventType, subId EventSubscriberId) { if evtTypeSubs, ok := e.subscribers[eventType]; ok { delete(evtTypeSubs, subId) } + metricSubscribers.WithLabelValues(string(eventType)).Dec() } // Publish allows a producer to send an event of a particular type to all subscribers @@ -110,4 +112,5 @@ func (e *EventBus) Publish(eventType EventType, evt Event) { subCh <- evt } } + metricEventsTotal.WithLabelValues(string(eventType)).Inc() } diff --git a/event/metrics.go b/event/metrics.go new file mode 100644 index 0000000..0803fc4 --- /dev/null +++ b/event/metrics.go @@ -0,0 +1,37 @@ +// Copyright 2024 Blink Labs Software +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package event + +import ( + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promauto" +) + +var ( + metricEventsTotal = promauto.NewCounterVec( + prometheus.CounterOpts{ + Name: "event_total", + Help: "total events by type", + }, + []string{"type"}, + ) + metricSubscribers = promauto.NewGaugeVec( + prometheus.GaugeOpts{ + Name: "event_subscribers", + Help: "subscribers by event type", + }, + []string{"type"}, + ) +)