Skip to content

Commit

Permalink
feat: event system metrics (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
agaffney authored Aug 22, 2024
1 parent 88696a1 commit 1bbfef0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -110,4 +112,5 @@ func (e *EventBus) Publish(eventType EventType, evt Event) {
subCh <- evt
}
}
metricEventsTotal.WithLabelValues(string(eventType)).Inc()
}
37 changes: 37 additions & 0 deletions event/metrics.go
Original file line number Diff line number Diff line change
@@ -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"},
)
)

0 comments on commit 1bbfef0

Please sign in to comment.