Skip to content

Commit

Permalink
feat: add label with hash experimental feature
Browse files Browse the repository at this point in the history
  • Loading branch information
martabal committed Oct 27, 2024
1 parent 5f745ca commit 3626b06
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 113 deletions.
8 changes: 7 additions & 1 deletion .env.exemple
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ QBITTORRENT_BASE_URL=
QBITTORRENT_USERNAME=
QBITTORRENT_PASSWORD=
EXPORTER_PORT=
LOG_LEVEL=INFO
LOG_LEVEL=
QBITTORRENT_TIMEOUT=

## features
DISABLE_TRACKER=false
ENABLE_HIGH_CARDINALITY=false

## experimental features
ENABLE_LABEL_WITH_HASH=false
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,18 @@ Docker compressed size is ~10 MB.

### Environment variables

| Parameters | Function | Default Value |
| :--------------------------: | ---------------------------------------------------------------------------------------------------------------------- | ----------------------- |
| `-p 8090` | Webservice port | |
| `-e QBITTORRENT_USERNAME` | qBittorrent username | `admin` |
| `-e QBITTORRENT_PASSWORD` | qBittorrent password | `adminadmin` |
| `-e QBITTORRENT_BASE_URL` | qBittorrent base URL | `http://localhost:8090` |
| `-e QBITTORRENT_TIMEOUT` | duration before ending a request to qBittorrent | `30` |
| `-e EXPORTER_PORT` | qbittorrent export port (optional) | `8090` |
| `-e DISABLE_TRACKER` | get tracker infos, which needs an API request for each tracker (require `ENABLE_HIGH_CARDINALITY` to be set to `true`) | `false` |
| `-e LOG_LEVEL` | App log level (`DEBUG`, `INFO`, `WARN` and `ERROR`) | `INFO` |
| `-e ENABLE_HIGH_CARDINALITY` | enable high cardinality metric (`qbittorrent_torrent_info` and `qbittorrent_tracker_info` ) | `false` |
| Parameters | Function | Default Value |
| :-------------------------------: | ------------------------------------------------------------------------------------------ | ----------------------- |
| `-p 8090` | Webservice port | |
| `-e QBITTORRENT_USERNAME` | qBittorrent username | `admin` |
| `-e QBITTORRENT_PASSWORD` | qBittorrent password | `adminadmin` |
| `-e QBITTORRENT_BASE_URL` | qBittorrent base URL | `http://localhost:8090` |
| `-e QBITTORRENT_TIMEOUT` | duration before ending a request to qBittorrent | `30` |
| `-e EXPORTER_PORT` | qbittorrent export port (optional) | `8090` |
| `-e LOG_LEVEL` | App log level (`DEBUG`, `INFO`, `WARN` and `ERROR`) | `INFO` |
| `-e ENABLE_TRACKER` | get tracker infos, | `true` |
| `-e ENABLE_HIGH_CARDINALITY` | enable high cardinality metric (`qbittorrent_torrent_info` and `qbittorrent_tracker_info`) | `false` |
| `-e ENABLE_LABEL_WITH_HASH` | **[EXPERIMENTAL]** add the torrent hash to the `qbittorrent_torrent_*` metrics label | `false` |

### Arguments

Expand Down
57 changes: 40 additions & 17 deletions src/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,49 @@ import (
)

var (
QBittorrentTimeout time.Duration
Port int
ShouldShowError bool
DisableTracker bool
LogLevel string
BaseUrl string
Cookie string
Username string
Password string
EnableHighCardinality bool
QBittorrentTimeout time.Duration
Port int
ShouldShowError bool
LogLevel string
BaseUrl string
Cookie string
Username string
Password string
ExperimentalFeature ExperimentalFeatures
Feature Features
UsingEnvFile bool
)

func SetVar(port int, disableTracker bool, loglevel string, baseUrl string, username string, password string, qBittorrentTimeout int, enableHighCardinality bool) {
type ExperimentalFeatures struct {
EnableLabelWithHash bool
}

type Features struct {
EnableHighCardinality bool
EnableTracker bool
}

func SetVar(port int, enableTracker bool, loglevel string, baseUrl string, username string, password string, qBittorrentTimeout int, enableHighCardinality bool, enableLabelWithHash bool) {
Port = port
ShouldShowError = true
DisableTracker = disableTracker
Feature.EnableTracker = enableTracker
LogLevel = loglevel
BaseUrl = baseUrl
Username = username
Password = password
QBittorrentTimeout = time.Duration(qBittorrentTimeout)
EnableHighCardinality = enableHighCardinality
Feature.EnableHighCardinality = enableHighCardinality
ExperimentalFeature.EnableLabelWithHash = enableLabelWithHash
}

func LoadEnv() {
var envfile bool
flag.BoolVar(&envfile, "e", false, "Use .env file")
flag.Parse()
_, err := os.Stat(".env")
UsingEnvFile = false
if !os.IsNotExist(err) && !envfile {
UsingEnvFile = true
err := godotenv.Load(".env")
if err != nil {
errormessage := "Error loading .env file:" + err.Error()
Expand All @@ -56,8 +69,9 @@ func LoadEnv() {
qbitURL := strings.TrimSuffix(getEnv(defaultBaseUrl), "/")
exporterPortEnv := getEnv(defaultPort)
timeoutDurationEnv := getEnv(defaultTimeout)
disableTracker := getEnv(defaultDisableTracker)
enableTracker := getEnv(defaultDisableTracker)
enableHighCardinality := getEnv(defaultHighCardinality)
labelWithHash := getEnv(defaultLabelWithHash)

exporterPort, errExporterPort := strconv.Atoi(exporterPortEnv)
if errExporterPort != nil {
Expand All @@ -75,7 +89,11 @@ func LoadEnv() {
panic(fmt.Sprintf("%s must be > 0", defaultPort.Key))
}

SetVar(exporterPort, strings.ToLower(disableTracker) == "true", loglevel, qbitURL, qbitUsername, qbitPassword, timeoutDuration, strings.ToLower(enableHighCardinality) == "true")
SetVar(exporterPort, envSetToTrue(enableTracker), loglevel, qbitURL, qbitUsername, qbitPassword, timeoutDuration, envSetToTrue(enableHighCardinality), envSetToTrue(labelWithHash))
}

func envSetToTrue(env string) bool {
return strings.ToLower(env) == "true"
}

func GetPasswordMasked() string {
Expand All @@ -91,15 +109,20 @@ func GetFeaturesEnabled() string {
}
}

if EnableHighCardinality {
if Feature.EnableHighCardinality {
features += "High cardinality"
}

if !DisableTracker {
if Feature.EnableTracker {
addComma()
features += "Trackers"
}

if ExperimentalFeature.EnableLabelWithHash {
addComma()
features += "Label with hash (experimental)"
}

features = "[" + features + "]"

return features
Expand Down
91 changes: 69 additions & 22 deletions src/app/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,88 @@ import (

func TestGetFeaturesEnabled(t *testing.T) {
tests := []struct {
name string
enableHighCardinality bool
disableTracker bool
expectedOutput string
name string
features Features
experimentalFeature ExperimentalFeatures

expectedOutput string
}{
{
name: "Both disabled",
enableHighCardinality: false,
disableTracker: true,
expectedOutput: "[]",
name: "All features disabled",
features: Features{
EnableHighCardinality: false,
EnableTracker: false,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: false,
},
expectedOutput: "[]",
},
{
name: "Only High Cardinality enabled",
features: Features{
EnableHighCardinality: true,
EnableTracker: false,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: false,
},
expectedOutput: "[High cardinality]",
},
{
name: "Only High Cardinality enabled",
enableHighCardinality: true,
disableTracker: true,
expectedOutput: "[High cardinality]",
name: "Only Trackers enabled",
features: Features{
EnableHighCardinality: false,
EnableTracker: true,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: false,
},
expectedOutput: "[Trackers]",
},
{
name: "Only Trackers enabled",
enableHighCardinality: false,
disableTracker: false,
expectedOutput: "[Trackers]",
name: "Both High Cardinality and Trackers enabled",
features: Features{
EnableHighCardinality: true,
EnableTracker: true,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: false,
},
expectedOutput: "[High cardinality, Trackers]",
},
{
name: "Both High Cardinality and Trackers enabled",
enableHighCardinality: true,
disableTracker: false,
expectedOutput: "[High cardinality, Trackers]",
name: "Experimental feature enabled",
features: Features{
EnableHighCardinality: false,
EnableTracker: false,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: true,
},
expectedOutput: "[Label with hash (experimental)]",
},
{
name: "All features enabled",
features: Features{
EnableHighCardinality: true,
EnableTracker: true,
},
experimentalFeature: ExperimentalFeatures{
EnableLabelWithHash: true,
},
expectedOutput: "[High cardinality, Trackers, Label with hash (experimental)]",
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
EnableHighCardinality = test.enableHighCardinality
DisableTracker = test.disableTracker
// Set main features
Feature.EnableHighCardinality = test.features.EnableHighCardinality
Feature.EnableTracker = test.features.EnableTracker

// Set experimental features
ExperimentalFeature.EnableLabelWithHash = test.experimentalFeature.EnableLabelWithHash

result := GetFeaturesEnabled()
if result != test.expectedOutput {
Expand Down
10 changes: 8 additions & 2 deletions src/app/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ var defaultBaseUrl = Env{
}

var defaultDisableTracker = Env{
Key: "DISABLE_TRACKER",
DefaultValue: "false",
Key: "ENABLE_TRACKER",
DefaultValue: "true",
Help: "",
}

Expand All @@ -63,6 +63,12 @@ var defaultHighCardinality = Env{
Help: "",
}

var defaultLabelWithHash = Env{
Key: "ENABLE_LABEL_WITH_HASH",
DefaultValue: "false",
Help: "",
}

func getEnv(env Env) string {
value, ok := os.LookupEnv(env.Key)
if !ok || value == "" {
Expand Down
2 changes: 1 addition & 1 deletion src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/klauspost/compress v1.17.11 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.60.0 // indirect
github.com/prometheus/common v0.60.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/protobuf v1.35.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
github.com/prometheus/common v0.60.0 h1:+V9PAREWNvJMAuJ1x1BaWl9dewMW4YrHZQbx0sJNllA=
github.com/prometheus/common v0.60.0/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
github.com/prometheus/common v0.60.1 h1:FUas6GcOw66yB/73KC+BOZoFJmbo/1pojoILArPAaSc=
github.com/prometheus/common v0.60.1/go.mod h1:h0LYf1R1deLSKtD4Vdg8gy4RuOvENW2J/h19V5NADQw=
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
10 changes: 5 additions & 5 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ func main() {
fmt.Println("Author:", Author)
fmt.Println("Using log level: " + fmt.Sprintf("%s%s%s", logger.ColorLogLevel[logger.LogLevels[app.LogLevel]], app.LogLevel, logger.Reset))

envFileMessage := "Using environment variables"
if app.UsingEnvFile {
envFileMessage = "Using .env"
}
logger.Log.Debug(envFileMessage)
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 {
isTrackerEnabled = "disabled"
}
logger.Log.Debug("Trackers info is " + isTrackerEnabled)

qbit.Auth()

Expand Down
Loading

0 comments on commit 3626b06

Please sign in to comment.