-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector.go
136 lines (120 loc) · 3.26 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/ropenttd/gopenttd/pkg/gopenttd"
"time"
)
var (
// Create a gauge to show whether the server is queryable or not..
statusDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "active"),
"Server state.",
[]string{}, nil,
)
// Create a gauge to track user counts. Spectators and overall Clients are
// differentiated via a "type" label.
clientsDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "clients"),
"Currently active clients.",
[]string{"type"}, nil,
)
clientsLimitsDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "client_limits"),
"Client limits.",
[]string{"type"}, nil,
)
// Create a gauge to track company count.
companiesDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "companies"),
"Currently active companies.",
[]string{}, nil,
)
companiesLimitsDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "company_limit"),
"Company limit.",
[]string{}, nil,
)
// Create a counter for the current runtime.
gameRunTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "game", "runtime"),
"How long the current game has been running in in-game days.",
[]string{}, nil,
)
queryTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName("openttd", "meta", "query_time"),
"Duration of the last query.",
[]string{}, nil,
)
)
type OpenttdCollector struct{}
// Describe is implemented with DescribeByCollect. That's possible because the
// Collect method will always return the same two metrics with the same two
// descriptors.
func (cc OpenttdCollector) Describe(ch chan<- *prometheus.Desc) {
prometheus.DescribeByCollect(cc, ch)
}
func (cc OpenttdCollector) Collect(ch chan<- prometheus.Metric) {
begin := time.Now()
result, _ := gopenttd.ScanServer(*targetServer, *targetPort)
duration := time.Since(begin)
var state int
if result.Status {
state = 1
} else {
state = 0
}
ch <- prometheus.MustNewConstMetric(
statusDesc,
prometheus.GaugeValue,
float64(state),
)
ch <- prometheus.MustNewConstMetric(
clientsDesc,
prometheus.GaugeValue,
float64(result.NumClients),
"clients",
)
ch <- prometheus.MustNewConstMetric(
clientsDesc,
prometheus.GaugeValue,
float64(result.NumSpectators),
"spectators",
)
ch <- prometheus.MustNewConstMetric(
clientsLimitsDesc,
prometheus.GaugeValue,
float64(result.MaxClients),
"clients",
)
ch <- prometheus.MustNewConstMetric(
clientsLimitsDesc,
prometheus.GaugeValue,
float64(result.MaxSpectators),
"spectators",
)
ch <- prometheus.MustNewConstMetric(
companiesDesc,
prometheus.GaugeValue,
float64(result.NumCompanies),
)
ch <- prometheus.MustNewConstMetric(
companiesLimitsDesc,
prometheus.GaugeValue,
float64(result.MaxCompanies),
)
ch <- prometheus.MustNewConstMetric(
gameRunTimeDesc,
prometheus.CounterValue,
float64(result.DateCurrent.Sub(result.DateStart).Hours()/24),
)
ch <- prometheus.MustNewConstMetric(
queryTimeDesc,
prometheus.GaugeValue,
duration.Seconds(),
)
}
func NewOpenttdCollector(reg prometheus.Registerer) *OpenttdCollector {
cc := &OpenttdCollector{}
prometheus.WrapRegistererWith(prometheus.Labels{}, reg).MustRegister(cc)
return cc
}