Skip to content

Commit a317ec9

Browse files
Remove snake case, change milliseconds to seconds per standards (#1380)
* Remove snake case, change milliseconds to seconds per standards * Update to use seconds instead of milliseconds * Delete old unused function GetTImeDiffSeconds
1 parent e35d910 commit a317ec9

File tree

3 files changed

+14
-53
lines changed

3 files changed

+14
-53
lines changed

backend/pkg/commons/metrics/metrics.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,20 @@ var (
8686
Name: "counter",
8787
Help: "Generic counter of events with name in labels",
8888
}, []string{"name"})
89-
NotificationsQueue_Event_Size = promauto.NewGaugeVec(prometheus.GaugeOpts{
89+
NotificationsQueueEventSize = promauto.NewGaugeVec(prometheus.GaugeOpts{
9090
Name: "notifications_queue_event_size",
9191
Help: "Number of notifications in the queue by event type and status",
9292
}, []string{"event_type", "status"})
93-
NotificationsQueue_Channel_Size = promauto.NewGaugeVec(prometheus.GaugeOpts{
93+
NotificationsQueueChannelSize = promauto.NewGaugeVec(prometheus.GaugeOpts{
9494
Name: "notifications_queue_channel_size",
9595
Help: "Number of notifications in the queue by channel and status",
9696
}, []string{"channel", "status"})
97-
NotificationsQueue_Pending_Time = promauto.NewHistogramVec(prometheus.HistogramOpts{
98-
Name: "notifications_queue_pending_duration_milliseconds",
97+
NotificationsQueuePendingTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
98+
Name: "notifications_queue_pending_duration_seconds",
9999
Help: "How long pending notifications have been in the queue",
100100
}, []string{"channel", "event_type"})
101-
NotificationsQueue_Sent_Time = promauto.NewHistogramVec(prometheus.HistogramOpts{
102-
Name: "notifications_queue_sent_duration_milliseconds",
101+
NotificationsQueueSentTime = promauto.NewHistogramVec(prometheus.HistogramOpts{
102+
Name: "notifications_queue_sent_duration_seconds",
103103
Help: "Amount of time notification took to be successfully sent",
104104
}, []string{"channel", "event_type"})
105105
)

backend/pkg/notification/sending.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"encoding/json"
77
"fmt"
88
"io"
9-
"math"
109
"net/http"
1110
"net/url"
1211
"strings"
@@ -630,39 +629,41 @@ func collectNotificationQueueMetrics() {
630629
eventType := GetEventLabelForNotification(notification)
631630

632631
// Record the amount of time records that were sent (and that still exist in the queue) took to sent
633-
metrics.NotificationsQueue_Sent_Time.WithLabelValues(notification.Channel, eventType).Observe(GetTimeDiffMilliseconds(*notification.Sent, *notification.Created))
632+
duration := notification.Created.Sub(*notification.Sent).Abs()
633+
metrics.NotificationsQueueSentTime.WithLabelValues(notification.Channel, eventType).Observe(duration.Seconds())
634634
}
635635

636636
// Record for each pending notification how long it has been in the queue
637637
for _, notification := range pendingNotifications {
638638
eventType := GetEventLabelForNotification(notification)
639639

640640
// Record the amount of time these records have been waiting to been sent
641-
metrics.NotificationsQueue_Pending_Time.WithLabelValues(notification.Channel, eventType).Observe(GetTimeDiffMilliseconds(*notification.Created, now))
641+
duration := notification.Created.Sub(now).Abs()
642+
metrics.NotificationsQueuePendingTime.WithLabelValues(notification.Channel, eventType).Observe(duration.Seconds())
642643
}
643644

644645
// Count number of pending notifications in the queue by event type
645646
eventTypeCount := CountByEventType(pendingNotifications)
646647
for eventType, numNotifications := range eventTypeCount {
647-
metrics.NotificationsQueue_Event_Size.WithLabelValues(eventType, string(Pending)).Set(float64(numNotifications))
648+
metrics.NotificationsQueueEventSize.WithLabelValues(eventType, string(Pending)).Set(float64(numNotifications))
648649
}
649650

650651
// Count number of sent notifications in the queue by event type
651652
eventTypeCount = CountByEventType(sentNotifications)
652653
for eventType, numNotifications := range eventTypeCount {
653-
metrics.NotificationsQueue_Event_Size.WithLabelValues(eventType, string(Sent)).Set(float64(numNotifications))
654+
metrics.NotificationsQueueEventSize.WithLabelValues(eventType, string(Sent)).Set(float64(numNotifications))
654655
}
655656

656657
// Count number of pending notifications in the queue by channel
657658
channelCount := CountByChannel(pendingNotifications)
658659
for channelType, numNotifications := range channelCount {
659-
metrics.NotificationsQueue_Channel_Size.WithLabelValues(channelType, string(Pending)).Set(float64(numNotifications))
660+
metrics.NotificationsQueueChannelSize.WithLabelValues(channelType, string(Pending)).Set(float64(numNotifications))
660661
}
661662

662663
// Count number of sent notifications in the queue by channel
663664
channelCount = CountByChannel(sentNotifications)
664665
for channelType, numNotifications := range channelCount {
665-
metrics.NotificationsQueue_Channel_Size.WithLabelValues(channelType, string(Sent)).Set(float64(numNotifications))
666+
metrics.NotificationsQueueChannelSize.WithLabelValues(channelType, string(Sent)).Set(float64(numNotifications))
666667
}
667668
}
668669

@@ -736,12 +737,3 @@ func CountByChannel(notifications []Notification) map[string]int {
736737
}
737738
return channelCountMap
738739
}
739-
740-
/**
741-
* Returns the amount of milliseconds between two timestamps. Always returns a positive
742-
* duration, so you don't have to worry about date ordering
743-
*/
744-
func GetTimeDiffMilliseconds(time1 time.Time, time2 time.Time) float64 {
745-
duration := time1.Sub(time2)
746-
return math.Abs(float64(duration.Milliseconds()))
747-
}

backend/pkg/notification/sending_test.go

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,11 @@ package notification
22

33
import (
44
"testing"
5-
"time"
65

76
"github.com/gobitfly/beaconchain/pkg/commons/types"
87
"github.com/google/go-cmp/cmp"
98
)
109

11-
// getTimeDiffMilliseconds - Positive diff
12-
// getTimeDiffMilliseconds - Negative diff
13-
// countByChannel - All Notifications, no notifications, unknown notification type
14-
//
15-
// countByEventType
16-
// extractEventTypeFromNotification - All event types, grab from DB
17-
18-
func TestGetTimeDiffMilliseconds(t *testing.T) {
19-
tests := map[string]struct {
20-
time1 time.Time
21-
time2 time.Time
22-
want float64
23-
}{
24-
"no time difference": {time1: time.UnixMilli(9000000000), time2: time.UnixMilli(9000000000), want: 0.0},
25-
"positive time difference": {time1: time.UnixMilli(9000001000), time2: time.UnixMilli(9000000000), want: 1000.0},
26-
"negative time difference": {time1: time.UnixMilli(9000000000), time2: time.UnixMilli(9000001000), want: 1000.0},
27-
"sub-second time difference": {time1: time.UnixMilli(9000001000), time2: time.UnixMilli(9000000500), want: 500.0},
28-
}
29-
30-
for name, tc := range tests {
31-
t.Run(name, func(t *testing.T) {
32-
got := GetTimeDiffMilliseconds(tc.time1, tc.time2)
33-
diff := cmp.Diff(tc.want, got)
34-
if diff != "" {
35-
t.Fatal(diff)
36-
}
37-
})
38-
}
39-
}
40-
4110
func TestExtractEventNameStringFromNotificationForMetrics(t *testing.T) {
4211
tests := map[string]struct {
4312
notification Notification

0 commit comments

Comments
 (0)