-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter.go
259 lines (231 loc) · 6.68 KB
/
exporter.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// Copyright 2019 Johannes Kohnen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package exporter
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/fsnotify/fsnotify"
"github.com/prometheus/client_golang/prometheus"
)
type Exporter struct {
c *Config
promUpdateCount prometheus.Counter
promUpdateAge prometheus.Gauge
promUpdateRunning prometheus.Gauge
promUpdateDuration prometheus.Summary
onceRegisterUpdateRunning sync.Once
onceRegisterUpdateDuration sync.Once
onceRegisterUpdateAge sync.Once
startup time.Time
promHandler http.Handler
log Logger
mu sync.RWMutex
start time.Time
end time.Time
oldEnd time.Time
}
func NewExporter(c *Config) *Exporter {
logger := log.New(os.Stderr, "", log.LstdFlags)
return NewExporterWithLogger(c, logger)
}
func NewExporterWithLogger(c *Config, logger Logger) *Exporter {
x := &Exporter{
c: c,
startup: time.Now(),
log: logger,
promUpdateCount: prometheus.NewCounter(prometheus.CounterOpts{
Namespace: c.Namespace,
Subsystem: c.Subsystem,
Name: "update_count_total",
Help: "Counter of update runs.",
}),
promUpdateAge: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: c.Namespace,
Subsystem: c.Subsystem,
Name: "update_age_seconds",
Help: "Time since last time an update finished.",
}),
promUpdateRunning: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: c.Namespace,
Subsystem: c.Subsystem,
Name: "update_running",
Help: "If the monitored process seems to run: 0 no; 1 yes.",
}),
promUpdateDuration: prometheus.NewSummary(prometheus.SummaryOpts{
Namespace: c.Namespace,
Subsystem: c.Subsystem,
Name: "update_duration_seconds",
Help: "Duration of update runs in seconds.",
}),
}
prometheus.MustRegister(x.promUpdateCount)
var (
startFile, endFile string
err error
)
if x.c.StartFile != "" {
startFile, err = filepath.Abs(x.c.StartFile)
if err != nil {
logger.Fatal(err)
}
}
if x.c.EndFile == "" {
logger.Fatalln("--end-file must be set!")
}
endFile, err = filepath.Abs(x.c.EndFile)
if err != nil {
logger.Fatal(err)
}
startWatcher, endWatcher := x.createWatcher(startFile), x.createWatcher(endFile)
x.watch(startWatcher, endWatcher)
return x
}
func (x *Exporter) WrapPromHandler(handler http.Handler) {
x.promHandler = handler
}
func (x *Exporter) createWatcher(filename string) *fsnotify.Watcher {
if filename == "" {
// return a watcher that will block forever
return &fsnotify.Watcher{}
}
w, err := fsnotify.NewWatcher()
if err != nil {
x.log.Fatalf("Error creating fs notifier: %v", err)
}
dir := filepath.Dir(filename)
deadline := time.NewTimer(time.Until(x.startup.Add(x.c.DirectoryTimeout)))
retry:
for backoff := time.Second; ; backoff *= 2 {
addErr := w.Add(dir)
if addErr == nil {
break retry
}
select {
case <-time.After(backoff):
x.log.Printf("Retrying to add directory \"%s\" in %s after error: %v", dir, backoff, addErr)
continue retry
case <-deadline.C:
x.log.Fatalf("Giving up adding directory \"%s\": %v", dir, addErr)
}
}
return w
}
func (x *Exporter) watch(startWatcher, endWatcher *fsnotify.Watcher) {
go func() {
bs := filepath.Base(x.c.StartFile)
be := filepath.Base(x.c.EndFile)
x.update()
for {
select {
case e := <-startWatcher.Events:
if filepath.Base(e.Name) == bs {
x.update()
}
case e := <-endWatcher.Events:
if filepath.Base(e.Name) == be {
x.update()
}
case err := <-startWatcher.Errors:
x.log.Printf("Error waiting for fs event on start file: %v", err)
case err := <-endWatcher.Errors:
x.log.Printf("Error waiting for fs event on end file: %v", err)
}
}
}()
}
// in case of error returns zero time.Time
func measure(filename string) (mtime time.Time) {
if filename == "" {
return
}
stat, err := os.Stat(filename)
if err != nil {
return
}
return stat.ModTime()
}
func (x *Exporter) update() {
start, end := measure(x.c.StartFile), measure(x.c.EndFile)
x.mu.Lock()
defer x.mu.Unlock()
x.start, x.end = start, end
if !start.IsZero() {
x.onceRegisterUpdateRunning.Do(func() { prometheus.MustRegister(x.promUpdateRunning) })
if end.IsZero() || start.After(end) {
if x.c.Debug {
x.log.Printf("An update run started.")
}
x.promUpdateRunning.Set(1)
} else {
x.promUpdateRunning.Set(0)
}
}
if !end.IsZero() && end != x.oldEnd {
x.oldEnd = end
if start.After(end) || x.startup.After(end) {
return
}
if x.c.Debug {
x.log.Printf("An update run ended.")
}
x.promUpdateCount.Inc()
if !start.IsZero() {
x.onceRegisterUpdateDuration.Do(func() { prometheus.MustRegister(x.promUpdateDuration) })
x.promUpdateDuration.Observe(end.Sub(start).Seconds())
}
}
}
// PromHandler updates update_age just before handling scrape
func (x *Exporter) PromHandler(w http.ResponseWriter, r *http.Request) {
x.mu.RLock()
myEnd := x.end
x.mu.RUnlock()
if !myEnd.IsZero() {
x.onceRegisterUpdateAge.Do(func() { prometheus.MustRegister(x.promUpdateAge) })
x.promUpdateAge.Set(time.Since(myEnd).Seconds())
}
x.promHandler.ServeHTTP(w, r)
}
func (x *Exporter) healthHandler(w http.ResponseWriter, r *http.Request) {
x.writeStatusResponse(w, x.c.HealthTimeout, x.c.Welpenschutz)
}
func (x *Exporter) livenessHandler(w http.ResponseWriter, r *http.Request) {
x.writeStatusResponse(w, x.c.LivenessTimeout, 0)
}
func (x *Exporter) writeStatusResponse(w http.ResponseWriter, timeout, welpenschutz time.Duration) {
x.mu.RLock()
myEnd := x.end
x.mu.RUnlock()
updateAge := time.Since(myEnd)
good := updateAge < timeout
if welpenschutz > 0 && time.Since(x.startup) < welpenschutz {
good = true
}
const body = "last_update: %s\r\n" +
"# time %s means never.\r\n" +
"# alive/healthy: %t\r\n"
endF := myEnd.Format(time.RFC3339Nano)
if good {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprintf(w, body, endF, time.Time{}, good)
} else {
http.Error(w, fmt.Sprintf(body, endF, time.Time{}, good), http.StatusServiceUnavailable)
}
}