@@ -35,8 +35,8 @@ func StartServer(ctx context.Context, cancel context.CancelFunc) {
3535 logger .Info ("Start worker -->" )
3636 workerPool := worker .CreateWorkerPool (config .Worker .WorkersPoolSize , bufferChannel , config .Worker .DeliveryChannelSize , kPublisher )
3737 workerPool .StartWorkers ()
38- go kPublisher .ReportStats ()
39- go reportProcMetrics ()
38+ go kPublisher .ReportStats (ctx )
39+ go reportProcMetrics (ctx )
4040 go shutDownServer (ctx , cancel , httpServices , bufferChannel , workerPool , kPublisher )
4141}
4242
@@ -53,17 +53,19 @@ func shutDownServer(ctx context.Context, cancel context.CancelFunc, httpServices
5353 timedOut := workerPool .FlushWithTimeOut (config .Worker .WorkerFlushTimeout )
5454 if timedOut {
5555 logger .Info (fmt .Sprintf ("WorkerPool flush timedout %t" , timedOut ))
56+ } else {
57+ logger .Info ("WorkerPool flushed all events" )
5658 }
5759 flushInterval := config .PublisherKafka .FlushInterval
5860 logger .Info ("Closing Kafka producer" )
5961 logger .Info (fmt .Sprintf ("Wait %d ms for all messages to be delivered" , flushInterval ))
6062 eventsInProducer := kp .Close ()
61- /**
62- @TODO - should compute the actual no., of events per batch and therefore the total. We can do this only when we close all the active connections
63- Until then we fall back to approximation */
64- eventsInChannel : = len (bufferChannel ) * 7
65- logger . Info ( fmt . Sprintf ( "Outstanding unprocessed events in the channel, data lost ~ (No batches %d * 5 events) = ~%d" , len ( bufferChannel ), eventsInChannel ))
66- metrics .Count ("kafka_messages_delivered_total " , eventsInChannel + eventsInProducer , "success=false " )
63+ eventCountInChannel := 0
64+ for i := 0 ; i < len ( bufferChannel ); i ++ {
65+ req := <- bufferChannel
66+ eventCountInChannel + = len (req . Events )
67+ }
68+ metrics .Count ("shutdown_event_drops " , eventCountInChannel + eventsInProducer , "" )
6769 logger .Info ("Exiting server" )
6870 cancel ()
6971 default :
@@ -72,21 +74,28 @@ func shutDownServer(ctx context.Context, cancel context.CancelFunc, httpServices
7274 }
7375}
7476
75- func reportProcMetrics () {
76- t := time .Tick (config .MetricStatsd .FlushPeriodMs )
77+ func reportProcMetrics (ctx context.Context ) {
78+ ticker := time .NewTicker (config .MetricStatsd .FlushPeriodMs )
79+ defer ticker .Stop ()
80+
7781 m := & runtime.MemStats {}
7882 for {
79- <- t
80- metrics .Gauge ("server_go_routines_count_current" , runtime .NumGoroutine (), "" )
83+ select {
84+ case <- ctx .Done ():
85+ logger .Info ("Stopping proc metrics reporter" )
86+ return
87+ case <- ticker .C :
88+ metrics .Gauge ("server_go_routines_count_current" , runtime .NumGoroutine (), "" )
8189
82- runtime .ReadMemStats (m )
83- metrics .Gauge ("server_mem_heap_alloc_bytes_current" , m .HeapAlloc , "" )
84- metrics .Gauge ("server_mem_heap_inuse_bytes_current" , m .HeapInuse , "" )
85- metrics .Gauge ("server_mem_heap_objects_total_current" , m .HeapObjects , "" )
86- metrics .Gauge ("server_mem_stack_inuse_bytes_current" , m .StackInuse , "" )
87- metrics .Gauge ("server_mem_gc_triggered_current" , m .LastGC / 1000 , "" )
88- metrics .Gauge ("server_mem_gc_pauseNs_current" , m .PauseNs [(m .NumGC + 255 )% 256 ]/ 1000 , "" )
89- metrics .Gauge ("server_mem_gc_count_current" , m .NumGC , "" )
90- metrics .Gauge ("server_mem_gc_pauseTotalNs_current" , m .PauseTotalNs , "" )
90+ runtime .ReadMemStats (m )
91+ metrics .Gauge ("server_mem_heap_alloc_bytes_current" , m .HeapAlloc , "" )
92+ metrics .Gauge ("server_mem_heap_inuse_bytes_current" , m .HeapInuse , "" )
93+ metrics .Gauge ("server_mem_heap_objects_total_current" , m .HeapObjects , "" )
94+ metrics .Gauge ("server_mem_stack_inuse_bytes_current" , m .StackInuse , "" )
95+ metrics .Gauge ("server_mem_gc_triggered_current" , m .LastGC / 1000 , "" )
96+ metrics .Gauge ("server_mem_gc_pauseNs_current" , m .PauseNs [(m .NumGC + 255 )% 256 ]/ 1000 , "" )
97+ metrics .Gauge ("server_mem_gc_count_current" , m .NumGC , "" )
98+ metrics .Gauge ("server_mem_gc_pauseTotalNs_current" , m .PauseTotalNs , "" )
99+ }
91100 }
92101}
0 commit comments