Skip to content

Commit

Permalink
refactor: minor code changes
Browse files Browse the repository at this point in the history
  • Loading branch information
clambin committed Oct 1, 2023
1 parent b833b45 commit d78281c
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 67 deletions.
6 changes: 2 additions & 4 deletions internal/collectors/connectivity/connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
// Collect implements the prometheus.Collector interface
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
start := time.Now()
err := c.ping()

value := 0.0
if err == nil {
var value float64
if err := c.ping(); err == nil {
value = 1.0
}
ch <- prometheus.MustNewConstMetric(upMetric, prometheus.GaugeValue, value)
Expand Down
8 changes: 4 additions & 4 deletions internal/collectors/plex/plex.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

// Collector presents Plex statistics as Prometheus metrics
type Collector struct {
Getter
IPLocator
Plex Getter
url string
transport *httpclient.RoundTripper
logger *slog.Logger
Expand Down Expand Up @@ -46,7 +46,7 @@ func NewCollector(version, url, username, password string) *Collector {
)
l := slog.Default().With("collector", "plex")
return &Collector{
Getter: plex.New(username, password, "github.com/clambin/mediamon", version, url, r),
Plex: plex.New(username, password, "github.com/clambin/mediamon", version, url, r),
IPLocator: iplocator.New(l),
url: url,
transport: r,
Expand All @@ -73,7 +73,7 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
}

func (c *Collector) collectVersion(ch chan<- prometheus.Metric) {
identity, err := c.Getter.GetIdentity(context.Background())
identity, err := c.Plex.GetIdentity(context.Background())
if err != nil {
//ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("mediamon_error","Error getting Plex version", nil, nil),err)
c.logger.Error("failed to collect version", "err", err)
Expand All @@ -84,7 +84,7 @@ func (c *Collector) collectVersion(ch chan<- prometheus.Metric) {
}

func (c *Collector) collectSessionStats(ch chan<- prometheus.Metric) {
sessions, err := c.Getter.GetSessions(context.Background())
sessions, err := c.Plex.GetSessions(context.Background())
if err != nil {
ch <- prometheus.NewInvalidMetric(prometheus.NewDesc("mediamon_error", "Error getting Plex session stats", nil, nil), err)
c.logger.Error("failed to collect session stats", "err", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/collectors/plex/plex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestCollector_Collect(t *testing.T) {
c := plex.NewCollector("1.0", "", "", "")
l := mocks.NewIPLocator(t)
p := mocks.NewGetter(t)
c.Getter = p
c.Plex = p
c.IPLocator = l

l.EXPECT().Locate("1.2.3.4").Return(10.0, 20.0, nil)
Expand Down
20 changes: 10 additions & 10 deletions internal/collectors/transmission/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ type Getter interface {

// Collector presents Transmission statistics as Prometheus metrics
type Collector struct {
Getter
url string
transport *httpclient.RoundTripper
logger *slog.Logger
Transmission Getter
url string
transport *httpclient.RoundTripper
logger *slog.Logger
}

var _ prometheus.Collector = &Collector{}
Expand Down Expand Up @@ -87,10 +87,10 @@ func (s transmissionStats) collect(ch chan<- prometheus.Metric, url string) {
func NewCollector(url string) *Collector {
r := httpclient.NewRoundTripper(httpclient.WithMetrics("mediamon", "", "transmission"))
return &Collector{
Getter: transmission.NewClient(url, r),
url: url,
transport: r,
logger: slog.Default().With(slog.String("collector", "transmission")),
Transmission: transmission.NewClient(url, r),
url: url,
transport: r,
logger: slog.Default().With(slog.String("collector", "transmission")),
}
}

Expand Down Expand Up @@ -127,11 +127,11 @@ func (c *Collector) getStats() (stats transmissionStats, err error) {
}

func (c *Collector) getVersion(ctx context.Context) (string, error) {
params, err := c.Getter.GetSessionParameters(ctx)
params, err := c.Transmission.GetSessionParameters(ctx)
return params.Arguments.Version, err
}

func (c *Collector) getSessionStats(ctx context.Context) (int, int, int, int, error) {
stats, err := c.Getter.GetSessionStatistics(ctx)
stats, err := c.Transmission.GetSessionStatistics(ctx)
return stats.Arguments.ActiveTorrentCount, stats.Arguments.PausedTorrentCount, stats.Arguments.DownloadSpeed, stats.Arguments.UploadSpeed, err
}
2 changes: 1 addition & 1 deletion internal/collectors/transmission/transmission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestCollector_Collect(t *testing.T) {
g.EXPECT().GetSessionParameters(mock.Anything).Return(sessionParameters, nil)

c := transmission.NewCollector("")
c.Getter = g
c.Transmission = g

e := strings.NewReader(`# HELP mediamon_transmission_active_torrent_count Number of active torrents
# TYPE mediamon_transmission_active_torrent_count gauge
Expand Down
18 changes: 9 additions & 9 deletions internal/collectors/xxxarr/scraper/radarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ type RadarrGetter interface {

// RadarrScraper collects Stats from a Radarr instance
type RadarrScraper struct {
Client RadarrGetter
Radarr RadarrGetter
}

// Scrape returns Stats from a Radarr instance
func (s RadarrScraper) Scrape(ctx context.Context) (Stats, error) {
stats := Stats{
URL: s.Client.GetURL(),
URL: s.Radarr.GetURL(),
}

var err error
stats.URL = s.Client.GetURL()
stats.URL = s.Radarr.GetURL()

stats.Version, err = s.getVersion(ctx)

Expand All @@ -53,12 +53,12 @@ func (s RadarrScraper) Scrape(ctx context.Context) (Stats, error) {
}

func (s RadarrScraper) getVersion(ctx context.Context) (string, error) {
systemStatus, err := s.Client.GetSystemStatus(ctx)
systemStatus, err := s.Radarr.GetSystemStatus(ctx)
return systemStatus.Version, err
}

func (s RadarrScraper) getHealth(ctx context.Context) (map[string]int, error) {
health, err := s.Client.GetHealth(ctx)
health, err := s.Radarr.GetHealth(ctx)
if err != nil {
return nil, err
}
Expand All @@ -71,7 +71,7 @@ func (s RadarrScraper) getHealth(ctx context.Context) (map[string]int, error) {
}

func (s RadarrScraper) getCalendar(ctx context.Context) ([]string, error) {
calendar, err := s.Client.GetCalendar(ctx)
calendar, err := s.Radarr.GetCalendar(ctx)
if err != nil {
return nil, err
}
Expand All @@ -85,14 +85,14 @@ func (s RadarrScraper) getCalendar(ctx context.Context) ([]string, error) {
}

func (s RadarrScraper) getQueued(ctx context.Context) ([]QueuedFile, error) {
queued, err := s.Client.GetQueue(ctx)
queued, err := s.Radarr.GetQueue(ctx)
if err != nil {
return nil, err
}
var queuedFiles []QueuedFile
for _, entry := range queued.Records {
var movie xxxarr.RadarrMovieResponse
movie, err = s.Client.GetMovieByID(ctx, entry.MovieID)
movie, err = s.Radarr.GetMovieByID(ctx, entry.MovieID)
if err != nil {
return nil, err
}
Expand All @@ -107,7 +107,7 @@ func (s RadarrScraper) getQueued(ctx context.Context) ([]QueuedFile, error) {
}

func (s RadarrScraper) getMonitored(ctx context.Context) (int, int, error) {
movies, err := s.Client.GetMovies(ctx)
movies, err := s.Radarr.GetMovies(ctx)
if err != nil {
return 0, 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/collectors/xxxarr/scraper/radarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestRadarrScraper_Scrape(t *testing.T) {
c := mocks.NewRadarrGetter(t)
u := scraper.RadarrScraper{Client: c}
u := scraper.RadarrScraper{Radarr: c}

c.EXPECT().GetURL().Return("http://localhost:8080")
c.EXPECT().GetSystemStatus(mock.Anything).Return(radarrSystemStatus, nil)
Expand Down
18 changes: 9 additions & 9 deletions internal/collectors/xxxarr/scraper/sonarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type SonarrGetter interface {

// SonarrScraper collects Stats from a Sonarr instance
type SonarrScraper struct {
Client SonarrGetter
Sonarr SonarrGetter
}

// Scrape returns Stats from a Sonarr instance
func (s SonarrScraper) Scrape(ctx context.Context) (Stats, error) {
stats := Stats{
URL: s.Client.GetURL(),
URL: s.Sonarr.GetURL(),
}

var err error
Expand All @@ -49,7 +49,7 @@ func (s SonarrScraper) Scrape(ctx context.Context) (Stats, error) {
}

func (s SonarrScraper) getVersion(ctx context.Context) (string, error) {
systemStatus, err := s.Client.GetSystemStatus(ctx)
systemStatus, err := s.Sonarr.GetSystemStatus(ctx)
if err != nil {
return "", err
}
Expand All @@ -58,7 +58,7 @@ func (s SonarrScraper) getVersion(ctx context.Context) (string, error) {

func (s SonarrScraper) getHealth(ctx context.Context) (map[string]int, error) {
var healthEntries map[string]int
health, err := s.Client.GetHealth(ctx)
health, err := s.Sonarr.GetHealth(ctx)
if err == nil {
healthEntries = make(map[string]int)
for _, entry := range health {
Expand All @@ -70,7 +70,7 @@ func (s SonarrScraper) getHealth(ctx context.Context) (map[string]int, error) {
}

func (s SonarrScraper) getCalendar(ctx context.Context) ([]string, error) {
calendar, err := s.Client.GetCalendar(ctx)
calendar, err := s.Sonarr.GetCalendar(ctx)
if err != nil {
return nil, err
}
Expand All @@ -96,14 +96,14 @@ func (s SonarrScraper) getCalendar(ctx context.Context) ([]string, error) {
}

func (s SonarrScraper) getQueued(ctx context.Context) ([]QueuedFile, error) {
queued, err := s.Client.GetQueue(ctx)
queued, err := s.Sonarr.GetQueue(ctx)
if err != nil {
return nil, err
}
var entries []QueuedFile
for _, entry := range queued.Records {
var episode xxxarr.SonarrEpisodeResponse
episode, err = s.Client.GetEpisodeByID(ctx, entry.EpisodeID)
episode, err = s.Sonarr.GetEpisodeByID(ctx, entry.EpisodeID)
if err != nil {
return nil, fmt.Errorf("GetEpisideByID: %w", err)
}
Expand All @@ -120,7 +120,7 @@ func (s SonarrScraper) getQueued(ctx context.Context) ([]QueuedFile, error) {

func (s SonarrScraper) getMonitored(ctx context.Context) (int, int, error) {
var monitored, unmonitored int
movies, err := s.Client.GetSeries(ctx)
movies, err := s.Sonarr.GetSeries(ctx)
if err == nil {
for _, entry := range movies {
if entry.Monitored {
Expand All @@ -135,7 +135,7 @@ func (s SonarrScraper) getMonitored(ctx context.Context) (int, int, error) {

func (s SonarrScraper) getShowName(ctx context.Context, id int) (string, error) {
var title string
show, err := s.Client.GetSeriesByID(ctx, id)
show, err := s.Sonarr.GetSeriesByID(ctx, id)
if err == nil {
title = show.Title
}
Expand Down
2 changes: 1 addition & 1 deletion internal/collectors/xxxarr/scraper/sonarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func TestSonarrScraper_Scrape(t *testing.T) {
c := mocks.NewSonarrGetter(t)
u := scraper.SonarrScraper{Client: c}
u := scraper.SonarrScraper{Sonarr: c}

c.EXPECT().GetURL().Return("http://localhost:8080")
c.EXPECT().GetSystemStatus(mock.Anything).Return(sonarrSystemStatus, nil)
Expand Down
11 changes: 2 additions & 9 deletions internal/collectors/xxxarr/xxxarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewRadarrCollector(url, apiKey string) *Collector {
)

return &Collector{
Scraper: scraper.RadarrScraper{Client: xxxarr.NewRadarrClient(url, apiKey, r)},
Scraper: scraper.RadarrScraper{Radarr: xxxarr.NewRadarrClient(url, apiKey, r)},
application: "radarr",
metrics: createMetrics("radarr", url),
transport: r,
Expand All @@ -78,7 +78,7 @@ func NewSonarrCollector(url, apiKey string) *Collector {
)

return &Collector{
Scraper: scraper.SonarrScraper{Client: xxxarr.NewSonarrClient(url, apiKey, r)},
Scraper: scraper.SonarrScraper{Sonarr: xxxarr.NewSonarrClient(url, apiKey, r)},
application: "sonarr",
metrics: createMetrics("sonarr", url),
transport: r,
Expand All @@ -96,13 +96,6 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) {

// Collect implements the prometheus.Collector interface
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
// TODO: panic due to scrape was due to a bug in httpclient Cache. Safe to remove now?
defer func() {
if err := recover(); err != nil {
c.logger.Warn("scrape panicked", "err", err)
}
}()

start := time.Now()
stats, err := c.Scraper.Scrape(context.Background())
if err != nil {
Expand Down
18 changes: 0 additions & 18 deletions internal/collectors/xxxarr/xxxarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package xxxarr_test

import (
"bytes"
"context"
"errors"
"github.com/clambin/mediamon/v2/internal/collectors/xxxarr"
mocks2 "github.com/clambin/mediamon/v2/internal/collectors/xxxarr/mocks"
"github.com/clambin/mediamon/v2/internal/collectors/xxxarr/scraper"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
Expand Down Expand Up @@ -142,19 +140,3 @@ mediamon_xxxarr_version{application="radarr",url="",version="foo"} 1
})
}
}

func TestCollector_Collect_Panic(t *testing.T) {
c := xxxarr.NewRadarrCollector("", "")
c.Scraper = &panickingScraper{}

assert.NotPanics(t, func() {
ch := make(chan prometheus.Metric)
c.Collect(ch)
})
}

type panickingScraper struct{}

func (p *panickingScraper) Scrape(_ context.Context) (scraper.Stats, error) {
panic("panic")
}

0 comments on commit d78281c

Please sign in to comment.