Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Add TrackerDomain, DownloadTotals and UploadTotals to metrics #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Sample
------

Here is a screenshot of a sample dashboard created using [`grafana`](https://github.com/grafana/grafana)
with metrics from exported from `rtorrent_exporter`.
with metrics from exported from `rtorrent_exporter` (see sample_dash.json).

![sample](https://cloud.githubusercontent.com/assets/1926905/13891308/bad263be-ed26-11e5-9601-9d770d95c538.png)
![sample](https://user-images.githubusercontent.com/766820/29206006-923e7b20-7e45-11e7-84ae-1e51d0755f92.jpg)
2 changes: 1 addition & 1 deletion cmd/rtorrent_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func main() {
http.Redirect(w, r, *metricsPath, http.StatusMovedPermanently)
})

log.Printf("starting rTorrent exporter on %q for server %q (authentication: %v)",
log.Printf("starting rTorrent v0.1.1 exporter on %q for server %q (authentication: %v)",
*telemetryAddr, *rtorrentAddr, rt != nil)

if err := http.ListenAndServe(*telemetryAddr, nil); err != nil {
Expand Down
88 changes: 87 additions & 1 deletion downloadscollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ type DownloadsSource interface {
Active() ([]string, error)

BaseFilename(infoHash string) (string, error)
TrackerDomain(infoHash string) (string, error)
DownloadRate(infoHash string) (int, error)
UploadRate(infoHash string) (int, error)

DownloadTotal(infoHash string) (int, error)
UploadTotal(infoHash string) (int, error)
}

// A DownloadsCollector is a Prometheus collector for metrics regarding rTorrent
Expand All @@ -43,6 +47,9 @@ type DownloadsCollector struct {
DownloadRateBytes *prometheus.Desc
UploadRateBytes *prometheus.Desc

DownloadTotalBytes *prometheus.Desc
UploadTotalBytes *prometheus.Desc

ds DownloadsSource
}

Expand All @@ -57,7 +64,7 @@ func NewDownloadsCollector(ds DownloadsSource) *DownloadsCollector {
)

var (
labels = []string{"info_hash", "name"}
labels = []string{"info_hash", "name", "tracker"}
)

return &DownloadsCollector{
Expand Down Expand Up @@ -139,6 +146,20 @@ func NewDownloadsCollector(ds DownloadsSource) *DownloadsCollector {
nil,
),

DownloadTotalBytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "download_total_bytes"),
"Current download total in bytes.",
labels,
nil,
),

UploadTotalBytes: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "upload_total_bytes"),
"Current upload total in bytes.",
labels,
nil,
),

ds: ds,
}
}
Expand All @@ -154,6 +175,10 @@ func (c *DownloadsCollector) collect(ch chan<- prometheus.Metric) (*prometheus.D
return desc, err
}

if desc, err := c.collectAllDownloads(ch); err != nil {
return desc, err
}

return nil, nil
}

Expand Down Expand Up @@ -271,9 +296,15 @@ func (c *DownloadsCollector) collectActiveDownloads(ch chan<- prometheus.Metric)
return c.DownloadRateBytes, err
}

tracker, err := c.ds.TrackerDomain(a)
if err != nil {
return c.DownloadRateBytes, err
}

labels := []string{
a,
name,
tracker,
}

down, err := c.ds.DownloadRate(a)
Expand Down Expand Up @@ -304,6 +335,58 @@ func (c *DownloadsCollector) collectActiveDownloads(ch chan<- prometheus.Metric)
return nil, nil
}

// collectAllDownloads collects information about all downloads
func (c *DownloadsCollector) collectAllDownloads(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
all, err := c.ds.All()
if err != nil {
return c.Downloads, err
}

for _, a := range all {
name, err := c.ds.BaseFilename(a)
if err != nil {
return c.DownloadTotalBytes, err
}

tracker, err := c.ds.TrackerDomain(a)
if err != nil {
return c.DownloadTotalBytes, err
}

labels := []string{
a,
name,
tracker,
}

down, err := c.ds.DownloadTotal(a)
if err != nil {
return c.DownloadTotalBytes, err
}

up, err := c.ds.UploadTotal(a)
if err != nil {
return c.UploadTotalBytes, err
}

ch <- prometheus.MustNewConstMetric(
c.DownloadTotalBytes,
prometheus.CounterValue,
float64(down),
labels...,
)

ch <- prometheus.MustNewConstMetric(
c.UploadTotalBytes,
prometheus.CounterValue,
float64(up),
labels...,
)
}

return nil, nil
}

// Describe sends the descriptors of each metric over to the provided channel.
// The corresponding metric values are sent separately.
func (c *DownloadsCollector) Describe(ch chan<- *prometheus.Desc) {
Expand All @@ -320,6 +403,9 @@ func (c *DownloadsCollector) Describe(ch chan<- *prometheus.Desc) {

c.DownloadRateBytes,
c.UploadRateBytes,

c.DownloadTotalBytes,
c.UploadTotalBytes,
}

for _, d := range ds {
Expand Down
Loading