From 810b72187fdd1636caa078278f6a8186c931f324 Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Mon, 10 Jun 2024 14:41:20 -0300 Subject: [PATCH 1/2] Wrap go routines around metric calls --- service/adapters/prometheus/prometheus.go | 100 ++++++++++++++-------- 1 file changed, 64 insertions(+), 36 deletions(-) diff --git a/service/adapters/prometheus/prometheus.go b/service/adapters/prometheus/prometheus.go index 8f6720c..49503d3 100644 --- a/service/adapters/prometheus/prometheus.go +++ b/service/adapters/prometheus/prometheus.go @@ -237,43 +237,57 @@ func (p *Prometheus) StartApplicationCall(handlerName string) app.ApplicationCal } func (p *Prometheus) ReportNumberOfRelayDownloaders(n int) { - p.relayDownloadersGauge.Set(float64(n)) + go func() { + p.relayDownloadersGauge.Set(float64(n)) + }() } func (p *Prometheus) ReportRelayConnectionsState(m map[domain.RelayAddress]relays.RelayConnectionState) { - p.relayConnectionStateGauge.Reset() - for address, state := range m { - p.relayConnectionStateGauge.With( - prometheus.Labels{ - labelConnectionState: state.String(), - labelAddress: address.String(), - }, - ).Set(1) - } + go func() { + p.relayConnectionStateGauge.Reset() + for address, state := range m { + p.relayConnectionStateGauge.With( + prometheus.Labels{ + labelConnectionState: state.String(), + labelAddress: address.String(), + }, + ).Set(1) + } + }() } func (p *Prometheus) ReportReceivedEvent(address domain.RelayAddress) { - p.receivedEventsCounter.With(prometheus.Labels{labelAddress: address.String()}).Inc() + go func() { + p.receivedEventsCounter.With(prometheus.Labels{labelAddress: address.String()}).Inc() + }() } func (p *Prometheus) ReportQueueLength(topic string, n int) { - p.subscriptionQueueLengthGauge.With(prometheus.Labels{labelTopic: topic}).Set(float64(n)) + go func() { + p.subscriptionQueueLengthGauge.With(prometheus.Labels{labelTopic: topic}).Set(float64(n)) + }() } func (p *Prometheus) ReportQueueOldestMessageAge(topic string, age time.Duration) { - p.subscriptionQueueOldestMessageAgeGauge.With(prometheus.Labels{labelTopic: topic}).Set(age.Seconds()) + go func() { + p.subscriptionQueueOldestMessageAgeGauge.With(prometheus.Labels{labelTopic: topic}).Set(age.Seconds()) + }() } func (p *Prometheus) ReportNumberOfSubscriptions(address domain.RelayAddress, n int) { - p.relayConnectionSubscriptionsGauge.With(prometheus.Labels{ - labelAddress: address.String(), - }).Set(float64(n)) + go func() { + p.relayConnectionSubscriptionsGauge.With(prometheus.Labels{ + labelAddress: address.String(), + }).Set(float64(n)) + }() } func (p *Prometheus) ReportRateLimitBackoffMs(address domain.RelayAddress, n int) { - p.relayRateLimitBackoffMsGauge.With(prometheus.Labels{ - labelAddress: address.HostWithoutPort(), - }).Set(float64(n)) + go func() { + p.relayRateLimitBackoffMsGauge.With(prometheus.Labels{ + labelAddress: address.HostWithoutPort(), + }).Set(float64(n)) + }() } func (p *Prometheus) ReportMessageReceived(address domain.RelayAddress, messageType relays.MessageType, err *error) { @@ -282,37 +296,49 @@ func (p *Prometheus) ReportMessageReceived(address domain.RelayAddress, messageT labelMessageType: messageType.String(), } setResultLabel(labels, err) - p.relayConnectionReceivedMessagesCounter.With(labels).Inc() + go func() { + p.relayConnectionReceivedMessagesCounter.With(labels).Inc() + }() } func (p *Prometheus) ReportRelayDisconnection(address domain.RelayAddress, err error) { - p.relayConnectionDisconnectionsCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelReason: p.getDisconnectionReason(err), - }).Inc() + go func() { + p.relayConnectionDisconnectionsCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelReason: p.getDisconnectionReason(err), + }).Inc() + }() } func (p *Prometheus) ReportNumberOfStoredRelayAddresses(n int) { - p.storedRelayAddressesGauge.Set(float64(n)) + go func() { + p.storedRelayAddressesGauge.Set(float64(n)) + }() } func (p *Prometheus) ReportNumberOfStoredEvents(n int) { - p.storedEventsGauge.Set(float64(n)) + go func() { + p.storedEventsGauge.Set(float64(n)) + }() } func (p *Prometheus) ReportEventSentToRelay(address domain.RelayAddress, decision app.SendEventToRelayDecision, result app.SendEventToRelayResult) { - p.eventsSentToRelayCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelDecision: decision.String(), - labelResult: result.String(), - }).Inc() + go func() { + p.eventsSentToRelayCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelDecision: decision.String(), + labelResult: result.String(), + }).Inc() + }() } func (p *Prometheus) ReportNotice(address domain.RelayAddress, noticeType relays.NoticeType) { - p.noticeTypeCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelNoticeType: string(noticeType), - }).Inc() + go func() { + p.noticeTypeCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelNoticeType: string(noticeType), + }).Inc() + }() } func (p *Prometheus) Registry() *prometheus.Registry { @@ -361,7 +387,9 @@ func (a *ApplicationCall) End(err *error) { labels := a.getLabels(err) a.p.applicationHandlerCallsCounter.With(labels).Inc() - a.p.applicationHandlerCallDurationHistogram.With(labels).Observe(duration.Seconds()) + go func() { + a.p.applicationHandlerCallDurationHistogram.With(labels).Observe(duration.Seconds()) + }() } func (a *ApplicationCall) getLabels(err *error) prometheus.Labels { From 26aa8a46f31ee0c2faae31eca6865567b296d3fe Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Mon, 10 Jun 2024 14:49:41 -0300 Subject: [PATCH 2/2] Cleaner goroutines --- service/adapters/prometheus/prometheus.go | 80 ++++++++--------------- 1 file changed, 27 insertions(+), 53 deletions(-) diff --git a/service/adapters/prometheus/prometheus.go b/service/adapters/prometheus/prometheus.go index 49503d3..f012dbe 100644 --- a/service/adapters/prometheus/prometheus.go +++ b/service/adapters/prometheus/prometheus.go @@ -237,9 +237,7 @@ func (p *Prometheus) StartApplicationCall(handlerName string) app.ApplicationCal } func (p *Prometheus) ReportNumberOfRelayDownloaders(n int) { - go func() { - p.relayDownloadersGauge.Set(float64(n)) - }() + go p.relayDownloadersGauge.Set(float64(n)) } func (p *Prometheus) ReportRelayConnectionsState(m map[domain.RelayAddress]relays.RelayConnectionState) { @@ -257,37 +255,27 @@ func (p *Prometheus) ReportRelayConnectionsState(m map[domain.RelayAddress]relay } func (p *Prometheus) ReportReceivedEvent(address domain.RelayAddress) { - go func() { - p.receivedEventsCounter.With(prometheus.Labels{labelAddress: address.String()}).Inc() - }() + go p.receivedEventsCounter.With(prometheus.Labels{labelAddress: address.String()}).Inc() } func (p *Prometheus) ReportQueueLength(topic string, n int) { - go func() { - p.subscriptionQueueLengthGauge.With(prometheus.Labels{labelTopic: topic}).Set(float64(n)) - }() + go p.subscriptionQueueLengthGauge.With(prometheus.Labels{labelTopic: topic}).Set(float64(n)) } func (p *Prometheus) ReportQueueOldestMessageAge(topic string, age time.Duration) { - go func() { - p.subscriptionQueueOldestMessageAgeGauge.With(prometheus.Labels{labelTopic: topic}).Set(age.Seconds()) - }() + go p.subscriptionQueueOldestMessageAgeGauge.With(prometheus.Labels{labelTopic: topic}).Set(age.Seconds()) } func (p *Prometheus) ReportNumberOfSubscriptions(address domain.RelayAddress, n int) { - go func() { - p.relayConnectionSubscriptionsGauge.With(prometheus.Labels{ - labelAddress: address.String(), - }).Set(float64(n)) - }() + go p.relayConnectionSubscriptionsGauge.With(prometheus.Labels{ + labelAddress: address.String(), + }).Set(float64(n)) } func (p *Prometheus) ReportRateLimitBackoffMs(address domain.RelayAddress, n int) { - go func() { - p.relayRateLimitBackoffMsGauge.With(prometheus.Labels{ - labelAddress: address.HostWithoutPort(), - }).Set(float64(n)) - }() + go p.relayRateLimitBackoffMsGauge.With(prometheus.Labels{ + labelAddress: address.HostWithoutPort(), + }).Set(float64(n)) } func (p *Prometheus) ReportMessageReceived(address domain.RelayAddress, messageType relays.MessageType, err *error) { @@ -296,49 +284,37 @@ func (p *Prometheus) ReportMessageReceived(address domain.RelayAddress, messageT labelMessageType: messageType.String(), } setResultLabel(labels, err) - go func() { - p.relayConnectionReceivedMessagesCounter.With(labels).Inc() - }() + go p.relayConnectionReceivedMessagesCounter.With(labels).Inc() } func (p *Prometheus) ReportRelayDisconnection(address domain.RelayAddress, err error) { - go func() { - p.relayConnectionDisconnectionsCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelReason: p.getDisconnectionReason(err), - }).Inc() - }() + go p.relayConnectionDisconnectionsCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelReason: p.getDisconnectionReason(err), + }).Inc() } func (p *Prometheus) ReportNumberOfStoredRelayAddresses(n int) { - go func() { - p.storedRelayAddressesGauge.Set(float64(n)) - }() + go p.storedRelayAddressesGauge.Set(float64(n)) } func (p *Prometheus) ReportNumberOfStoredEvents(n int) { - go func() { - p.storedEventsGauge.Set(float64(n)) - }() + go p.storedEventsGauge.Set(float64(n)) } func (p *Prometheus) ReportEventSentToRelay(address domain.RelayAddress, decision app.SendEventToRelayDecision, result app.SendEventToRelayResult) { - go func() { - p.eventsSentToRelayCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelDecision: decision.String(), - labelResult: result.String(), - }).Inc() - }() + go p.eventsSentToRelayCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelDecision: decision.String(), + labelResult: result.String(), + }).Inc() } func (p *Prometheus) ReportNotice(address domain.RelayAddress, noticeType relays.NoticeType) { - go func() { - p.noticeTypeCounter.With(prometheus.Labels{ - labelAddress: address.String(), - labelNoticeType: string(noticeType), - }).Inc() - }() + go p.noticeTypeCounter.With(prometheus.Labels{ + labelAddress: address.String(), + labelNoticeType: string(noticeType), + }).Inc() } func (p *Prometheus) Registry() *prometheus.Registry { @@ -387,9 +363,7 @@ func (a *ApplicationCall) End(err *error) { labels := a.getLabels(err) a.p.applicationHandlerCallsCounter.With(labels).Inc() - go func() { - a.p.applicationHandlerCallDurationHistogram.With(labels).Observe(duration.Seconds()) - }() + go a.p.applicationHandlerCallDurationHistogram.With(labels).Observe(duration.Seconds()) } func (a *ApplicationCall) getLabels(err *error) prometheus.Labels {