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