11package prometheus
22
33import (
4+ "fmt"
45 "github.com/hellofresh/goengine/driver/sql"
56 "github.com/prometheus/client_golang/prometheus"
7+ "strconv"
8+ "time"
69)
710
811const namespace = "goengine"
912
1013type Metrics struct {
11- notificationCounter * prometheus.CounterVec
12- queueDuration * prometheus.HistogramVec
13- notificationHandleDuration * prometheus.HistogramVec
14+ notificationCounter * prometheus.CounterVec
15+ notificationQueueDuration * prometheus.HistogramVec
16+ notificationProcessingDuration * prometheus.HistogramVec
17+ notificationStartTimes map [string ]time.Time
1418}
1519
1620func NewMetrics () * Metrics {
@@ -22,10 +26,10 @@ func NewMetrics() *Metrics {
2226 Name : "notification_count" ,
2327 Help : "counter for number of notifications received" ,
2428 },
25- []string {"is_event " },
29+ []string {"is_notification " },
2630 ),
2731 // queueDuration is used to expose 'queue_duration_seconds' metrics
28- queueDuration : prometheus .NewHistogramVec (
32+ notificationQueueDuration : prometheus .NewHistogramVec (
2933 prometheus.HistogramOpts {
3034 Namespace : namespace ,
3135 Name : "queue_duration_seconds" ,
@@ -35,15 +39,15 @@ func NewMetrics() *Metrics {
3539 []string {"retry" , "success" },
3640 ),
3741
38- // notificationHandleDuration is used to expose 'notification_handle_duration_seconds' metrics
39- notificationHandleDuration : prometheus .NewHistogramVec (
42+ // notificationProcessingDuration is used to expose 'notification_handle_duration_seconds' metrics
43+ notificationProcessingDuration : prometheus .NewHistogramVec (
4044 prometheus.HistogramOpts {
4145 Namespace : namespace ,
42- Name : "notification_handle_duration_seconds " ,
43- Help : "histogram of event handled latencies" ,
46+ Name : "notification_processing_duration_seconds " ,
47+ Help : "histogram of notifications handled latencies" ,
4448 Buckets : []float64 {0.1 , 0.5 , 0.9 , 0.99 }, //buckets for histogram
4549 },
46- []string {"retry " , "success " },
50+ []string {"success " , "retry " },
4751 ),
4852 }
4953}
@@ -55,30 +59,39 @@ func (m *Metrics) RegisterMetrics(registry *prometheus.Registry) error {
5559 return err
5660 }
5761
58- err = registry .Register (m .queueDuration )
62+ err = registry .Register (m .notificationQueueDuration )
5963 if err != nil {
6064 return err
6165 }
6266
63- return registry .Register (m .notificationHandleDuration )
67+ return registry .Register (m .notificationProcessingDuration )
6468}
6569
6670// ReceivedNotification
6771func (m * Metrics ) ReceivedNotification (isNotification bool ) {
68-
72+ labels := prometheus.Labels {"is_notification" : strconv .FormatBool (isNotification )}
73+ m .notificationCounter .With (labels ).Inc ()
6974}
7075
7176// QueueNotification returns http handler for prometheus
7277func (m * Metrics ) QueueNotification (notification * sql.ProjectionNotification ) {
73-
78+ key := "q" + fmt .Sprintf ("%p" , notification )
79+ m .notificationStartTimes [key ] = time .Now ()
7480}
7581
7682// StartNotificationProcessing is used to record start time of notification processing
7783func (m * Metrics ) StartNotificationProcessing (notification * sql.ProjectionNotification ) {
78-
84+ key := "p" + fmt .Sprintf ("%p" , notification )
85+ m .notificationStartTimes [key ] = time .Now ()
7986}
8087
8188// FinishNotificationProcessing is used to observe end time of notification queue and processing time
82- func (m * Metrics ) FinishNotificationProcessing (notification * sql.ProjectionNotification ) {
83-
89+ func (m * Metrics ) FinishNotificationProcessing (notification * sql.ProjectionNotification , success bool , retry bool ) {
90+ memAddress := fmt .Sprintf ("%p" , notification )
91+ queueStartTime := m .notificationStartTimes ["q" + memAddress ]
92+ processingStartTime := m .notificationStartTimes ["p" + memAddress ]
93+ labels := prometheus.Labels {"success" : strconv .FormatBool (success ), "retry" : strconv .FormatBool (retry )}
94+
95+ m .notificationQueueDuration .With (labels ).Observe (time .Since (queueStartTime ).Seconds ())
96+ m .notificationProcessingDuration .With (labels ).Observe (time .Since (processingStartTime ).Seconds ())
8497}
0 commit comments