doing initial data population of prometheus so that it doesn't enter zero values on startup

This commit is contained in:
2023-09-26 21:14:33 -04:00
parent 147ef10c01
commit 8f36fc3f15
3 changed files with 54 additions and 20 deletions

13
homepage.html Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Plex Data Exporter</title>
</head>
<body>
<h1>Hello, you have reached the homepage of a prometheus/jsonapi plex exporter!</h1>
<br />
<div>You most likely want to configure prometheus to hit this server at the <b>/metrics</b> route to pull data.</div>
<br />
<div>Or you can use grafana JSONAPI plugin to pull ephemeral text data from <b>/plex-stats</b>.</div>
</body>
</html>

59
main.go
View File

@@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
"text/template"
"time" "time"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@@ -68,6 +69,12 @@ func setupJSONAPI(plexStatsChannel chan *plexStats, settings serverSettings) {
// Create a new router // Create a new router
mux := http.NewServeMux() mux := http.NewServeMux()
tmpl := template.Must(template.ParseFiles("homepage.html"))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, nil)
})
// Create a handler for the `/plex-stats` endpoint // Create a handler for the `/plex-stats` endpoint
mux.HandleFunc("/plex-stats", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/plex-stats", func(w http.ResponseWriter, r *http.Request) {
// Create a new plexStats struct // Create a new plexStats struct
@@ -89,10 +96,16 @@ func setupJSONAPI(plexStatsChannel chan *plexStats, settings serverSettings) {
// Start the server // Start the server
fmt.Println("Starting JSON API Server.....") fmt.Println("Starting JSON API Server.....")
err := http.ListenAndServe(fmt.Sprintf(":%d", settings.jsonPort), mux) http.ListenAndServe(fmt.Sprintf(":%d", settings.jsonPort), mux)
if err != nil {
log.Fatal("Unable to start JSON Server on port: ", settings.jsonPort, " with err: ", err) }
}
func setStats(stats *plexStats) {
plexActiveSessions.Set(stats.CurrentSessions)
plexNumMovies.Set(stats.NumMovies)
plexNumTVShows.Set(stats.NumTV)
plexTranscodeSessions.Set(stats.NumTranscodes)
plexAllSessions.Set(stats.NumAllSessions)
} }
func main() { func main() {
@@ -108,15 +121,11 @@ func main() {
settings := setupServerSettings() settings := setupServerSettings()
// Setup plex client // Setup plex client
pc := setupClient(&settings) pc := setupClient(&settings)
// Our initial gather to ensure we don't send zeros to prometheus
prometheus.MustRegister(plexActiveSessions) stats := pc.gatherAllStats()
prometheus.MustRegister(plexNumMovies) setStats(stats)
prometheus.MustRegister(plexNumTVShows) // Creating a data sharing channel to send same data to our json api server
prometheus.MustRegister(plexTranscodeSessions)
prometheus.MustRegister(plexAllSessions)
plexStatsChannel := make(chan *plexStats) plexStatsChannel := make(chan *plexStats)
go setupJSONAPI(plexStatsChannel, settings) go setupJSONAPI(plexStatsChannel, settings)
go func() { go func() {
@@ -124,17 +133,29 @@ func main() {
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
fmt.Println("Collecting info...") fmt.Println("Collecting info...")
stats := pc.gatherAllStats() stats := pc.gatherAllStats()
plexStatsChannel <- stats setStats(stats)
plexActiveSessions.Set(stats.CurrentSessions) // Kind of an ugly workaround?
plexNumMovies.Set(stats.NumMovies) //Will cause some travel times to be up to 5 seconds as the channel in the mux waits for data to be populated?
plexNumTVShows.Set(stats.NumTV) select {
plexTranscodeSessions.Set(stats.NumTranscodes) case plexStatsChannel <- stats:
plexAllSessions.Set(stats.NumAllSessions) default:
}
} }
}() }()
prometheus.MustRegister(plexActiveSessions)
prometheus.MustRegister(plexNumMovies)
prometheus.MustRegister(plexNumTVShows)
prometheus.MustRegister(plexTranscodeSessions)
prometheus.MustRegister(plexAllSessions)
// m := NewMetrics(reg) // m := NewMetrics(reg)
fmt.Println("Startup of promethus handler complete...")
http.Handle("/metrics", promhttp.Handler()) http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf(":%d", settings.port), nil) err := http.ListenAndServe(fmt.Sprintf(":%d", settings.port), nil)
if err != nil {
log.Fatal("Unable to start the prometheus handler....")
}
} }

View File

@@ -115,7 +115,7 @@ func (pc *PlexClient) getNumTranscodes() *RawPlexModel {
// getServerHistory gets the entire history of plays on the plex server // getServerHistory gets the entire history of plays on the plex server
func (pc *PlexClient) getServerHistory() *RawPlexModel { func (pc *PlexClient) getServerHistory() *RawPlexModel {
result := pc.sendRequest("/status/sessions/history/all", nil) //Hard code library ID for now TODO: Grab from library/sections call result := pc.sendRequest("/status/sessions/history/all", nil)
sessionsAll := RawPlexModel{} sessionsAll := RawPlexModel{}
err := json.Unmarshal(result, &sessionsAll) err := json.Unmarshal(result, &sessionsAll)
if err != nil { if err != nil {