Skip to content

Commit

Permalink
feat: measure Plex session bandwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Nov 26, 2023
1 parent efc40e6 commit 5e8f917
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 15 additions & 1 deletion internal/collectors/plex/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ import (
var (
sessionMetric = prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "plex", "session_count"),
"Active Plex session",
"Active Plex session progress",
[]string{"url", "user", "player", "title", "mode", "location", "address", "lon", "lat", "videoCodec", "audioCodec"},
nil,
)

bandwidthMetric = prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "plex", "session_bandwidth"),
"Active Plex session Bandwidth usage (in kbps)",
[]string{"url", "user", "player", "title", "mode", "location", "address", "lon", "lat", "videoCodec", "audioCodec"},
nil,
)
Expand Down Expand Up @@ -49,6 +56,7 @@ type sessionGetter interface {

func (c sessionCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- sessionMetric
ch <- bandwidthMetric
ch <- transcodersMetric
ch <- speedMetric
}
Expand All @@ -73,6 +81,10 @@ func (c sessionCollector) Collect(ch chan<- prometheus.Metric) {
c.url, stats.user, stats.player, stats.title, stats.videoMode, stats.location, stats.address, lon, lat, stats.videoCodec, stats.audioCodec,
)

ch <- prometheus.MustNewConstMetric(bandwidthMetric, prometheus.GaugeValue, float64(stats.bandwidth),
c.url, stats.user, stats.player, stats.title, stats.videoMode, stats.location, stats.address, lon, lat, stats.videoCodec, stats.audioCodec,
)

if stats.videoMode == "transcode" {
if stats.throttled {
throttled++
Expand Down Expand Up @@ -110,6 +122,7 @@ type plexSession struct {
title string
address string
progress float64
bandwidth int
videoMode string
throttled bool
speed float64
Expand Down Expand Up @@ -139,6 +152,7 @@ func parseSessions(sessions []plex.Session) map[string]plexSession {
title: session.GetTitle(),
address: session.Player.Address,
progress: progress,
bandwidth: session.Session.Bandwidth,
videoMode: session.GetVideoMode(),
throttled: session.TranscodeSession.Throttled,
speed: session.TranscodeSession.Speed,
Expand Down
10 changes: 8 additions & 2 deletions internal/collectors/plex/sessions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func TestSessionsCollector_Collector(t *testing.T) {
Session: plex.SessionStats{ID: "1", Location: "lan"},
},
want: `
# HELP mediamon_plex_session_count Active Plex session
# HELP mediamon_plex_session_bandwidth Active Plex session Bandwidth usage (in kbps)
# TYPE mediamon_plex_session_bandwidth gauge
mediamon_plex_session_bandwidth{address="192.168.0.1",audioCodec="aac",lat="",location="lan",lon="",mode="directplay",player="Plex Web",title="foo",url="http://localhost:8080",user="bar",videoCodec="hvec"} 0
# HELP mediamon_plex_session_count Active Plex session progress
# TYPE mediamon_plex_session_count gauge
mediamon_plex_session_count{address="192.168.0.1",audioCodec="aac",lat="",location="lan",lon="",mode="directplay",player="Plex Web",title="foo",url="http://localhost:8080",user="bar",videoCodec="hvec"} 0.5
`,
Expand All @@ -58,7 +61,10 @@ mediamon_plex_session_count{address="192.168.0.1",audioCodec="aac",lat="",locati
},
},
want: `
# HELP mediamon_plex_session_count Active Plex session
# HELP mediamon_plex_session_bandwidth Active Plex session Bandwidth usage (in kbps)
# TYPE mediamon_plex_session_bandwidth gauge
mediamon_plex_session_bandwidth{address="1.2.3.4",audioCodec="aac",lat="20.00",location="wan",lon="10.00",mode="transcode",player="Plex Web",title="foo - S01E10 - bar",url="http://localhost:8080",user="bar",videoCodec="hvec"} 0
# HELP mediamon_plex_session_count Active Plex session progress
# TYPE mediamon_plex_session_count gauge
mediamon_plex_session_count{address="1.2.3.4",audioCodec="aac",lat="20.00",location="wan",lon="10.00",mode="transcode",player="Plex Web",title="foo - S01E10 - bar",url="http://localhost:8080",user="bar",videoCodec="hvec"} 0.75
# HELP mediamon_plex_transcoder_count Video transcode session
Expand Down

0 comments on commit 5e8f917

Please sign in to comment.