Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Prowlarr #197

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- master
- http
- prowlarr
paths-ignore:
- 'assets/**'

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches-ignore:
- master
- http
- prowlarr
pull_request_target:

jobs:
Expand Down
3 changes: 3 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ packages:
github.com/clambin/mediamon/v2/internal/collectors/xxxarr:
interfaces:
Client:
github.com/clambin/mediamon/v2/internal/collectors/prowlarr:
interfaces:
Client:
github.com/clambin/mediamon/v2/internal/collectors/xxxarr/clients:
interfaces:
RadarrClient:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/clambin/go-common/charmer v0.2.0
github.com/clambin/go-common/http v0.5.0
github.com/clambin/go-common/set v0.4.3
github.com/clambin/mediaclients v0.4.2
github.com/clambin/mediaclients v0.5.0
github.com/prometheus/client_golang v1.19.1
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/clambin/go-common/set v0.4.3 h1:Sm9lkAJsh82j40RDpfQIziHyHjwr07+KsQF6v
github.com/clambin/go-common/set v0.4.3/go.mod h1:Q5GpBoM7B7abNV2Wzys+wQMInBHMoHyh/h0Cn2OmY4A=
github.com/clambin/go-common/testutils v0.1.0 h1:/nGWaOCIhW+Ew1c2NU7GLY/YPb8dp9SV8+MTgWksAgk=
github.com/clambin/go-common/testutils v0.1.0/go.mod h1:bV0j8D4zhNkleCeluFKLBeLQ0L/dqkxbaR/joLn8kzg=
github.com/clambin/mediaclients v0.4.2 h1:9d46sKGTJRTE4gHXeRthfHCsGZmCbJtPZjeCS4hOcSc=
github.com/clambin/mediaclients v0.4.2/go.mod h1:EAjWM31ZMHqV1e92r+n9FWEI6A93vBSEyEfuLnRFpm8=
github.com/clambin/mediaclients v0.5.0 h1:71G5qtAxmcM07JooKv/dWAztCyxLw1pzaG4QFMTjqcs=
github.com/clambin/mediaclients v0.5.0/go.mod h1:EAjWM31ZMHqV1e92r+n9FWEI6A93vBSEyEfuLnRFpm8=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
7 changes: 7 additions & 0 deletions internal/cmd/mediamon/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/clambin/mediamon/v2/internal/collectors/bandwidth"
"github.com/clambin/mediamon/v2/internal/collectors/connectivity"
"github.com/clambin/mediamon/v2/internal/collectors/plex"
"github.com/clambin/mediamon/v2/internal/collectors/prowlarr"
"github.com/clambin/mediamon/v2/internal/collectors/transmission"
"github.com/clambin/mediamon/v2/internal/collectors/xxxarr"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -37,6 +38,12 @@ var constructors = map[string]constructor{
return xxxarr.NewRadarrCollector(url, v.GetString("radarr.apikey"), logger), nil
},
},
"prowlarr.url": {
name: "prowlarr",
make: func(url, _ string, v *viper.Viper, logger *slog.Logger) (prometheus.Collector, error) {
return prowlarr.New(url, v.GetString("prowlarr.apikey"), logger), nil
},
},
"plex.url": {
name: "plex",
make: func(url, version string, v *viper.Viper, logger *slog.Logger) (prometheus.Collector, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/mediamon/mediamon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestExecute(t *testing.T) {
assert.Eventually(t, func() bool {
_, err := http.Get("http://127.0.0.1:9090/metrics")
return err == nil
}, time.Second, time.Millisecond*100)
}, 5*time.Second, time.Millisecond*100)

assert.NoError(t, testutil.GatherAndCompare(
prometheus.DefaultGatherer,
Expand Down
51 changes: 51 additions & 0 deletions internal/collectors/prowlarr/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package prowlarr

import "github.com/prometheus/client_golang/prometheus"

func newMetrics(url string) map[string]*prometheus.Desc {
constLabels := prometheus.Labels{"application": "prowlarr", "url": url}
return map[string]*prometheus.Desc{
"indexerResponseTime": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "indexer_response_time"),
"Average response time in seconds",
[]string{"indexer"},
constLabels,
),
"indexerQueryTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "indexer_query_total"),
"Total number of queries to this indexer",
[]string{"indexer"},
constLabels,
),
"indexerGrabTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "indexer_grab_total"),
"Total number of grabs from this indexer",
[]string{"indexer"},
constLabels,
),
"indexerFailedQueryTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "indexer_failed_query_total"),
"Total number of failed queries to this indexer",
[]string{"indexer"},
constLabels,
),
"indexerFailedGrabTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "indexer_failed_grab_total"),
"Total number of failed grabs from this indexer",
[]string{"indexer"},
constLabels,
),
"userAgentQueryTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "user_agent_query_total"),
"Total number of queries by user agent",
[]string{"user_agent"},
constLabels,
),
"userAgentGrabTotal": prometheus.NewDesc(
prometheus.BuildFQName("mediamon", "prowlarr", "user_agent_grab_total"),
"Total number of grabs by user agent",
[]string{"user_agent"},
constLabels,
),
}
}
94 changes: 94 additions & 0 deletions internal/collectors/prowlarr/mocks/Client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions internal/collectors/prowlarr/prowlarr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package prowlarr

import (
"context"
"github.com/clambin/go-common/http/metrics"
"github.com/clambin/go-common/http/roundtripper"
"github.com/clambin/mediaclients/xxxarr"
collectorbreaker "github.com/clambin/mediamon/v2/pkg/collector-breaker"
"github.com/prometheus/client_golang/prometheus"
"log/slog"
"time"
)

type Collector struct {
client Client
metrics map[string]*prometheus.Desc
tpMetrics metrics.RequestMetrics
cacheMetrics roundtripper.CacheMetrics
logger *slog.Logger
}

type Client interface {
GetIndexStats(context.Context) (xxxarr.ProwlarrIndexersStats, error)
}

func New(url, apiKey string, logger *slog.Logger) *collectorbreaker.CBCollector {
tpMetrics := metrics.NewRequestMetrics(metrics.Options{
Namespace: "mediamon",
ConstLabels: prometheus.Labels{"application": "prowlarr"},
})
cacheMetrics := roundtripper.NewCacheMetrics(roundtripper.CacheMetricsOptions{
Namespace: "mediamon",
ConstLabels: prometheus.Labels{"application": "prowlarr"},
})

r := roundtripper.New(
roundtripper.WithCache(roundtripper.CacheOptions{
DefaultExpiration: 15 * time.Minute,
CleanupInterval: time.Hour,
CacheMetrics: cacheMetrics,
}),
roundtripper.WithRequestMetrics(tpMetrics),
)
c := Collector{
client: xxxarr.NewProwlarrClient(url, apiKey, r),
metrics: newMetrics(url),
tpMetrics: tpMetrics,
cacheMetrics: cacheMetrics,
logger: logger,
}
return collectorbreaker.New("prowlarr", &c, logger)
}

func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
for _, m := range c.metrics {
ch <- m
}
c.tpMetrics.Describe(ch)
c.cacheMetrics.Describe(ch)
}

func (c *Collector) CollectE(ch chan<- prometheus.Metric) error {
stats, err := c.client.GetIndexStats(context.Background())
if err == nil {
for _, indexer := range stats.Indexers {
name := indexer.IndexerName
//c.logger.Debug("indexer found", "indexer", name, "queries", indexer.NumberOfQueries, "grabs", indexer.NumberOfGrabs)
ch <- prometheus.MustNewConstMetric(c.metrics["indexerResponseTime"], prometheus.GaugeValue, time.Duration(indexer.AverageResponseTime).Seconds(), name)
ch <- prometheus.MustNewConstMetric(c.metrics["indexerQueryTotal"], prometheus.CounterValue, float64(indexer.NumberOfQueries), name)
ch <- prometheus.MustNewConstMetric(c.metrics["indexerFailedQueryTotal"], prometheus.CounterValue, float64(indexer.NumberOfFailedQueries), name)
ch <- prometheus.MustNewConstMetric(c.metrics["indexerGrabTotal"], prometheus.CounterValue, float64(indexer.NumberOfGrabs), name)
ch <- prometheus.MustNewConstMetric(c.metrics["indexerFailedGrabTotal"], prometheus.CounterValue, float64(indexer.NumberOfFailedGrabs), name)
}
for _, userAgent := range stats.UserAgents {
agent := userAgent.UserAgent
//c.logger.Debug("user agent found", "agent", agent, "queries", userAgent.NumberOfQueries, "grabs", userAgent.NumberOfGrabs)
ch <- prometheus.MustNewConstMetric(c.metrics["userAgentQueryTotal"], prometheus.CounterValue, float64(userAgent.NumberOfQueries), agent)
ch <- prometheus.MustNewConstMetric(c.metrics["userAgentGrabTotal"], prometheus.CounterValue, float64(userAgent.NumberOfGrabs), agent)
}
}
c.tpMetrics.Collect(ch)
c.cacheMetrics.Collect(ch)
return err
}
74 changes: 74 additions & 0 deletions internal/collectors/prowlarr/prowlarr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package prowlarr

import (
"context"
"github.com/clambin/mediaclients/xxxarr"
"github.com/clambin/mediamon/v2/internal/collectors/prowlarr/mocks"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"log/slog"
"strings"
"testing"
"time"
)

func TestCollector(t *testing.T) {
prowlarr := mocks.NewClient(t)
prowlarr.EXPECT().GetIndexStats(context.Background()).Return(xxxarr.ProwlarrIndexersStats{
Indexers: []xxxarr.ProwlarrIndexerStats{{
IndexerId: 1,
IndexerName: "foo",
AverageResponseTime: xxxarr.ProwlarrResponseTime(100 * time.Millisecond),
NumberOfQueries: 10,
NumberOfFailedQueries: 1,
NumberOfGrabs: 2,
NumberOfFailedGrabs: 1,
}},
UserAgents: []xxxarr.ProwlarrUserAgentStats{{
UserAgent: "foo",
NumberOfQueries: 10,
NumberOfGrabs: 1,
}},
}, nil)

c := New("http://localhost", "", slog.Default())
c.Collector.(*Collector).client = prowlarr

assert.NoError(t, testutil.CollectAndCompare(c, strings.NewReader(`
# HELP mediamon_prowlarr_indexer_failed_grab_total Total number of failed grabs from this indexer
# TYPE mediamon_prowlarr_indexer_failed_grab_total counter
mediamon_prowlarr_indexer_failed_grab_total{application="prowlarr",indexer="foo",url="http://localhost"} 1

# HELP mediamon_prowlarr_indexer_failed_query_total Total number of failed queries to this indexer
# TYPE mediamon_prowlarr_indexer_failed_query_total counter
mediamon_prowlarr_indexer_failed_query_total{application="prowlarr",indexer="foo",url="http://localhost"} 1

# HELP mediamon_prowlarr_indexer_grab_total Total number of grabs from this indexer
# TYPE mediamon_prowlarr_indexer_grab_total counter
mediamon_prowlarr_indexer_grab_total{application="prowlarr",indexer="foo",url="http://localhost"} 2

# HELP mediamon_prowlarr_indexer_query_total Total number of queries to this indexer
# TYPE mediamon_prowlarr_indexer_query_total counter
mediamon_prowlarr_indexer_query_total{application="prowlarr",indexer="foo",url="http://localhost"} 10

# HELP mediamon_prowlarr_indexer_response_time Average response time in seconds
# TYPE mediamon_prowlarr_indexer_response_time gauge
mediamon_prowlarr_indexer_response_time{application="prowlarr",indexer="foo",url="http://localhost"} 0.1

# HELP mediamon_prowlarr_user_agent_grab_total Total number of grabs by user agent
# TYPE mediamon_prowlarr_user_agent_grab_total counter
mediamon_prowlarr_user_agent_grab_total{application="prowlarr",url="http://localhost",user_agent="foo"} 1

# HELP mediamon_prowlarr_user_agent_query_total Total number of queries by user agent
# TYPE mediamon_prowlarr_user_agent_query_total counter
mediamon_prowlarr_user_agent_query_total{application="prowlarr",url="http://localhost",user_agent="foo"} 10
`),
"mediamon_prowlarr_indexer_grab_total",
"mediamon_prowlarr_indexer_query_total",
"mediamon_prowlarr_indexer_failed_grab_total",
"mediamon_prowlarr_indexer_failed_query_total",
"mediamon_prowlarr_indexer_response_time",
"mediamon_prowlarr_user_agent_query_total",
"mediamon_prowlarr_user_agent_grab_total",
))
}