-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
202 lines (182 loc) · 5.69 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
202
package main
import (
"fmt"
"log"
"math"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type wazeMetric struct {
wazeParameters WazeParameters
wazeRequest *WazeRequest
timeTravelTime prometheus.Gauge
timeTravelDistance prometheus.Gauge
}
type context struct {
sleepTime time.Duration
listen string
wazeMetrics []*wazeMetric
wazeTimeSpent prometheus.Counter
wazeCallsOk prometheus.Counter
wazeCallsKo prometheus.Counter
wazeParameters prometheus.Counter
}
const (
namespace = "waze"
)
var (
promWazeTravelTime = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "travel_time_seconds",
Help: "travel time in seconds",
}, []string{"from", "to"})
promWazeTravelDistance = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "travel_distance_meters",
Help: "travel distance in meters",
}, []string{"from", "to"})
promWazeCalls = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "api_calls",
Help: "number of calls to the Waze API",
}, []string{"status"})
promWazeParams = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "parameters",
Help: "Waze parameters",
}, []string{"region", "sleep", "vehicle", "avoid_toll", "avoid_subscription_road", "avoid_ferry"})
promWazeTimeSpent = prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Name: "time_seconds",
Help: "total time spent to to process Waze API",
})
)
func (c *context) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.wazeMetrics {
metric.describe(ch)
}
c.wazeCallsOk.Describe(ch)
c.wazeCallsKo.Describe(ch)
c.wazeTimeSpent.Describe(ch)
c.wazeParameters.Describe(ch)
}
func (c *context) Collect(ch chan<- prometheus.Metric) {
sleep := false
for _, metric := range c.wazeMetrics {
if sleep {
time.Sleep(c.sleepTime)
}
duration, err := metric.collect(ch)
if err == nil {
c.wazeCallsOk.Inc()
} else {
c.wazeCallsKo.Inc()
}
c.wazeTimeSpent.Add(duration.Seconds())
sleep = true
}
c.wazeCallsOk.Collect(ch)
c.wazeCallsKo.Collect(ch)
c.wazeTimeSpent.Collect(ch)
c.wazeParameters.Collect(ch)
}
func (w *wazeMetric) describe(ch chan<- *prometheus.Desc) {
w.timeTravelDistance.Describe(ch)
w.timeTravelTime.Describe(ch)
}
func (w *wazeMetric) collect(ch chan<- prometheus.Metric) (time.Duration, error) {
begin := time.Now()
result, err := w.wazeRequest.Call()
duration := time.Now().Sub(begin)
if err != nil {
// dont change the values
log.Println("Error", w.timeTravelTime.Desc().String(), err)
} else if len(result) > 0 {
w.timeTravelDistance.Set(float64(result[0].Distance))
w.timeTravelTime.Set(math.Round(result[0].Duration.Seconds()))
}
w.timeTravelDistance.Collect(ch)
w.timeTravelTime.Collect(ch)
return duration, err
}
func createWazeCoordinates(addresses map[string]string, region Region, client *http.Client) map[string]string {
result := map[string]string{}
for name, address := range addresses {
coordinates, err := WazeAddressToQuery(address, region, client)
log.Println("Address", address, "has been found at", coordinates)
if err != nil {
log.Fatalln("Failed to retrieve the address", address, err)
}
result[name] = coordinates
}
return result
}
func getContext(filename string, client *http.Client) context {
jsonConfig := NewConfig(filename)
context := context{
sleepTime: time.Millisecond * time.Duration(jsonConfig.Sleep),
listen: jsonConfig.Listen,
wazeTimeSpent: promWazeTimeSpent,
wazeCallsOk: promWazeCalls.WithLabelValues("ok"),
wazeCallsKo: promWazeCalls.WithLabelValues("ko"),
wazeParameters: promWazeParams.WithLabelValues(
jsonConfig.Region.String(),
strconv.FormatInt(jsonConfig.Sleep, 10),
jsonConfig.Vehicle.String(),
strconv.FormatBool(jsonConfig.AvoidToll),
strconv.FormatBool(jsonConfig.AvoidSubscriptionRoad),
strconv.FormatBool(jsonConfig.AvoidFerry),
),
}
log.Println("Look for", len(jsonConfig.Addresses), "addresses")
coordinates := createWazeCoordinates(jsonConfig.Addresses, jsonConfig.Region, client)
log.Println("Create", len(jsonConfig.Paths), "paths")
for _, path := range jsonConfig.Paths {
fromCoordinates, fromFound := coordinates[path.From]
if !fromFound {
log.Fatalln("Address not found:", path.From)
}
toCoordinates, toFound := coordinates[path.To]
if !toFound {
log.Fatalln("Address not found:", path.To)
}
wazeMetric := &wazeMetric{
wazeParameters: WazeParameters{
FromCoordinates: fromCoordinates,
ToCoordinates: toCoordinates,
Region: jsonConfig.Region,
Vehicle: jsonConfig.Vehicle,
AvoidToll: jsonConfig.AvoidToll,
AvoidSubscriptionRoad: jsonConfig.AvoidSubscriptionRoad,
AvoidFerry: jsonConfig.AvoidFerry,
},
timeTravelTime: promWazeTravelTime.WithLabelValues(path.From, path.To),
timeTravelDistance: promWazeTravelDistance.WithLabelValues(path.From, path.To),
}
var err error
wazeMetric.wazeRequest, err = CreateRequest(wazeMetric.wazeParameters, client)
if err != nil {
log.Fatalln(err)
}
context.wazeMetrics = append(context.wazeMetrics, wazeMetric)
}
context.wazeParameters.Inc()
return context
}
func main() {
if len(os.Args) != 2 {
fmt.Fprintln(os.Stderr, "Usage", os.Args[0], "<config_file>")
os.Exit(1)
}
client := &http.Client{
Timeout: time.Second * 10,
}
context := getContext(os.Args[1], client)
prometheus.MustRegister(&context)
http.Handle("/metrics", promhttp.Handler())
log.Println(http.ListenAndServe(context.listen, nil))
}