-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (177 loc) · 6.09 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
const (
namespace string = "gbfs"
// Port list: https://github.com/prometheus/prometheus/wiki/Default-port-allocations
listenAddress string = ":9607"
)
/***********
BEGIN UTIL
***********/
// Max returns the greatest of the two integers.
func Max(x, y int64) int64 {
if x > y {
return x
}
return y
}
// BoolToFloat64 converts a boolean value to a float. 1 if true, else 0.
func BoolToFloat64(x bool) float64 {
if x {
return 1
}
return 0
}
/***********
END UTIL
***********/
// GBFSAPIResponse yada yada
type GBFSAPIResponse struct {
LastUpdated int64 `json:"last_updated"`
TTL int64 `json:"ttl"`
}
// StationStatus holds the status of stations
type StationStatus struct {
ID string `json:"station_id"`
BikesAvailable int64 `json:"num_bikes_available"`
BikesDisabled int64 `json:"num_bikes_disabled,omitempty"`
DocksAvailable int64 `json:"num_docks_available"`
DocksDisabled int64 `json:"num_docks_disabled,omitempty"`
Installed bool `json:"is_installed"`
Renting bool `json:"is_renting"`
Returning bool `json:"is_returning"`
LastReported int64 `json:"last_reported"`
}
// UnmarshalJSON I hate warnings
func (s *StationStatus) UnmarshalJSON(data []byte) error {
type Alias StationStatus
alias := &struct {
Installed int64 `json:"is_installed"`
Renting int64 `json:"is_renting"`
Returning int64 `json:"is_returning"`
*Alias
}{
Alias: (*Alias)(s),
}
if err := json.Unmarshal(data, &alias); err != nil {
return err
}
s.Installed = alias.Installed != 0
s.Renting = alias.Renting != 0
s.Returning = alias.Returning != 0
return nil
}
// StationStatusAPIResponse holds the API response for station status
type StationStatusAPIResponse struct {
Data struct {
Stations []StationStatus `json:"stations"`
} `json:"data"`
GBFSAPIResponse
}
// GetStationStatuses blah blah
func GetStationStatuses(body []byte) (*StationStatusAPIResponse, error) {
resp := new(StationStatusAPIResponse)
err := json.Unmarshal(body, &resp)
return resp, err
}
func probeGBFS(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
target := params.Get("target")
if target == "" {
http.Error(w, "Target parameter missing", 400)
return
}
resp, err := http.Get(target)
if err != nil {
// Room for improvement, check types of errors that can be returned (ex. timeouts, redirects)
http.Error(w, fmt.Sprintf("HTTP error: %v", err), 400)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to read HTTP body of target '%s': %v", target, err), 500)
return
}
var (
bikesAvailableGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "bikes_available",
Help: "The number of bikes available for rental",
}, []string{"station_id"})
bikesDisabledGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "bikes_disabled",
Help: "The number of disabled bikes",
}, []string{"station_id"})
docksAvailableGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "docks_available",
Help: "The number of docks accepting bike returns",
}, []string{"station_id"})
docksDisabledGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "docks_disabled",
Help: "The number of empty but disabled dock points",
}, []string{"station_id"})
installedGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "installed",
Help: "Indicates if the station is currently renting bikes, " +
"regardless of if any bikes are available",
}, []string{"station_id"})
rentingGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "renting",
Help: "Indicates if the station is currently accepting bike returns, " +
"regardless of if any docks are available",
}, []string{"station_id"})
lastReportedGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "last_reported_timestamp_seconds",
Help: "Last time this station reported its status to the feed, in unixtime",
}, []string{"station_id"})
)
registry := prometheus.NewRegistry()
registry.MustRegister(bikesAvailableGauge)
registry.MustRegister(bikesDisabledGauge)
registry.MustRegister(docksAvailableGauge)
registry.MustRegister(docksDisabledGauge)
registry.MustRegister(installedGauge)
registry.MustRegister(rentingGauge)
registry.MustRegister(lastReportedGauge)
stationStatusResp, err := GetStationStatuses(body)
if err != nil {
http.Error(w, fmt.Sprintf("Could not unmarshal target JSON,"+
" target '%s' does not have the expected schema: %v", target, err), 400)
return
}
for _, status := range stationStatusResp.Data.Stations {
bikesAvailableGauge.With(prometheus.Labels{"station_id": status.ID}).Set(float64(status.BikesAvailable))
bikesDisabledGauge.With(prometheus.Labels{"station_id": status.ID}).Set(float64(status.BikesDisabled))
docksAvailableGauge.With(prometheus.Labels{"station_id": status.ID}).Set(float64(status.DocksAvailable))
docksDisabledGauge.With(prometheus.Labels{"station_id": status.ID}).Set(float64(status.DocksDisabled))
installedGauge.With(prometheus.Labels{"station_id": status.ID}).Set(BoolToFloat64(status.Installed))
rentingGauge.With(prometheus.Labels{"station_id": status.ID}).Set(BoolToFloat64(status.Renting))
lastReportedGauge.With(prometheus.Labels{"station_id": status.ID}).Set(float64(status.LastReported))
}
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
handler.ServeHTTP(w, r)
}
func main() {
log.Printf("G O O D B O I L A U N C H I N G ON %s\n", listenAddress)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", probeGBFS)
if err := http.ListenAndServe(listenAddress, nil); err != nil {
log.Fatalln(errors.Wrapf(err, "Failed to spin up server"))
}
}