mirror of
				https://gitlab.crans.org/nounous/ghostream.git
				synced 2025-11-04 01:42:04 +01:00 
			
		
		
		
	Store connected viewers in Prometheus and serve this amount
This commit is contained in:
		
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@@ -5,10 +5,12 @@ go 1.13
 | 
			
		||||
require (
 | 
			
		||||
	github.com/go-ldap/ldap/v3 v3.2.3
 | 
			
		||||
	github.com/haivision/srtgo v0.0.0-20200731151239-e00427ae473a
 | 
			
		||||
	github.com/hashicorp/go.net v0.0.1
 | 
			
		||||
	github.com/markbates/pkger v0.17.1
 | 
			
		||||
	github.com/pion/rtp v1.6.0
 | 
			
		||||
	github.com/pion/webrtc/v3 v3.0.0-beta.5
 | 
			
		||||
	github.com/prometheus/client_golang v1.7.1
 | 
			
		||||
	github.com/prometheus/client_model v0.2.0
 | 
			
		||||
	github.com/spf13/viper v1.7.1
 | 
			
		||||
	golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										1
									
								
								go.sum
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								go.sum
									
									
									
									
									
								
							@@ -148,6 +148,7 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX
 | 
			
		||||
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
 | 
			
		||||
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
 | 
			
		||||
github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
 | 
			
		||||
github.com/hashicorp/go.net v0.0.1 h1:sNCoNyDEvN1xa+X0baata4RdcpKwcMS6DH+xwfqPgjw=
 | 
			
		||||
github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
 | 
			
		||||
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 | 
			
		||||
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@ import (
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus/promauto"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus/promhttp"
 | 
			
		||||
	dto "github.com/prometheus/client_model/go"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// Options holds web package configuration
 | 
			
		||||
@@ -26,8 +27,23 @@ var (
 | 
			
		||||
		Name: "ghostream_web_sessions_total",
 | 
			
		||||
		Help: "The total amount of WebRTC sessions exchanged",
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	// WebRTCConnectedSessions is the total amount of WebRTC session exchange
 | 
			
		||||
	WebRTCConnectedSessions = promauto.NewGauge(prometheus.GaugeOpts{
 | 
			
		||||
		Name: "ghostream_webrtc_connected_sessions",
 | 
			
		||||
		Help: "The current amount of opened WebRTC sessions",
 | 
			
		||||
	})
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func GetGaugeValue(metric prometheus.Gauge) float64 {
 | 
			
		||||
	var m = &dto.Metric{}
 | 
			
		||||
	if err := metric.Write(m); err != nil {
 | 
			
		||||
		log.Fatal(err)
 | 
			
		||||
		return 0
 | 
			
		||||
	}
 | 
			
		||||
	return m.Gauge.GetValue()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Serve monitoring server that expose prometheus metrics
 | 
			
		||||
func Serve(cfg *Options) {
 | 
			
		||||
	mux := http.NewServeMux()
 | 
			
		||||
 
 | 
			
		||||
@@ -2,6 +2,7 @@ package webrtc
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"gitlab.crans.org/nounous/ghostream/internal/monitoring"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"math/rand"
 | 
			
		||||
@@ -128,10 +129,12 @@ func newPeerHandler(remoteSdp webrtc.SessionDescription, cfg *Options) webrtc.Se
 | 
			
		||||
			// Register tracks
 | 
			
		||||
			videoTracks = append(videoTracks, videoTrack)
 | 
			
		||||
			audioTracks = append(audioTracks, audioTrack)
 | 
			
		||||
			monitoring.WebRTCConnectedSessions.Inc()
 | 
			
		||||
		} else if connectionState == webrtc.ICEConnectionStateDisconnected {
 | 
			
		||||
			// Unregister tracks
 | 
			
		||||
			videoTracks = removeTrack(videoTracks, videoTrack)
 | 
			
		||||
			audioTracks = removeTrack(audioTracks, audioTrack)
 | 
			
		||||
			monitoring.WebRTCConnectedSessions.Dec()
 | 
			
		||||
		}
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -84,3 +84,11 @@ func staticHandler() http.Handler {
 | 
			
		||||
	staticFs := http.FileServer(pkger.Dir("/web/static"))
 | 
			
		||||
	return http.StripPrefix("/static/", staticFs)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func statisticsHandler(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	// Display connected users stats
 | 
			
		||||
	enc := json.NewEncoder(w)
 | 
			
		||||
	enc.Encode(struct {
 | 
			
		||||
		ConnectedViewers int
 | 
			
		||||
	}{int(monitoring.GetGaugeValue(monitoring.WebRTCConnectedSessions))})
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -84,6 +84,7 @@ func Serve(rSdpChan chan webrtc.SessionDescription, lSdpChan chan webrtc.Session
 | 
			
		||||
	mux := http.NewServeMux()
 | 
			
		||||
	mux.HandleFunc("/", viewerHandler)
 | 
			
		||||
	mux.Handle("/static/", staticHandler())
 | 
			
		||||
	mux.HandleFunc("/_stats/", statisticsHandler)
 | 
			
		||||
	log.Printf("HTTP server listening on %s", cfg.ListenAddress)
 | 
			
		||||
	log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user