Skip to content

Commit

Permalink
speed up transport messages increments
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia committed Oct 19, 2023
1 parent f95a835 commit 9cf5d9e
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,50 +213,57 @@ type transportMessageLabels struct {
ChannelGroup string
}

type transportMessagesSent struct {
counterSent prometheus.Counter
counterSentSize prometheus.Counter
}

type transportMessagesReceived struct {
counterReceived prometheus.Counter
counterReceivedSize prometheus.Counter
}

var (
transportMessagesSentCache sync.Map
transportMessagesSentSizeCache sync.Map

transportMessagesReceivedCache sync.Map
transportMessagesReceivedSizeCache sync.Map
transportMessagesReceivedCache sync.Map
)

func (m *metrics) incTransportMessagesSent(transport string, channelGroup string, size int) {
labels := transportMessageLabels{
Transport: transport,
ChannelGroup: channelGroup,
}
counterSent, okSent := transportMessagesSentCache.Load(labels)
if !okSent {
counterSent = m.transportMessagesSent.WithLabelValues(transport, channelGroup)
transportMessagesSentCache.Store(labels, counterSent)
}
counterSentSize, okSentSize := transportMessagesSentSizeCache.Load(labels)
if !okSentSize {
counterSentSize = m.transportMessagesSentSize.WithLabelValues(transport, channelGroup)
transportMessagesSentSizeCache.Store(labels, counterSentSize)
}
counterSent.(prometheus.Counter).Inc()
counterSentSize.(prometheus.Counter).Add(float64(size))
counters, ok := transportMessagesSentCache.Load(labels)
if !ok {
counterSent := m.transportMessagesSent.WithLabelValues(transport, channelGroup)
counterSentSize := m.transportMessagesSentSize.WithLabelValues(transport, channelGroup)
counters = transportMessagesSent{
counterSent: counterSent,
counterSentSize: counterSentSize,
}
transportMessagesSentCache.Store(labels, counters)
}
counters.(transportMessagesSent).counterSent.Inc()
counters.(transportMessagesSent).counterSentSize.Add(float64(size))
}

func (m *metrics) incTransportMessagesReceived(transport string, channelGroup string, size int) {
labels := transportMessageLabels{
Transport: transport,
ChannelGroup: channelGroup,
}
counterReceived, okReceived := transportMessagesReceivedCache.Load(labels)
if !okReceived {
counterReceived = m.transportMessagesReceived.WithLabelValues(transport, channelGroup)
transportMessagesReceivedCache.Store(labels, counterReceived)
}
counterReceivedSize, okReceivedSize := transportMessagesReceivedSizeCache.Load(labels)
if !okReceivedSize {
counterReceivedSize = m.transportMessagesReceivedSize.WithLabelValues(transport, channelGroup)
transportMessagesReceivedSizeCache.Store(labels, counterReceivedSize)
}
counterReceived.(prometheus.Counter).Inc()
counterReceivedSize.(prometheus.Counter).Add(float64(size))
counters, ok := transportMessagesReceivedCache.Load(labels)
if !ok {
counterReceived := m.transportMessagesReceived.WithLabelValues(transport, channelGroup)
counterReceivedSize := m.transportMessagesReceivedSize.WithLabelValues(transport, channelGroup)
counters = transportMessagesReceived{
counterReceived: counterReceived,
counterReceivedSize: counterReceivedSize,
}
transportMessagesReceivedCache.Store(labels, counters)
}
counters.(transportMessagesReceived).counterReceived.Inc()
counters.(transportMessagesReceived).counterReceivedSize.Add(float64(size))
}

func (m *metrics) incServerDisconnect(code uint32) {
Expand Down

0 comments on commit 9cf5d9e

Please sign in to comment.