Add EventBroadcaster metrics for observability and debugging
All checks were successful
CI / build (pull_request) Successful in 38s
All checks were successful
CI / build (pull_request) Successful in 38s
Implement comprehensive metrics tracking for EventBroadcaster implementations: - Add BroadcasterMetrics interface for reading metrics per namespace - Add MetricsCollector interface and DefaultMetricsCollector implementation - Track events_published and events_received counters per namespace - Track active_subscriptions gauge per namespace - Track publish_errors, subscribe_errors, and dropped_events counters - Add MetricsProvider interface for EventBroadcaster implementations - Integrate metrics tracking into EventBus and NATSEventBus - Add optional Prometheus integration via PrometheusMetricsAdapter Closes #22 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -66,6 +66,8 @@ func (neb *NATSEventBus) Subscribe(namespaceID string) <-chan *Event {
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("[NATSEventBus] Failed to subscribe to NATS subject %s: %v", subject, err)
|
||||
// Record subscription error
|
||||
neb.metrics.RecordSubscribeError(namespaceID)
|
||||
} else {
|
||||
neb.subscriptions = append(neb.subscriptions, sub)
|
||||
log.Printf("[NATSEventBus] Node %s subscribed to %s", neb.nodeID, subject)
|
||||
@@ -109,13 +111,16 @@ func (neb *NATSEventBus) handleNATSEvent(msg *nats.Msg) {
|
||||
return
|
||||
}
|
||||
|
||||
// Record receive from NATS (cross-node event)
|
||||
neb.metrics.RecordReceive(eventMsg.NamespaceID)
|
||||
|
||||
// Forward to local EventBus subscribers
|
||||
neb.EventBus.Publish(eventMsg.NamespaceID, eventMsg.Event)
|
||||
}
|
||||
|
||||
// Publish publishes an event both locally and to NATS for cross-node broadcasting
|
||||
func (neb *NATSEventBus) Publish(namespaceID string, event *Event) {
|
||||
// First publish locally
|
||||
// First publish locally (this also records metrics)
|
||||
neb.EventBus.Publish(namespaceID, event)
|
||||
|
||||
// Then publish to NATS for other nodes
|
||||
@@ -130,11 +135,13 @@ func (neb *NATSEventBus) Publish(namespaceID string, event *Event) {
|
||||
data, err := json.Marshal(eventMsg)
|
||||
if err != nil {
|
||||
log.Printf("[NATSEventBus] Failed to marshal event for NATS: %v", err)
|
||||
neb.metrics.RecordPublishError(namespaceID)
|
||||
return
|
||||
}
|
||||
|
||||
if err := neb.nc.Publish(subject, data); err != nil {
|
||||
log.Printf("[NATSEventBus] Failed to publish event to NATS: %v", err)
|
||||
neb.metrics.RecordPublishError(namespaceID)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user