-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.go
89 lines (75 loc) · 2.85 KB
/
metrics.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
package main
import (
"context"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
metricOTSubmitSuccessCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "owntracks_publish_endpoint_success",
Help: "Number of successful requests served at the OwnTracks publishing endpoint",
})
metricOTSubmitErrorCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "owntracks_publish_endpoint_errors",
Help: "Number of errors served at the OwnTracks publishing endpoint",
})
metric4sqSyncSuccessCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "foursquare_sync_success_count",
Help: "Number of successful syncs with foursquare",
})
metric4sqSyncErrorCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "foursquare_sync_error_count",
Help: "Number of syncs that failed with foursquare",
})
metricTripitSyncSuccessCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "tripit_sync_success_count",
Help: "Number of successful syncs with TripIt",
})
metricTripitSyncErrorCount = promauto.NewCounter(prometheus.CounterOpts{
Name: "tripit_sync_error_count",
Help: "Number of syncs that failed with TripIt",
})
)
var _ prometheus.Collector = (*metricsCollector)(nil)
type metricsCollector struct {
l logger
s *Storage
lastDeviceLocationTime *prometheus.Desc
latestFoursquareCheckin *prometheus.Desc
}
func newMetricsCollector(l logger, s *Storage) *metricsCollector {
return &metricsCollector{
l: l,
s: s,
lastDeviceLocationTime: prometheus.NewDesc(
"last_device_location_at",
"Unix timestamp when of the last device location reported", nil, nil),
latestFoursquareCheckin: prometheus.NewDesc(
"last_foursquare_checkin_at",
"Unix time timestamp when the last foursquare checkin was recorded into the DB", nil, nil),
}
}
func (m *metricsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- m.lastDeviceLocationTime
ch <- m.latestFoursquareCheckin
}
func (m *metricsCollector) Collect(ch chan<- prometheus.Metric) {
// we don't get a context from the scrape here, so just create one with a
// reasonably timeout to avoid blocking things here
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
lt, err := m.s.LatestLocationTimestamp(ctx)
if err == nil {
ch <- prometheus.MustNewConstMetric(m.lastDeviceLocationTime, prometheus.GaugeValue, float64(lt.Unix()))
} else {
ch <- prometheus.NewInvalidMetric(m.lastDeviceLocationTime, fmt.Errorf("get latest device location timestamp: %v", err))
}
lfsq, err := m.s.Last4sqCheckinTime(ctx)
if err == nil {
ch <- prometheus.MustNewConstMetric(m.latestFoursquareCheckin, prometheus.GaugeValue, float64(lfsq.Unix()))
} else {
ch <- prometheus.NewInvalidMetric(m.latestFoursquareCheckin, fmt.Errorf("get latest foursquare checkin timestamp: %v", err))
}
}