/
releases.go
112 lines (103 loc) · 2.77 KB
/
releases.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package collector
import (
"path/filepath"
"strings"
"sync"
"time"
"github.com/caarlos0/github_releases_exporter/client"
"github.com/caarlos0/github_releases_exporter/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
)
type releasesCollector struct {
mutex sync.Mutex
config *config.Config
client client.Client
up *prometheus.Desc
downloads *prometheus.Desc
scrapeDuration *prometheus.Desc
}
// NewReleasesCollector returns a releases collector
func NewReleasesCollector(config *config.Config, client client.Client) prometheus.Collector {
const namespace = "github"
const subsystem = "release"
return &releasesCollector{
config: config,
client: client,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "up"),
"Exporter is being able to talk with GitHub API",
nil,
nil,
),
downloads: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "asset_download_count"),
"Download count of each asset of a github release",
[]string{"repository", "tag", "name", "extension"},
nil,
),
scrapeDuration: prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "scrape_duration_seconds"),
"Returns how long the probe took to complete in seconds",
nil,
nil,
),
}
}
// Describe all metrics
func (c *releasesCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.up
ch <- c.downloads
ch <- c.scrapeDuration
}
// Collect all metrics
func (c *releasesCollector) Collect(ch chan<- prometheus.Metric) {
c.mutex.Lock()
defer c.mutex.Unlock()
log.Infof("collecting %d repositories", len(c.config.Repositories))
var start = time.Now()
var success = 1
for _, repository := range c.config.Repositories {
log.Infof("collecting %s", repository)
releases, err := c.client.Releases(repository)
if err != nil {
success = 0
log.Errorf("failed to collect: %s", err.Error())
continue
}
for _, release := range releases {
assets, err := c.client.Assets(repository, release.ID)
if err != nil {
success = 0
log.Errorf(
"failed to collect repo %s, release %s: %s",
repository,
release.Tag,
err.Error(),
)
continue
}
for _, asset := range assets {
ext := strings.TrimPrefix(filepath.Ext(asset.Name), ".")
log.Debugf(
"collecting %s@%s / %s (%s)",
repository,
release.Tag,
asset.Name,
ext,
)
ch <- prometheus.MustNewConstMetric(
c.downloads,
prometheus.CounterValue,
float64(asset.Downloads),
repository,
release.Tag,
asset.Name,
ext,
)
}
}
}
ch <- prometheus.MustNewConstMetric(c.scrapeDuration, prometheus.GaugeValue, time.Since(start).Seconds())
ch <- prometheus.MustNewConstMetric(c.up, prometheus.GaugeValue, float64(success))
}