-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
187 lines (172 loc) · 6.48 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type HomebrewMetricsItem struct {
Number int `json:"number"`
Formula string `json:"formula"`
Count string `json:"count"`
Percent string `json:"percent"`
}
type HomebrewMetrics struct {
Category string `json:"category"`
TotalItems int `json:"total_items"`
StartDate string `json:"start_date"`
EndDate string `json:"end_date"`
TotalCount int `json:"total_count"`
Items []HomebrewMetricsItem `json:"items"`
}
type HomebrewCollector struct {
Formulae []string
install30d *prometheus.Desc
install90d *prometheus.Desc
install365d *prometheus.Desc
installOnRequest30d *prometheus.Desc
installOnRequest90d *prometheus.Desc
installOnRequest365d *prometheus.Desc
buildError30d *prometheus.Desc
buildError90d *prometheus.Desc
buildError365d *prometheus.Desc
}
func newHomebrewCollector(formulae []string) *HomebrewCollector {
return &HomebrewCollector{
Formulae: formulae,
install30d: prometheus.NewDesc("homebrew_install_30d",
"Results from https://formulae.brew.sh/api/analytics/install/30d.json",
[]string{"formula"}, nil,
),
install90d: prometheus.NewDesc("homebrew_install_90d",
"Results from https://formulae.brew.sh/api/analytics/install/90d.json",
[]string{"formula"}, nil,
),
install365d: prometheus.NewDesc("homebrew_install_365d",
"Results from https://formulae.brew.sh/api/analytics/install/365d.json",
[]string{"formula"}, nil,
),
installOnRequest30d: prometheus.NewDesc("homebrew_install_on_request_30d",
"Results from https://formulae.brew.sh/api/analytics/install-on-request/30d.json",
[]string{"formula"}, nil,
),
installOnRequest90d: prometheus.NewDesc("homebrew_install_on_request_90d",
"Results from https://formulae.brew.sh/api/analytics/install-on-request/90d.json",
[]string{"formula"}, nil,
),
installOnRequest365d: prometheus.NewDesc("homebrew_install_on_request_365d",
"Results from https://formulae.brew.sh/api/analytics/install-on-request/365d.json",
[]string{"formula"}, nil,
),
buildError30d: prometheus.NewDesc("homebrew_build_error_30d",
"Results from https://formulae.brew.sh/api/analytics/build-error/30d.json",
[]string{"formula"}, nil,
),
buildError90d: prometheus.NewDesc("homebrew_build_error_90d",
"Results from https://formulae.brew.sh/api/analytics/build-error/90d.json",
[]string{"formula"}, nil,
),
buildError365d: prometheus.NewDesc("homebrew_build_error_365d",
"Results from https://formulae.brew.sh/api/analytics/build-error/365d.json",
[]string{"formula"}, nil,
),
}
}
func (collector *HomebrewCollector) Describe(ch chan<- *prometheus.Desc) {
//Update this section with the each metric you create for a given collector
ch <- collector.install30d
ch <- collector.install90d
ch <- collector.install365d
ch <- collector.installOnRequest30d
ch <- collector.installOnRequest90d
ch <- collector.installOnRequest365d
ch <- collector.buildError30d
ch <- collector.buildError90d
ch <- collector.buildError365d
}
func getHomebrewMetrics(url string) HomebrewMetrics {
client := http.Client{}
homebrewMetrics := HomebrewMetrics{}
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
panic(err)
}
res, err := client.Do(req)
if err != nil {
panic(err)
}
body, err := io.ReadAll(res.Body)
if err != nil {
panic(err)
}
json.Unmarshal(body, &homebrewMetrics)
return homebrewMetrics
}
func (collector *HomebrewCollector) collectMetric(url string, metric *prometheus.Desc, ch chan<- prometheus.Metric) {
homebrewMetrics := getHomebrewMetrics(url)
log.Printf("INFO Started collecting metrics from %s \n", url)
for _, formula := range collector.Formulae {
for _, item := range homebrewMetrics.Items {
if item.Formula == formula {
log.Printf("INFO Found formula %s in %s \n", formula, url)
countString := strings.Replace(item.Count, ",", "", -1)
count, err := strconv.ParseFloat(countString, 32)
if err != nil {
panic(err)
}
m := prometheus.MustNewConstMetric(metric, prometheus.GaugeValue, count, item.Formula)
ch <- m
}
}
}
log.Printf("INFO Finished collecting metrics from %s \n", url)
}
func (collector *HomebrewCollector) Collect(ch chan<- prometheus.Metric) {
collector.collectMetric("https://formulae.brew.sh/api/analytics/install/30d.json", collector.install30d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/install/90d.json", collector.install90d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/install/365d.json", collector.install365d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/install-on-request/30d.json", collector.installOnRequest30d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/install-on-request/90d.json", collector.installOnRequest90d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/install-on-request/365d.json", collector.installOnRequest365d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/build-error/30d.json", collector.buildError30d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/build-error/90d.json", collector.buildError90d, ch)
collector.collectMetric("https://formulae.brew.sh/api/analytics/build-error/365d.json", collector.buildError365d, ch)
}
func main() {
listenPort := os.Getenv("LISTEN_PORT")
if listenPort == "" {
listenPort = "9888"
}
metricsPath := os.Getenv("METRICS_PATH")
if metricsPath == "" {
metricsPath = "/metrics"
}
formulaeString := os.Getenv("HOMEBREW_FORMULAE")
if formulaeString != "" {
formulae := strings.Split(formulaeString, ", ")
log.Fatal(homebrewExporter(listenPort, metricsPath, formulae))
}
}
func homebrewExporter(listenPort string, metricsPath string, formulae []string) error {
homebrew := newHomebrewCollector(formulae)
prometheus.MustRegister(homebrew)
http.Handle(metricsPath, promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<head><title>Homebrew Metrics Exporter</title></head>
<body>
<h1>Homebrew Exporter</h1>
<p><a href='` + metricsPath + `'>Metrics</a></p>
</body>
</html>
`))
})
return http.ListenAndServe(fmt.Sprintf(":%s", listenPort), nil)
}