@@ -16,29 +16,29 @@ var (
1616 Name : "mailbox_count" ,
1717 })
1818
19- // activeSessions tracks the active session count for mailbox
19+ // activeSessions tracks the active session count for mailbox.
2020 activeSessions = prometheus .NewGauge (prometheus.GaugeOpts {
2121 Namespace : "hashmail" ,
2222 Name : "mailbox_active_sessions" ,
2323 Help : "Number of active sessions" ,
2424 })
2525
26- // standbySessions tracks the standby session count for mailbox
26+ // standbySessions tracks the standby session count for mailbox.
2727 standbySessions = prometheus .NewGauge (prometheus.GaugeOpts {
2828 Namespace : "hashmail" ,
2929 Name : "mailbox_standby_sessions" ,
3030 Help : "Number of standby sessions" ,
3131 })
3232
33- // inUseSessions tracks the in-use session count for mailbox
33+ // inUseSessions tracks the in-use session count for mailbox.
3434 inUseSessions = prometheus .NewGauge (prometheus.GaugeOpts {
3535 Namespace : "hashmail" ,
3636 Name : "mailbox_inuse_sessions" ,
3737 Help : "Number of in-use sessions" ,
3838 })
3939)
4040
41- // streamActivityTracker handles the calculation of session statistics
41+ // streamActivityTracker handles the calculation of session statistics.
4242var streamActivityTracker = newStreamActivity ()
4343
4444// PrometheusConfig is the set of configuration data that specifies if
@@ -56,7 +56,9 @@ type PrometheusConfig struct {
5656// StartPrometheusExporter registers all relevant metrics with the Prometheus
5757// library, then launches the HTTP server that Prometheus will hit to scrape
5858// our metrics.
59- func StartPrometheusExporter (cfg * PrometheusConfig ) error {
59+ func StartPrometheusExporter (cfg * PrometheusConfig ,
60+ shutdown <- chan struct {}) error {
61+
6062 // If we're not active, then there's nothing more to do.
6163 if ! cfg .Enabled {
6264 return nil
@@ -68,16 +70,23 @@ func StartPrometheusExporter(cfg *PrometheusConfig) error {
6870 prometheus .MustRegister (standbySessions )
6971 prometheus .MustRegister (inUseSessions )
7072
71- // Periodically update session classification metrics from internal tracker
73+ // Periodically update session classification metrics from internal tracker.
7274 go func () {
7375 ticker := time .NewTicker (10 * time .Second )
7476 defer ticker .Stop ()
7577
76- for range ticker .C {
77- active , standby , inuse := streamActivityTracker .ClassifyAndReset ()
78- activeSessions .Set (float64 (active ))
79- standbySessions .Set (float64 (standby ))
80- inUseSessions .Set (float64 (inuse ))
78+ for {
79+ select {
80+ case <- ticker .C :
81+ active , standby , inuse :=
82+ streamActivityTracker .ClassifyAndReset ()
83+ activeSessions .Set (float64 (active ))
84+ standbySessions .Set (float64 (standby ))
85+ inUseSessions .Set (float64 (inuse ))
86+ case <- shutdown :
87+ log .Infof ("Shutting down Prometheus session metrics updater" )
88+ return
89+ }
8190 }
8291 }()
8392
0 commit comments