feat(session): track active sessions

This changes adds a prometheus guage that can be used to approximate
the number of sessions for a given page or country.
This commit is contained in:
Mya 2022-06-08 11:13:54 -05:00
parent 02d115f682
commit 5b1ee690f9
No known key found for this signature in database
GPG Key ID: C3ECFA648DAD27FA
3 changed files with 26 additions and 2 deletions

@ -74,3 +74,13 @@ pages_page_session_seconds_bucket{country="",path="/",le="+Inf"} 1
pages_page_session_seconds_sum{country="",path="/"} 1.855976549
pages_page_session_seconds_count{country="",path="/"} 1
```
### pages_page_sessions_active
A gauge that approximates the current number of sessions per page.
```text
# HELP pages_page_sessions_active the number of current sessions for a given page
# TYPE pages_page_sessions_active gauge
pages_page_sessions_active{country="",path="/"} 1
```

@ -45,4 +45,14 @@ var (
},
[]string{"path", "country"},
)
PageSessionsActive = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: page,
Name: "sessions_active",
Help: "the number of current sessions for a given page",
},
[]string{"path", "country"},
)
)

@ -46,16 +46,20 @@ type Handle struct {
func (h *Handle) ServeHTTP(w http.ResponseWriter, r *http.Request) {
conn, err := h.upgrader.Upgrade(w, r, nil)
defer func() { _ = conn.Close() }()
if err != nil {
// log
return
}
defer func() { _ = conn.Close() }()
path := r.URL.Path
geoInfo := geoip.Extract(r.Context())
active := metrics.PageSessionsActive.WithLabelValues(path, geoInfo.CountryCode)
active.Inc()
defer func() { active.Dec() }()
start := time.Now()
defer func() {
metrics.PageSessionDuration.WithLabelValues(path, geoInfo.CountryCode).Observe(time.Since(start).Seconds())