ghostream/internal/monitoring/monitoring.go

51 lines
1.4 KiB
Go
Raw Permalink Normal View History

2020-10-09 20:36:02 +00:00
// Package monitoring serves Prometheus monitoring endpoints
2020-09-21 19:33:32 +00:00
package monitoring
import (
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
2020-09-22 09:42:57 +00:00
// Options holds web package configuration
type Options struct {
2020-10-09 20:06:30 +00:00
Enabled bool
2020-09-22 09:42:57 +00:00
ListenAddress string
}
2020-09-21 19:33:32 +00:00
var (
2020-09-24 09:24:13 +00:00
// WebViewerServed is the total amount of viewer page served
WebViewerServed = promauto.NewCounter(prometheus.CounterOpts{
Name: "ghostream_web_viewer_served_total",
2020-09-21 19:33:32 +00:00
Help: "The total amount of viewer served",
})
2020-09-24 09:24:13 +00:00
// WebSessions is the total amount of WebRTC session exchange
WebSessions = promauto.NewCounter(prometheus.CounterOpts{
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",
})
2020-09-21 19:33:32 +00:00
)
2020-09-24 09:24:13 +00:00
// Serve monitoring server that expose prometheus metrics
func Serve(cfg *Options) {
2020-10-09 20:06:30 +00:00
if !cfg.Enabled {
// Monitoring is not enabled, ignore
return
}
2020-09-21 19:33:32 +00:00
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
2020-09-22 09:42:57 +00:00
log.Printf("Monitoring HTTP server listening on %s", cfg.ListenAddress)
log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
2020-09-21 19:33:32 +00:00
}