-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
reloader.go
889 lines (772 loc) · 23.9 KB
/
reloader.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
// Package reloader contains helpers to trigger reloads of Prometheus instances
// on configuration changes and to substitute environment variables in config files.
//
// Reloader type is useful when you want to:
//
// - Watch on changes against certain file e.g (`cfgFile`).
// - Optionally, specify different output file for watched `cfgFile` (`cfgOutputFile`).
// This will also try decompress the `cfgFile` if needed and substitute ALL the envvars using Kubernetes substitution format: (`$(var)`)
// - Watch on changes against certain directories (`watchedDirs`).
//
// Once any of those two changes, Prometheus on given `reloadURL` will be notified, causing Prometheus to reload configuration and rules.
//
// This and below for reloader:
//
// u, _ := url.Parse("http://localhost:9090")
// rl := reloader.New(nil, nil, &reloader.Options{
// ReloadURL: reloader.ReloadURLFromBase(u),
// CfgFile: "/path/to/cfg",
// CfgOutputFile: "/path/to/cfg.out",
// WatchedDirs: []string{"/path/to/dirs"},
// WatchInterval: 3 * time.Minute,
// RetryInterval: 5 * time.Second,
// })
//
// The url of reloads can be generated with function ReloadURLFromBase().
// It will append the default path of reload into the given url:
//
// u, _ := url.Parse("http://localhost:9090")
// reloader.ReloadURLFromBase(u) // It will return "http://localhost:9090/-/reload"
//
// Start watching changes and stopped until the context gets canceled:
//
// ctx, cancel := context.WithCancel(context.Background())
// go func() {
// if err := rl.Watch(ctx); err != nil {
// log.Fatal(err)
// }
// }()
// // ...
// cancel()
//
// Reloader will make a schedule to check the given config files and dirs of sum of hash with the last result,
// even if it is no changes.
//
// A basic example of configuration template with environment variables:
//
// global:
// external_labels:
// replica: '$(HOSTNAME)'
package reloader
import (
"bytes"
"compress/gzip"
"context"
"fmt"
"hash"
"io"
"maps"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
"syscall"
"time"
"github.com/fsnotify/fsnotify"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/minio/sha256-simd"
"github.com/mitchellh/go-ps"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/thanos-io/thanos/pkg/runutil"
)
// Reloader can watch config files and trigger reloads of a Prometheus server.
// It optionally substitutes environment variables in the configuration.
// Referenced environment variables must be of the form `$(var)` (not `$var` or `${var}`).
type Reloader struct {
logger log.Logger
cfgFile string
cfgOutputFile string
cfgDirs []CfgDirOption
tolerateEnvVarExpansionErrors bool
retryInterval time.Duration
watchInterval time.Duration
watchedDirs []string
watcher *watcher
tr TriggerReloader
lastCfgHash []byte
lastCfgDirsHash [][]byte
lastWatchedDirsHash []byte
lastCfgDirFiles []map[string]struct{}
forceReload bool
reloads prometheus.Counter
reloadErrors prometheus.Counter
lastReloadSuccess prometheus.Gauge
lastReloadSuccessTimestamp prometheus.Gauge
configApplyErrors prometheus.Counter
configEnvVarExpansionErrors prometheus.Gauge
configApply prometheus.Counter
reloaderInfo *prometheus.GaugeVec
}
// TriggerReloader reloads the configuration of the process.
type TriggerReloader interface {
TriggerReload(ctx context.Context) error
}
// CfgDirOption contains options for watching directories containing configurations. For example, a
// directory could contain additional scrape config files or rule files listed in the main
// Prometheus configuration. Sub-directories are ignored.
type CfgDirOption struct {
// Dir is the path containing the Prometheus configurations to watch.
Dir string
// OutputDir is a directory path to output configurations. If OutputDir is not empty,
// then all config files in the Dir directory are decompressed if needed, environment
// variables will be substituted and the output written into the given path. Prometheus
// should then use OutputDir as its config path.
OutputDir string
// TODO: https://github.com/thanos-io/thanos/issues/7201
}
// Options bundles options for the Reloader.
type Options struct {
// ReloadURL is the Prometheus URL to trigger reloads.
ReloadURL *url.URL
// HTTP client used to connect to the web server.
HTTPClient http.Client
// ProcessName is the process executable name to trigger reloads. If not
// empty, the reloader sends a SIGHUP signal to the matching process ID
// instead of using the HTTP reload endpoint.
ProcessName string
// RuntimeInfoURL is the Prometheus URL returning runtime information
// including the last configuration status (e.g. `/api/v1/status/runtimeinfo`).
// It is only relevant for signal-based reloads.
// If empty, the reloader will not be able to assess that the reloading is
// successful.
RuntimeInfoURL *url.URL
// CfgFile is a path to the Prometheus config file to watch.
CfgFile string
// CfgOutputFile is a path for the output config file.
// If cfgOutputFile is not empty the config file will be decompressed if needed, environment variables
// will be substituted and the output written into the given path. Prometheus should then use
// cfgOutputFile as its config file path.
CfgOutputFile string
// CfgDirs is an array of paths to directories containing Prometheus configs to watch.
CfgDirs []CfgDirOption
// WatchedDirs is a collection of paths for the reloader to watch over.
WatchedDirs []string
// DelayInterval controls how long the reloader will wait without receiving
// new file-system events before it applies the reload.
DelayInterval time.Duration
// WatchInterval controls how often reloader re-reads config and directories.
WatchInterval time.Duration
// RetryInterval controls how often the reloader retries a reloading of the
// configuration in case the reload operation returned an error.
RetryInterval time.Duration
// TolerateEnvVarExpansionErrors suppresses errors when expanding environment variables in the config file, and
// leaves the unset variables as is. All found environment variables are still expanded.
TolerateEnvVarExpansionErrors bool
}
var firstGzipBytes = []byte{0x1f, 0x8b, 0x08}
// New creates a new reloader that watches the given config file and directories
// and triggers a Prometheus reload upon changes.
func New(logger log.Logger, reg prometheus.Registerer, o *Options) *Reloader {
if logger == nil {
logger = log.NewNopLogger()
}
r := &Reloader{
logger: logger,
cfgFile: o.CfgFile,
cfgOutputFile: o.CfgOutputFile,
cfgDirs: o.CfgDirs,
lastCfgDirFiles: make([]map[string]struct{}, len(o.CfgDirs)),
watcher: newWatcher(logger, reg, o.DelayInterval),
watchedDirs: o.WatchedDirs,
watchInterval: o.WatchInterval,
retryInterval: o.RetryInterval,
tolerateEnvVarExpansionErrors: o.TolerateEnvVarExpansionErrors,
reloads: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_reloads_total",
Help: "Total number of reload requests.",
},
),
reloadErrors: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_reloads_failed_total",
Help: "Total number of reload requests that failed.",
},
),
lastReloadSuccess: promauto.With(reg).NewGauge(
prometheus.GaugeOpts{
Name: "reloader_last_reload_successful",
Help: "Whether the last reload attempt was successful",
},
),
lastReloadSuccessTimestamp: promauto.With(reg).NewGauge(
prometheus.GaugeOpts{
Name: "reloader_last_reload_success_timestamp_seconds",
Help: "Timestamp of the last successful reload",
},
),
configApply: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_config_apply_operations_total",
Help: "Total number of config apply operations.",
},
),
configApplyErrors: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_config_apply_operations_failed_total",
Help: "Total number of config apply operations that failed.",
},
),
configEnvVarExpansionErrors: promauto.With(reg).NewGauge(
prometheus.GaugeOpts{
Name: "reloader_config_environment_variable_expansion_errors",
Help: "Number of environment variable expansions that failed during the last operation.",
},
),
reloaderInfo: promauto.With(reg).NewGaugeVec(
prometheus.GaugeOpts{
Name: "reloader_info",
Help: "A metric with a constant '1' value labeled by reload method (either 'http' or 'signal').",
},
[]string{"method"},
),
}
if o.ProcessName != "" {
r.tr = NewPIDReloader(r.logger, o.ProcessName, o.RuntimeInfoURL, o.HTTPClient)
r.reloaderInfo.WithLabelValues("signal").Set(1)
} else {
r.tr = NewHTTPReloader(r.logger, o.ReloadURL, o.HTTPClient)
r.reloaderInfo.WithLabelValues("http").Set(1)
}
return r
}
// Watch detects any change made to the watched config file and directories. It
// returns when the context is canceled.
// Whenever a filesystem change is detected or the watch interval has elapsed,
// the reloader expands the config file (if cfgOutputFile is specified) and
// triggers a reload if the configuration file or files in the watched
// directories have changed.
// Because some edge cases might be missing, the reloader also relies on the
// watch interval.
func (r *Reloader) Watch(ctx context.Context) error {
if r.cfgFile == "" && len(r.cfgDirs) == 0 && len(r.watchedDirs) == 0 {
level.Info(r.logger).Log("msg", "nothing to be watched")
<-ctx.Done()
return nil
}
if _, ok := r.tr.(*PIDReloader); ok {
level.Info(r.logger).Log("msg", "reloading via process signal")
} else {
level.Info(r.logger).Log("msg", "reloading via HTTP")
}
defer runutil.CloseWithLogOnErr(r.logger, r.watcher, "config watcher close")
if r.cfgFile != "" {
if err := r.watcher.addFile(r.cfgFile); err != nil {
return errors.Wrapf(err, "add config file %s to watcher", r.cfgFile)
}
initialSyncCtx, initialSyncCancel := context.WithTimeout(ctx, r.watchInterval)
err := r.apply(initialSyncCtx)
initialSyncCancel()
if err != nil {
return err
}
}
for _, cfgDir := range r.cfgDirs {
dir := cfgDir.Dir
if err := r.watcher.addDirectory(dir); err != nil {
return errors.Wrapf(err, "add directory %s to watcher", dir)
}
}
if r.watchInterval == 0 {
// Skip watching the file-system.
return nil
}
for _, dir := range r.watchedDirs {
if err := r.watcher.addDirectory(dir); err != nil {
return errors.Wrapf(err, "add directory %s to watcher", dir)
}
}
// Start watching the file-system.
var wg sync.WaitGroup
wg.Add(1)
go func() {
r.watcher.run(ctx)
wg.Done()
}()
cfgDirsNames := make([]string, 0, len(r.cfgDirs))
for _, cfgDir := range r.cfgDirs {
cfgDirsNames = append(cfgDirsNames, cfgDir.Dir)
}
level.Info(r.logger).Log(
"msg", "started watching config file and directories for changes",
"cfg", r.cfgFile,
"cfgDirs", strings.Join(cfgDirsNames, ","),
"out", r.cfgOutputFile,
"dirs", strings.Join(r.watchedDirs, ","))
applyCtx, applyCancel := context.WithTimeout(ctx, r.watchInterval)
for {
select {
case <-applyCtx.Done():
if ctx.Err() != nil {
applyCancel()
wg.Wait()
return nil
}
case <-r.watcher.notify:
}
// Reset the watch timeout.
applyCancel()
applyCtx, applyCancel = context.WithTimeout(ctx, r.watchInterval)
r.configApply.Inc()
if err := r.apply(applyCtx); err != nil {
r.configApplyErrors.Inc()
level.Error(r.logger).Log("msg", "apply error", "err", err)
continue
}
}
}
func (r *Reloader) normalize(inputFile, outputFile string) error {
b, err := os.ReadFile(inputFile)
if err != nil {
return errors.Wrap(err, "read file")
}
// Detect and extract gzipped file.
if bytes.Equal(b[0:3], firstGzipBytes) {
zr, err := gzip.NewReader(bytes.NewReader(b))
if err != nil {
return errors.Wrap(err, "create gzip reader")
}
defer runutil.CloseWithLogOnErr(r.logger, zr, "gzip reader close")
b, err = io.ReadAll(zr)
if err != nil {
return errors.Wrap(err, "read compressed config file")
}
}
b, err = r.expandEnv(b)
if err != nil {
return errors.Wrap(err, "expand environment variables")
}
tmpFile := outputFile + ".tmp"
defer func() {
_ = os.Remove(tmpFile)
}()
if err := os.WriteFile(tmpFile, b, 0644); err != nil {
return errors.Wrap(err, "write file")
}
if err := os.Rename(tmpFile, outputFile); err != nil {
return errors.Wrap(err, "rename file")
}
return nil
}
// apply triggers Prometheus reload if rules or config changed. If cfgOutputFile is set, we also
// expand env vars into config file before reloading.
// Reload is retried in retryInterval until watchInterval.
func (r *Reloader) apply(ctx context.Context) error {
var (
cfgHash []byte
watchedDirsHash []byte
)
if r.cfgFile != "" {
h := sha256.New()
if err := hashFile(h, r.cfgFile); err != nil {
return errors.Wrap(err, "hash file")
}
cfgHash = h.Sum(nil)
if r.cfgOutputFile != "" {
if err := r.normalize(r.cfgFile, r.cfgOutputFile); err != nil {
return err
}
}
}
cfgDirsHash := make([][]byte, len(r.cfgDirs))
cfgDirsChanged := len(r.lastCfgDirsHash) == 0 && len(r.cfgDirs) > 0
for i, cfgDir := range r.cfgDirs {
h := sha256.New()
walkDir, err := filepath.EvalSymlinks(cfgDir.Dir)
if err != nil {
return errors.Wrap(err, "dir symlink eval")
}
outDir, err := filepath.EvalSymlinks(cfgDir.OutputDir)
if err != nil {
return errors.Wrap(err, "dir symlink eval")
}
cfgDirFiles := map[string]struct{}{}
entries, err := os.ReadDir(walkDir)
if err != nil {
return errors.Wrapf(err, "read dir: %s", walkDir)
}
for _, entry := range entries {
path := filepath.Join(walkDir, entry.Name())
// Make sure to follow a symlink before checking if it is a directory.
targetFile, err := os.Stat(path)
if err != nil {
return errors.Wrapf(err, "stat file: %s", path)
}
if targetFile.IsDir() {
continue
}
if err := hashFile(h, path); err != nil {
return errors.Wrapf(err, "build hash for file: %s", path)
}
outFile := filepath.Join(outDir, targetFile.Name())
cfgDirFiles[outFile] = struct{}{}
if err := r.normalize(path, outFile); err != nil {
return errors.Wrapf(err, "move file: %s", path)
}
}
if r.lastCfgDirFiles[i] != nil {
if !maps.Equal(r.lastCfgDirFiles[i], cfgDirFiles) {
for outFile := range r.lastCfgDirFiles[i] {
if _, ok := cfgDirFiles[outFile]; !ok {
if err := os.Remove(outFile); err != nil {
return err
}
}
}
}
}
r.lastCfgDirFiles[i] = cfgDirFiles
cfgDirsHash[i] = h.Sum(nil)
// Skip comparing bytes if we already set the flag.
if !cfgDirsChanged && !bytes.Equal(r.lastCfgDirsHash[i], cfgDirsHash[i]) {
cfgDirsChanged = true
}
}
h := sha256.New()
for _, dir := range r.watchedDirs {
walkDir, err := filepath.EvalSymlinks(dir)
if err != nil {
return errors.Wrap(err, "dir symlink eval")
}
err = filepath.Walk(walkDir, func(path string, f os.FileInfo, err error) error {
if err != nil {
return err
}
// filepath.Walk uses Lstat to retrieve os.FileInfo. Lstat does not
// follow symlinks. Make sure to follow a symlink before checking
// if it is a directory.
targetFile, err := os.Stat(path)
if err != nil {
return err
}
if targetFile.IsDir() {
return nil
}
if err := hashFile(h, path); err != nil {
return err
}
return nil
})
if err != nil {
return errors.Wrap(err, "build hash")
}
}
if len(r.watchedDirs) > 0 {
watchedDirsHash = h.Sum(nil)
}
if !r.forceReload && !cfgDirsChanged && bytes.Equal(r.lastCfgHash, cfgHash) && bytes.Equal(r.lastWatchedDirsHash, watchedDirsHash) {
// Nothing to do.
return nil
}
if err := runutil.RetryWithLog(r.logger, r.retryInterval, ctx.Done(), func() error {
if r.watchInterval == 0 {
return nil
}
r.reloads.Inc()
if err := r.triggerReload(ctx); err != nil {
r.reloadErrors.Inc()
r.lastReloadSuccess.Set(0)
return errors.Wrap(err, "trigger reload")
}
r.forceReload = false
r.lastCfgHash = cfgHash
r.lastCfgDirsHash = cfgDirsHash
r.lastWatchedDirsHash = watchedDirsHash
cfgDirsNames := make([]string, 0, len(r.cfgDirs))
for _, cfgDir := range r.cfgDirs {
cfgDirsNames = append(cfgDirsNames, cfgDir.Dir)
}
level.Info(r.logger).Log(
"msg", "Reload triggered",
"cfg_in", r.cfgFile,
"cfg_out", r.cfgOutputFile,
"cfg_dirs", strings.Join(cfgDirsNames, ", "),
"watched_dirs", strings.Join(r.watchedDirs, ", "))
r.lastReloadSuccess.Set(1)
r.lastReloadSuccessTimestamp.SetToCurrentTime()
return nil
}); err != nil {
r.forceReload = true
level.Error(r.logger).Log("msg", "Failed to trigger reload. Retrying.", "err", err)
}
return nil
}
func (r *Reloader) triggerReload(ctx context.Context) error {
if err := r.tr.TriggerReload(ctx); err != nil {
return err
}
return nil
}
func hashFile(h hash.Hash, fn string) error {
f, err := os.Open(filepath.Clean(fn))
if err != nil {
return err
}
defer runutil.CloseWithErrCapture(&err, f, "close file")
if _, err := h.Write([]byte{'\xff'}); err != nil {
return err
}
if _, err := h.Write([]byte(fn)); err != nil {
return err
}
if _, err := h.Write([]byte{'\xff'}); err != nil {
return err
}
if _, err := io.Copy(h, f); err != nil {
return err
}
return nil
}
type PIDReloader struct {
pname string
prt *prometheusReloadTracker
}
func NewPIDReloader(logger log.Logger, processName string, u *url.URL, c http.Client) *PIDReloader {
return &PIDReloader{
pname: processName,
prt: &prometheusReloadTracker{
client: c,
runtimeURL: u,
logger: logger,
},
}
}
func (pr *PIDReloader) TriggerReload(ctx context.Context) error {
if err := pr.prt.preReload(ctx); err != nil {
return fmt.Errorf("pre-reload check failed: %w", err)
}
procs, err := ps.Processes()
if err != nil {
return fmt.Errorf("list processes: %w", err)
}
var proc ps.Process
for i := range procs {
if pr.pname == procs[i].Executable() {
proc = procs[i]
break
}
}
if proc == nil {
return fmt.Errorf("failed to find process matching %q", pr.pname)
}
p, err := os.FindProcess(proc.Pid())
if err != nil {
return fmt.Errorf("find process err: %w", err)
}
if p == nil {
return fmt.Errorf("failed to find process with pid %d", proc.Pid())
}
if err := p.Signal(syscall.SIGHUP); err != nil {
return fmt.Errorf("failed to send SIGHUP to pid %d: %w", p.Pid, err)
}
if err := pr.prt.postReload(ctx); err != nil {
return fmt.Errorf("post-reload check failed: %w", err)
}
return nil
}
var _ = TriggerReloader(&PIDReloader{})
type HTTPReloader struct {
logger log.Logger
u *url.URL
c http.Client
}
var _ = TriggerReloader(&HTTPReloader{})
func NewHTTPReloader(logger log.Logger, u *url.URL, c http.Client) *HTTPReloader {
return &HTTPReloader{
logger: logger,
u: u,
c: c,
}
}
func (hr *HTTPReloader) TriggerReload(ctx context.Context) error {
req, err := http.NewRequest("POST", hr.u.String(), nil)
if err != nil {
return errors.Wrap(err, "create request")
}
req = req.WithContext(ctx)
resp, err := hr.c.Do(req)
if err != nil {
return errors.Wrap(err, "reload request failed")
}
defer runutil.ExhaustCloseWithLogOnErr(hr.logger, resp.Body, "trigger reload resp body")
if resp.StatusCode != 200 {
return errors.Errorf("received non-200 response: %s; have you set `--web.enable-lifecycle` Prometheus flag?", resp.Status)
}
return nil
}
// ReloadURLFromBase returns the standard Prometheus reload URL from its base URL.
func ReloadURLFromBase(u *url.URL) *url.URL {
r := *u
r.Path = path.Join(r.Path, "/-/reload")
return &r
}
// RuntimeInfoURLFromBase returns the standard Prometheus runtime info URL from its base URL.
func RuntimeInfoURLFromBase(u *url.URL) *url.URL {
return u.JoinPath("/api/v1/status/runtimeinfo")
}
var envRe = regexp.MustCompile(`\$\(([a-zA-Z_0-9]+)\)`)
func (r *Reloader) expandEnv(b []byte) (replaced []byte, err error) {
configEnvVarExpansionErrorsCount := 0
replaced = envRe.ReplaceAllFunc(b, func(n []byte) []byte {
if err != nil {
return nil
}
m := n
n = n[2 : len(n)-1]
v, ok := os.LookupEnv(string(n))
if !ok {
configEnvVarExpansionErrorsCount++
errStr := errors.Errorf("found reference to unset environment variable %q", n)
if r.tolerateEnvVarExpansionErrors {
level.Warn(r.logger).Log("msg", "expand environment variable", "err", errStr)
return m
}
err = errStr
return nil
}
return []byte(v)
})
r.configEnvVarExpansionErrors.Set(float64(configEnvVarExpansionErrorsCount))
return replaced, err
}
type watcher struct {
notify chan struct{}
w *fsnotify.Watcher
watchedDirs map[string]struct{}
delayInterval time.Duration
logger log.Logger
watchedItems prometheus.Gauge
watchEvents prometheus.Counter
watchErrors prometheus.Counter
}
func newWatcher(logger log.Logger, reg prometheus.Registerer, delayInterval time.Duration) *watcher {
return &watcher{
logger: logger,
delayInterval: delayInterval,
notify: make(chan struct{}),
watchedDirs: make(map[string]struct{}),
watchedItems: promauto.With(reg).NewGauge(
prometheus.GaugeOpts{
Name: "reloader_watches",
Help: "Number of resources watched by the reloader.",
},
),
watchEvents: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_watch_events_total",
Help: "Total number of events received by the reloader from the watcher.",
},
),
watchErrors: promauto.With(reg).NewCounter(
prometheus.CounterOpts{
Name: "reloader_watch_errors_total",
Help: "Total number of errors received by the reloader from the watcher.",
},
),
}
}
// Close implements the io.Closer interface.
func (w *watcher) Close() error {
if w.w == nil {
return nil
}
watcher := w.w
w.w = nil
return watcher.Close()
}
func (w *watcher) addPath(name string) error {
if w.w == nil {
fsWatcher, err := fsnotify.NewWatcher()
if err != nil {
return errors.Wrap(err, "create watcher")
}
w.w = fsWatcher
}
if err := w.w.Add(name); err != nil {
return err
}
w.watchedDirs[name] = struct{}{}
w.watchedItems.Set(float64(len(w.watchedDirs)))
return nil
}
func (w *watcher) addDirectory(name string) error {
w.watchedDirs[name] = struct{}{}
return w.addPath(name)
}
func (w *watcher) addFile(name string) error {
w.watchedDirs[filepath.Dir(name)] = struct{}{}
return w.addPath(name)
}
func (w *watcher) run(ctx context.Context) {
defer runutil.CloseWithLogOnErr(w.logger, w.w, "config watcher close")
var (
wg sync.WaitGroup
notify = make(chan struct{})
)
wg.Add(1)
go func() {
defer wg.Done()
var (
delayCtx context.Context
cancel context.CancelFunc
)
for {
select {
case <-ctx.Done():
if cancel != nil {
cancel()
}
return
case <-notify:
if cancel != nil {
cancel()
}
delayCtx, cancel = context.WithCancel(ctx)
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
if w.delayInterval > 0 {
t := time.NewTicker(w.delayInterval)
defer t.Stop()
select {
case <-ctx.Done():
return
case <-t.C:
}
}
select {
case w.notify <- struct{}{}:
case <-ctx.Done():
}
}(delayCtx)
}
}
}()
for {
select {
case <-ctx.Done():
wg.Wait()
return
case event := <-w.w.Events:
w.watchEvents.Inc()
if _, ok := w.watchedDirs[filepath.Dir(event.Name)]; ok {
select {
case notify <- struct{}{}:
default:
}
}
case err := <-w.w.Errors:
w.watchErrors.Inc()
level.Error(w.logger).Log("msg", "watch error", "err", err)
}
}
}