Skip to content

Commit

Permalink
feat: improve trackers info (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal authored Oct 20, 2024
1 parent 398185d commit 36361ff
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 15 deletions.
23 changes: 23 additions & 0 deletions src/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
51 changes: 51 additions & 0 deletions src/app/app_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
1 change: 1 addition & 0 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
53 changes: 39 additions & 14 deletions src/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,28 +167,53 @@ 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) {
tier, err := strconv.Atoi(string(tracker.Tier))
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)
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion src/qbit/qbit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down

0 comments on commit 36361ff

Please sign in to comment.