diff --git a/README.md b/README.md index 1d6df56..3f2b428 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index b0d898c..d41c44d 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -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"}, + ) ) diff --git a/internal/session/handler.go b/internal/session/handler.go index a1aaa67..6b040b7 100644 --- a/internal/session/handler.go +++ b/internal/session/handler.go @@ -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())