/
log.go
112 lines (104 loc) · 2.58 KB
/
log.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
package health
import (
"context"
"errors"
"fmt"
"time"
"go.uber.org/zap"
)
func (c *Check) logHealthFail(err error) {
if !c.isDisruption() {
c.status.disruption = true
c.lg.Info(
"service disruption detected",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", false),
zap.Time("start", c.status.logDisruptionStart),
zap.Error(err),
)
return
}
//sanitize context error
if errors.Is(err, context.DeadlineExceeded) {
err = fmt.Errorf("client timeout exceeded: %s", DefaultContextDeadline)
}
c.lg.Info("health check",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", false),
zap.Time("start", c.status.logDisruptionStart),
zap.Error(err),
)
return
}
func (c *Check) logSlowRequest(err error) {
if !c.isDisruption() {
c.lg.Info(
"possible service disruption detected",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", false),
zap.Time("start", c.status.logDisruptionStart),
zap.Error(err),
)
}
return
}
func (c *Check) logHealthSuccess() {
if c.isDisruption() {
c.status.logDisruptionEnd = time.Now()
duration := time.Since(c.status.logDisruptionStart)
c.lg.Info(
"service restored",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", true),
zap.Time("start", c.status.logDisruptionStart),
zap.Time("end", c.status.logDisruptionEnd),
zap.Duration("duration", duration),
)
// reset status
c.status.disruption = false
c.status.slowRequest = false
c.status.logDisruptionStart = time.Time{}
c.status.logDisruptionEnd = time.Time{}
return
}
c.status.slowRequest = false
c.lg.Debug("health check",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", true),
)
return
}
func (c *Check) LogTermination() {
// close etcd client
c.client.Close()
if c.isDisruption() || c.isSlowRequest() {
c.status.logDisruptionEnd = time.Now()
duration := time.Since(c.status.logDisruptionStart)
c.lg.Warn("health probe terminated",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", false),
zap.Time("start", c.status.logDisruptionStart),
zap.Time("end", c.status.logDisruptionEnd),
zap.Duration("duration", duration),
)
return
}
c.lg.Info("health probe terminated",
zap.Strings("addresses", c.targets),
zap.Any("check", c.name),
zap.Bool("health", true),
)
return
}
func (c *Check) isDisruption() bool {
return c.status.disruption
}
func (c *Check) isSlowRequest() bool {
return c.status.slowRequest
}