Skip to content

Commit 7ce3a5b

Browse files
committed
httpmonitorscanner: lift logging up
1 parent a962290 commit 7ce3a5b

File tree

2 files changed

+20
-24
lines changed

2 files changed

+20
-24
lines changed

cmd/alertmanager/httpmonitorscanner.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func httpMonitorScanAndAlertFailures(ctx context.Context, app *amstate.App) erro
2525
failures := scanMonitors(
2626
ctx,
2727
app.State.HttpMonitors(),
28-
newScanner(logex.Prefix("httpscanner", app.Logger)))
28+
newScanner(),
29+
logex.Prefix("httpscanner", app.Logger))
2930

3031
// convert monitor failures into alerts
3132
alerts := []amstate.Alert{}
@@ -47,22 +48,35 @@ func scanMonitors(
4748
ctx context.Context,
4849
monitors []amstate.HttpMonitor,
4950
scanner HttpMonitorScanner,
51+
logger *log.Logger,
5052
) []monitorFailure {
53+
logl := logex.Levels(logger)
54+
5155
failed := []monitorFailure{}
5256
failedMu := sync.Mutex{}
5357

5458
checkOne := func(monitor amstate.HttpMonitor) {
5559
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
5660
defer cancel()
5761

58-
if err := scanner.Scan(ctx, monitor); err != nil {
62+
started := time.Now()
63+
64+
err := scanner.Scan(ctx, monitor)
65+
66+
durationMs := time.Since(started).Milliseconds()
67+
68+
if err != nil {
5969
failedMu.Lock()
6070
defer failedMu.Unlock()
6171

6272
failed = append(failed, monitorFailure{
6373
err,
6474
monitor,
6575
})
76+
77+
logl.Error.Printf("❌ %s @ %d ms => %v", monitor.Url, durationMs, err.Error())
78+
} else {
79+
logl.Debug.Printf("✔️ %s @ %d ms", monitor.Url, durationMs)
6680
}
6781
}
6882

@@ -88,13 +102,11 @@ type HttpMonitorScanner interface {
88102
}
89103

90104
type scanner struct {
91-
logl *logex.Leveled
92105
noRedirects *http.Client
93106
}
94107

95-
func newScanner(logger *log.Logger) HttpMonitorScanner {
108+
func newScanner() HttpMonitorScanner {
96109
return &scanner{
97-
logex.Levels(logger),
98110
&http.Client{
99111
CheckRedirect: func(req *http.Request, via []*http.Request) error {
100112
return http.ErrUseLastResponse // do not follow redirects
@@ -104,22 +116,6 @@ func newScanner(logger *log.Logger) HttpMonitorScanner {
104116
}
105117

106118
func (s *scanner) Scan(ctx context.Context, monitor amstate.HttpMonitor) error {
107-
started := time.Now()
108-
109-
err := s.scanInternal(ctx, monitor)
110-
111-
durationMs := time.Since(started).Milliseconds()
112-
113-
if err != nil {
114-
s.logl.Error.Printf("❌ %s @ %d ms => %v", monitor.Url, durationMs, err.Error())
115-
} else {
116-
s.logl.Debug.Printf("✔️ %s @ %d ms", monitor.Url, durationMs)
117-
}
118-
119-
return err
120-
}
121-
122-
func (s *scanner) scanInternal(ctx context.Context, monitor amstate.HttpMonitor) error {
123119
resp, err := ezhttp.Get(
124120
ctx,
125121
monitor.Url,

cmd/alertmanager/httpmonitorscanner_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestOneFails(t *testing.T) {
1818
Url: "http://example.com/contacts",
1919
2020
},
21-
}, &testScanner{})
21+
}, &testScanner{}, nil)
2222

2323
assert.Assert(t, len(failures) == 1)
2424
assert.EqualString(
@@ -37,7 +37,7 @@ func TestAllSucceed(t *testing.T) {
3737
Url: "http://example.com/contacts",
3838
3939
},
40-
}, &testScanner{})
40+
}, &testScanner{}, nil)
4141

4242
assert.Assert(t, len(failures) == 0)
4343
}
@@ -48,7 +48,7 @@ func Test404(t *testing.T) {
4848
Url: "http://notfound.net/",
4949
Find: "doesntmatter",
5050
},
51-
}, &testScanner{})
51+
}, &testScanner{}, nil)
5252

5353
assert.Assert(t, len(failures) == 1)
5454
assert.EqualString(t, failures[0].err.Error(), "404: http://notfound.net/")

0 commit comments

Comments
 (0)