-
Notifications
You must be signed in to change notification settings - Fork 3
/
collector.go
112 lines (98 loc) · 3.41 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
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 main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"strconv"
"github.com/prometheus/client_golang/prometheus"
)
type Instance struct {
Stats struct {
UserCount int `json:"user_count"`
StatusCount int `json:"status_count"`
DomainCount int `json:"domain_count"`
} `json:"stats"`
}
type Activities []Activity
type Activity struct {
Statuses string `json:"statuses"`
Logins string `json:"logins"`
Registrations string `json:"registrations"`
}
type Collector struct {
Users *prometheus.Desc
Statuses *prometheus.Desc
Domains *prometheus.Desc
WeeklyStatuses *prometheus.Desc
WeeklyLogins *prometheus.Desc
WeeklyRegistration *prometheus.Desc
}
func NewCollector() *Collector {
labels := []string{"host"}
return &Collector{
Users: prometheus.NewDesc("mastodon_users", "Total number of all users", labels, nil),
Statuses: prometheus.NewDesc("mastodon_statuses", "Total number of all statuses", labels, nil),
Domains: prometheus.NewDesc("mastodon_domains", "Total number of known domains", labels, nil),
WeeklyStatuses: prometheus.NewDesc("mastodon_weekly_statuses", "Total number of weekly published statuses", labels, nil),
WeeklyLogins: prometheus.NewDesc("mastodon_weekly_logins", "Total number of weekly logins", labels, nil),
WeeklyRegistration: prometheus.NewDesc("mastodon_weekly_registrations", "Total number of weekly registration", labels, nil),
}
}
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.Users
ch <- c.Statuses
ch <- c.Domains
ch <- c.WeeklyStatuses
ch <- c.WeeklyLogins
ch <- c.WeeklyRegistration
}
func (c *Collector) Collect(ch chan<- prometheus.Metric) {
mastodon, _ := url.Parse(*mastodonURL)
labels := []string{mastodon.Host}
instance, err := fetchInstance()
if err == nil {
ch <- prometheus.MustNewConstMetric(c.Users, prometheus.GaugeValue, float64(instance.Stats.UserCount), labels...)
ch <- prometheus.MustNewConstMetric(c.Statuses, prometheus.GaugeValue, float64(instance.Stats.StatusCount), labels...)
ch <- prometheus.MustNewConstMetric(c.Domains, prometheus.GaugeValue, float64(instance.Stats.DomainCount), labels...)
} else {
log.Println(err)
}
activities, err := fetchActivity()
if err == nil {
statuses, err := strconv.Atoi(activities[0].Statuses)
if err == nil {
ch <- prometheus.MustNewConstMetric(c.WeeklyStatuses, prometheus.GaugeValue, float64(statuses), labels...)
}
logins, err := strconv.Atoi(activities[0].Logins)
if err == nil {
ch <- prometheus.MustNewConstMetric(c.WeeklyLogins, prometheus.GaugeValue, float64(logins), labels...)
}
registrations, err := strconv.Atoi(activities[0].Registrations)
if err == nil {
ch <- prometheus.MustNewConstMetric(c.WeeklyRegistration, prometheus.GaugeValue, float64(registrations), labels...)
}
} else {
log.Println(err)
}
}
func fetchInstance() (Instance, error) {
var instance Instance
res, err := http.Get(*mastodonURL + "/api/v1/instance")
if err != nil {
return instance, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&instance)
return instance, err
}
func fetchActivity() (Activities, error) {
var activities Activities
res, err := http.Get(*mastodonURL + "/api/v1/instance/activity")
if err != nil {
return activities, err
}
defer res.Body.Close()
err = json.NewDecoder(res.Body).Decode(&activities)
return activities, err
}