-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollector.go
60 lines (50 loc) · 1.87 KB
/
collector.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
// Copyright: (c) 2022, Justin Béra (@just1not2) <[email protected]>
// GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
package main
import (
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
)
type YourlsCollector struct {
client *YourlsClient
YourlsClicksTotalDescription *prometheus.Desc
YourlsLinksTotalDescription *prometheus.Desc
}
func NewYourlsCollector(configuration *YourlsConfiguration) *YourlsCollector {
return &YourlsCollector{
client: NewYourlsClient(configuration.YourlsURL, configuration.Signature, configuration.HTTPTimeout),
YourlsClicksTotalDescription: prometheus.NewDesc("yourls_clicks_total",
"Total number of clicks.",
nil, nil,
),
YourlsLinksTotalDescription: prometheus.NewDesc("yourls_links_total",
"Total number of deployed links.",
nil, nil,
),
}
}
func (collector *YourlsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.YourlsClicksTotalDescription
ch <- collector.YourlsLinksTotalDescription
}
func (collector *YourlsCollector) Collect(ch chan<- prometheus.Metric) {
// Gathers the specific query parameters
parameters := map[string]string{
"action": "stats",
}
body, err := collector.client.Request(parameters)
if err != nil {
panic(err)
}
// Converts returned statistics into floats
linksTotalValue, _ := strconv.ParseFloat(body.Stats["total_links"], 64)
clicksTotalValue, _ := strconv.ParseFloat(body.Stats["total_clicks"], 64)
// Sends metrics to the channel
m1 := prometheus.MustNewConstMetric(collector.YourlsClicksTotalDescription, prometheus.GaugeValue, clicksTotalValue)
m2 := prometheus.MustNewConstMetric(collector.YourlsLinksTotalDescription, prometheus.GaugeValue, linksTotalValue)
m1 = prometheus.NewMetricWithTimestamp(time.Now(), m1)
m2 = prometheus.NewMetricWithTimestamp(time.Now(), m2)
ch <- m1
ch <- m2
}