-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
181 lines (151 loc) · 4.44 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strconv"
"time"
"github.com/go-resty/resty"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
addr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
veloURI = "https://www.velo-antwerpen.be/availability_map/getJsonObject"
)
func init() {
velo := NewVeloManager("antwerpen")
prometheus.MustRegister(velo)
}
// Station holds all the data that a single station has.
// See Station.json for how the original looks like.
type Station struct {
ID string `json:"id"`
District string `json:"-"`
Lon string `json:"-"`
Lat string `json:"-"`
Bikes string `json:"bikes"`
Slots string `json:"slots"`
Zip string `json:"-"`
Address string `json:"-"`
AddressNumber string `json:"-"`
NearbyStations string `json:"-"`
Status string `json:"-"`
Name string `json:"name"`
}
// VeloManager is what does a .
type VeloManager struct {
City string // Not realy necessary. Might only be usefull if another city uses the exact same infrastructure from ClearChanel.
BikesDesc *prometheus.Desc
SlotsDesc *prometheus.Desc
DurationDesc *prometheus.Desc
}
// GetStations calls the velo api and converts the json to whatever prometheus can handle.
func (v *VeloManager) GetStations() (bikesByStation map[string]int, slotsByStation map[string]int, duration float64) {
start := time.Now()
// GET request
resp, err := resty.R().Get(veloURI)
if err != nil {
fmt.Println("Error GET: ", err)
return
}
var Stations []Station
JSONObject := resp.Body()
JSONBytes := []byte(JSONObject)
// Unmarshal JSONBytes unto Stations.
err = json.Unmarshal(JSONBytes, &Stations)
if err != nil {
log.Printf("Error unmashaling json: %v", err)
}
// Create 2 maps which will hold the values of bikes and slots.
bikesByStation = make(map[string]int)
slotsByStation = make(map[string]int)
// Loop over all stations and pull out the available bikes,
// slots and the station name.
for idx := range Stations {
bike := Stations[idx].Bikes
slots := Stations[idx].Slots
name := Stations[idx].Name
// Convert json string to int.
iBikes, err := strconv.Atoi(bike)
if err != nil {
log.Printf("Error strconv bike: %v", err)
}
iSlots, err := strconv.Atoi(slots)
if err != nil {
log.Printf("Error strconv slot: %v", err)
}
bikesByStation[name] = iBikes
slotsByStation[name] = iSlots
}
end := time.Now()
duration = float64(end.Sub(start))
log.Printf("Duration: %v", end.Sub(start))
return
}
// Describe simply sends the twp Descs in the struct to the channel.
func (v *VeloManager) Describe(ch chan<- *prometheus.Desc) {
ch <- v.BikesDesc
ch <- v.SlotsDesc
ch <- v.DurationDesc
}
// Collect first triggers the GetStations. Then it creates the guages
// for each station on the fly based on the returned data.
func (v *VeloManager) Collect(ch chan<- prometheus.Metric) {
bikesByStation, slotsByStation, duration := v.GetStations()
for station, bikesCount := range bikesByStation {
ch <- prometheus.MustNewConstMetric(
v.BikesDesc,
prometheus.GaugeValue,
float64(bikesCount),
station,
)
}
for station, slotsCount := range slotsByStation {
ch <- prometheus.MustNewConstMetric(
v.SlotsDesc,
prometheus.GaugeValue,
float64(slotsCount),
station,
)
}
ch <- prometheus.MustNewConstMetric(
v.DurationDesc,
prometheus.GaugeValue,
float64(duration),
"time",
)
}
// NewVeloManager creates the two Descs BikesDesc and SlotsDesc.
func NewVeloManager(city string) *VeloManager {
return &VeloManager{
City: city,
BikesDesc: prometheus.NewDesc(
"velo_available_bikes",
"Number of bikes available at a given station.",
[]string{"station"},
prometheus.Labels{"city": city},
),
SlotsDesc: prometheus.NewDesc(
"velo_available_slots",
"Number of free slots available at a given station.",
[]string{"station"},
prometheus.Labels{"city": city},
),
DurationDesc: prometheus.NewDesc(
"velo_http_get_duration",
"The time it took to get json from Velo and process it.",
[]string{"time"},
prometheus.Labels{"city": city},
),
}
}
func main() {
flag.Parse()
log.Printf("Pushing metrics on port: %v", *addr)
// Expose the registered metrics via HTTP.
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*addr, nil))
}