diff --git a/src/app/app.go b/src/app/app.go index a917537..8bdb235 100644 --- a/src/app/app.go +++ b/src/app/app.go @@ -81,3 +81,26 @@ func LoadEnv() { func GetPasswordMasked() string { return strings.Repeat("*", len(Password)) } + +func GetFeaturesEnabled() string { + features := "" + + addComma := func() { + if features != "" { + features += ", " + } + } + + if EnableHighCardinality { + features += "High cardinality" + } + + if !DisableTracker { + addComma() + features += "Trackers" + } + + features = "[" + features + "]" + + return features +} diff --git a/src/app/app_test.go b/src/app/app_test.go new file mode 100644 index 0000000..46e8ca7 --- /dev/null +++ b/src/app/app_test.go @@ -0,0 +1,51 @@ +package app + +import ( + "testing" +) + +func TestGetFeaturesEnabled(t *testing.T) { + tests := []struct { + name string + enableHighCardinality bool + disableTracker bool + expectedOutput string + }{ + { + name: "Both disabled", + enableHighCardinality: false, + disableTracker: true, + expectedOutput: "[]", + }, + { + name: "Only High Cardinality enabled", + enableHighCardinality: true, + disableTracker: true, + expectedOutput: "[High cardinality]", + }, + { + name: "Only Trackers enabled", + enableHighCardinality: false, + disableTracker: false, + expectedOutput: "[Trackers]", + }, + { + name: "Both High Cardinality and Trackers enabled", + enableHighCardinality: true, + disableTracker: false, + expectedOutput: "[High cardinality, Trackers]", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + EnableHighCardinality = test.enableHighCardinality + DisableTracker = test.disableTracker + + result := GetFeaturesEnabled() + if result != test.expectedOutput { + t.Errorf("expected %s, got %s", test.expectedOutput, result) + } + }) + } +} diff --git a/src/main.go b/src/main.go index 66d85d6..ee54aa6 100644 --- a/src/main.go +++ b/src/main.go @@ -31,6 +31,7 @@ func main() { logger.Log.Info("qbittorrent URL: " + app.BaseUrl) logger.Log.Info("username: " + app.Username) logger.Log.Info("password: " + app.GetPasswordMasked()) + logger.Log.Info("Features enabled: " + app.GetFeaturesEnabled()) logger.Log.Info("Started") isTrackerEnabled := "enabled" if app.DisableTracker { diff --git a/src/prometheus/prometheus.go b/src/prometheus/prometheus.go index 9ef2779..009348d 100644 --- a/src/prometheus/prometheus.go +++ b/src/prometheus/prometheus.go @@ -167,12 +167,26 @@ func Trackers(result []*API.Trackers, r *prometheus.Registry) { logger.Log.Trace("No tracker") return } - qbittorrent_tracker_info := prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Name: "qbittorrent_tracker_info", - Help: "All info for trackers", - }, []string{"message", "downloaded", "leeches", "peers", "seeders", "status", "tier", "url"}) - r.MustRegister(qbittorrent_tracker_info) + label_name := "url" + + metrics := map[string]*prometheus.GaugeVec{ + "qbittorrent_tracker_downloaded": newGaugeVec("qbittorrent_tracker_downloaded", "The current number of completed downloads for each trackers", label_name), + "qbittorrent_tracker_leeches": newGaugeVec("qbittorrent_tracker_leeches", "The current number of leechers for each trackers", label_name), + "qbittorrent_tracker_peers": newGaugeVec("qbittorrent_tracker_peers", "The current number of peers for each trackers", label_name), + "qbittorrent_tracker_seeders": newGaugeVec("qbittorrent_tracker_seeders", "The current number of seeders for each trackers", label_name), + "qbittorrent_tracker_status": newGaugeVec("qbittorrent_tracker_status", "The current status of each trackers", label_name), + "qbittorrent_tracker_tier": newGaugeVec("qbittorrent_tracker_tier", "The current tracker priority tier of each trackers", label_name), + } + + if app.EnableHighCardinality { + metrics["qbittorrent_tracker_info"] = newGaugeVec("qbittorrent_tracker_info", "All info for trackers", + "message", "downloaded", "leeches", "peers", "seeders", "status", "tier", "url") + } + + for _, metric := range metrics { + r.MustRegister(metric) + } for _, listOfTracker := range result { for _, tracker := range *listOfTracker { if isValidURL(tracker.URL) { @@ -180,15 +194,26 @@ func Trackers(result []*API.Trackers, r *prometheus.Registry) { if err != nil { tier = 0 } - qbittorrent_tracker_info.With(prometheus.Labels{ - "message": tracker.Message, - "downloaded": strconv.Itoa(tracker.NumDownloaded), - "leeches": strconv.Itoa(tracker.NumLeeches), - "peers": strconv.Itoa(tracker.NumPeers), - "seeders": strconv.Itoa(int(tracker.NumSeeds)), - "status": strconv.Itoa((tracker.Status)), - "tier": strconv.Itoa(tier), - "url": tracker.URL}).Set(1) + label := prometheus.Labels{label_name: tracker.URL} + metrics["qbittorrent_tracker_downloaded"].With(label).Set((float64(tracker.NumDownloaded))) + metrics["qbittorrent_tracker_leeches"].With(label).Set((float64(tracker.NumLeeches))) + metrics["qbittorrent_tracker_seeders"].With(label).Set((float64(tracker.NumSeeds))) + metrics["qbittorrent_tracker_peers"].With(label).Set((float64(tracker.NumPeers))) + metrics["qbittorrent_tracker_status"].With(label).Set((float64(tracker.Status))) + + if app.EnableHighCardinality { + qbittorrent_tracker_info_labels := prometheus.Labels{ + "message": tracker.Message, + "downloaded": strconv.Itoa(tracker.NumDownloaded), + "leeches": strconv.Itoa(tracker.NumLeeches), + "peers": strconv.Itoa(tracker.NumPeers), + "seeders": strconv.Itoa(int(tracker.NumSeeds)), + "status": strconv.Itoa((tracker.Status)), + "tier": strconv.Itoa(tier), + "url": tracker.URL} + metrics["qbittorrent_tracker_info"].With(qbittorrent_tracker_info_labels).Set(1) + } + } } diff --git a/src/qbit/qbit.go b/src/qbit/qbit.go index 1c380ae..84219aa 100644 --- a/src/qbit/qbit.go +++ b/src/qbit/qbit.go @@ -104,7 +104,7 @@ func getData(r *prometheus.Registry, data Data, goroutine bool, c chan func() (b result := new(API.Info) if handleUnmarshal(result, body) { prom.Torrent(result, r) - if !app.DisableTracker && app.EnableHighCardinality { + if !app.DisableTracker { getTrackers(result, r) } }